It's not a secret anymore!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes IEEremainder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "IEEremainder" Watch "IEEremainder" New topic


Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
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
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
I got, Math.IEEEremainder(2, 5) == 2.0

<a href="" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
I am very confused:
Math.IEEEremainder(3,5)==-2.0,while Math.IEEEremainder(2,5)==2.0
who can explain it to me?

help you means help me
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
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...

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 ]

[Blog] [Blogroll] [My Reviews] My Linked In
Seany Iris
Ranch Hand

Joined: Jan 08, 2002
Posts: 54
Great!I got it.
thank you val!
I agree. Here's the link:
subject: IEEremainder
It's not a secret anymore!