This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

IEEremainder

 
Anuji Philip
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 178
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got, Math.IEEEremainder(2, 5) == 2.0
 
Seany Iris
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am very confused:
Math.IEEEremainder(3,5)==-2.0,while Math.IEEEremainder(2,5)==2.0
who can explain it to me?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Seany Iris
Ranch Hand
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great!I got it.
thank you val!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic