• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IEEremainder

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got, Math.IEEEremainder(2, 5) == 2.0
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Great!I got it.
thank you val!
reply
    Bookmark Topic Watch Topic
  • New Topic