Anuji Philip

Ranch Hand

Posts: 46

posted 15 years ago

In java.lang.Math package ,function IEEEremainder(double,double)

produces output differently,

IEEEremainder(2,4) = 2

IEEEremainder(2.0,5.0) = 2

IEEEremainder(2,5) =1

I thought I will get remainder zero

produces output differently,

IEEEremainder(2,4) = 2

IEEEremainder(2.0,5.0) = 2

IEEEremainder(2,5) =1

I thought I will get remainder zero

Rajinder Yadav

Ranch Hand

Posts: 178

Seany Iris

Ranch Hand

Posts: 54

Valentin Crettaz

Gold Digger

Sheriff

Sheriff

Posts: 7610

posted 15 years ago

Ok first off, the correct values are:

Math.IEEEremainder(2,4) = 2.0

Math.IEEEremainder(2.0,5.0) = 2.0

Math.IEEEremainder(2,5) = 2.0

Math.IEEEremainder(3,5) = -2.0

So please verify your outputs before posting, so that people won't have to wonder why the hell an answer is wrong. Thanks!

Moreover,I think the api doc makes it clear...

Math.IEEEremainder(2,4) = 2.0

2/4 is 0.5 and the closest integers are 0 and 1, we take the closest even one which is 0, so (double)2-4*0 = 2.0

Math.IEEEremainder(2.0,5.0) = 2.0

2.0/5.0 is 0.4 so the closest integer is 0, so (double)2.0-5.0*0 = 2.0

Math.IEEEremainder(2,5) = 2.0

2/5 is 0.4 so the closest integer is 0, so (double)2-5*0 is 2.0

Math.IEEEremainder(3,5) = 2.0

3/5 is 0.6 so the closest integer is 1, so (double)3-5*1 = -2.0

I don't see any weird results...

[ February 27, 2002: Message edited by: Valentin Crettaz ]

Math.IEEEremainder(2,4) = 2.0

Math.IEEEremainder(2.0,5.0) = 2.0

Math.IEEEremainder(2,5) = 2.0

Math.IEEEremainder(3,5) = -2.0

So please verify your outputs before posting, so that people won't have to wonder why the hell an answer is wrong. Thanks!

Moreover,I think the api doc makes it clear...

Computes the remainder operation on two arguments as prescribed by the IEEE 754 standard. The remainder value is mathematically equal to f1 - f2 � n, where n is the mathematical integer closest to the exact mathematical value of the quotient f1/f2, and if two mathematical integers are equally close to f1/f2, then n is the integer that is even. If the remainder is zero, its sign is the same as the sign of the first argument. Special cases:

- If either argument is NaN, or the first argument is infinite, or the second argument is positive zero or negative zero, then the result is NaN.

- If the first argument is finite and the second argument is infinite, then the result is the same as the first argument.

Math.IEEEremainder(2,4) = 2.0

2/4 is 0.5 and the closest integers are 0 and 1, we take the closest even one which is 0, so (double)2-4*0 = 2.0

Math.IEEEremainder(2.0,5.0) = 2.0

2.0/5.0 is 0.4 so the closest integer is 0, so (double)2.0-5.0*0 = 2.0

Math.IEEEremainder(2,5) = 2.0

2/5 is 0.4 so the closest integer is 0, so (double)2-5*0 is 2.0

Math.IEEEremainder(3,5) = 2.0

3/5 is 0.6 so the closest integer is 1, so (double)3-5*1 = -2.0

I don't see any weird results...

[ February 27, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML

[Blog] [Blogroll] [My Reviews] My Linked In