• 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

Modulo operator

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Why does this print 2.1999999999999966 instead of 0
<pre><code>

public strictfp class Test {
public static void main(String[] args)
{
double a = 44d;
a = a % 2.2d;
System.out.println(a);
}
}
</pre></code>
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sapna,
JLS states " Instead, the Java programming language defines % on floating-point operations to behave in a manner analogous to that of the integer remainder operator; this may be compared with the C library function fmod. "
So the computation works like a integer modulo operation. For your example: 2.2d is treated as a 2, so the remainder is 2....
Cause its a double you have a "incorrect" value, when displaying it...
hope that helps,
correct me if i'm wrong
Oliver
 
Sapna Mathur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

No I dont. Can you tell me please ?
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can see it for yourself using the code

a = a - 2.2 is the operation being done continously when we do % operation.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It has to do with the precision of doubles and floats. Since doubles are not precise numbers like integers, you lose a .00000001 here or there. So you end up with 44d % 2.2d not ever giving you 0 becuase like lakshmi said, it is like writing 44d - 2.2d over and over until you are less than 2.2d.
You can try this in a loop like this:
 
Sapna Mathur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So how can I get an accurate modulus value if I have to use it
in a program ? Will it not affect the accuracy of the program if an incorrect value is produced in the beginning of a process?
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sapna Mathur:

So how can I get an accurate modulus value if I have to use it
in a program ? Will it not affect the accuracy of the program if an incorrect value is produced in the beginning of a process?


You can't. It is to do with the way java stores the floating point numbers internally. You will always get an approximate value while dealing with floating points no matter what operation you perform on them.
HTH
Shubhangi
 
Sapna Mathur
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wow. That is amazing. Thanks.
Best of luck with your exam.
 
lakshmi nair
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just wanted to share some knowledge i gained.
There is a method Math.IEEEremainder(double,double) which can give you the a result which can be sometimes equivalent to % operation.
Here goes the actual explanation from API..
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.

and some results...
IEEEremainder(10,2.5) 0.0
IEEEremainder(10.5, 2.5) 0.5
IEEEremainder(11, 2.5) 1.0
IEEEremainder(9.5, 2.5) -0.5
IEEEremainder(9.0, 2.5) -1.0
IEEEremainder(8.0, 2.5) 0.5
IEEEremainder(8.5, 2.5) 1.0
IEEEremainder(7.4, 2.5) -0.09999999999999964

lakshmi


[This message has been edited by lakshmi nair (edited November 16, 2000).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic