Description |
IEEERemainder gives the remainder when dividing two floating point numbers. The mechanics of the process adhere to IEEE standards, but are not intuitive.
The algorithm used is as follows:
Quotient = X / Y
RoundedQuotient = Round(Quotient)
Remainder = X - (Y * RoundedQuotient)
Note : The Round function uses so-called Bankers rules : rounding is to the nearest whole integer except that a value of exactly *.5 always gets to the nearest even integer.
|
| Notes | Static methods are not methods of an object - they are simply class functions or procedures available at any time.
|
| References | Math
|
|
Microsoft MSDN Links |
System
System.Math
|
|
|
Illustrating the peculiarities of IEEERemainder |
program Project1;
{$APPTYPE CONSOLE}
var
float : Double;
begin
// 10/5 gives quotient of 2.0 which gets rounded to 2
// The returned remainder = 10 - (5 * 2) = 0
float := System.Math.IEEERemainder(10, 5);
Console.WriteLine('Remainder of 10 /5 = {0}', float.ToString);
// 12/5 gives quotient of 2.4 which gets rounded to 2
// The returned remainder = 12 - (5 * 2) = 2
float := System.Math.IEEERemainder(12, 5);
Console.WriteLine('Remainder of 12 /5 = {0}', float.ToString);
// 12.5/5 gives quotient of 2.5 which gets rounded to 2
// The returned remainder = 12.5 - (5 * 2) = 2.5
float := System.Math.IEEERemainder(12.5, 5);
Console.WriteLine('Remainder of 12.5/5 = {0}', float.ToString);
// 13/5 gives quotient of 2.6 which gets rounded to 3
// The returned remainder = 13 - (5 * 3) = -2
float := System.Math.IEEERemainder(13, 5);
Console.WriteLine('Remainder of 13 /5 = {0}', float.ToString);
// This illustrates Banker's rounding to the nearest even integer
// 17.5/5 gives quotient of 3.5 which gets rounded to 4
// The returned remainder = 17.5 - (5 * 4) = -2.5
float := System.Math.IEEERemainder(17.5, 5);
Console.WriteLine('Remainder of 17.5/5 = {0}', float.ToString);
// Divide by zero returns NaN - Not a Number
float := System.Math.IEEERemainder(1, 0);
Console.WriteLine('Remainder of 1 /0 = {0}', float.ToString);
Console.ReadLine;
end.
| Show full unit code | Remainder of 10 /5 = 0
Remainder of 12 /5 = 2
Remainder of 12.5/5 = 2.5
Remainder of 13 /5 = -2
Remainder of 17.5/5 = -2.5
Remainder of 1 /0 = NaN
|
|
|
|