This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes java Modulus operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java Modulus operator" Watch "java Modulus operator" New topic
Author

java Modulus operator

Babar Saeed
Greenhorn

Joined: Jul 06, 2001
Posts: 13
when i compile the following code
float f = 6.3f % 2.1f;
The answer must be 0.0.
but it's not.
but when i try this code
float f = 8.4f % 2.1f;
the answer is 0.0.
Lucas Richardson
Ranch Hand

Joined: Jul 08, 2001
Posts: 32
There is a loss of precision due to the fact that it can't represent the answer as a power of two. (rather than round, it tells you 2.0999999...). It's representative of the inaccuracy of computers when it comes to floating point numbers.
 
jQuery in Action, 2nd edition
 
subject: java Modulus operator