File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java " Watch "java " New topic


Babar Saeed

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.

Ajith Kallambella

Joined: Mar 17, 2000
Posts: 5782
I am moving this post to Java in general( Beginner ) forum. Please restrict the discussions in this forum to topics related to this site.

Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Dale DeMott
Ranch Hand

Joined: Nov 02, 2000
Posts: 515
I think this is a bug in the math somewhere. Yes, you are correct. This should be 0. If you change the code to be...
float myFloat;
float firstFloat = 6.4f;
float secondFloat = 2.1f;
myFloat = firstFloat % secondFloat;

You should get .1 for your answer, however instead you get
0.10000038 for your answer. Obviously we have an lack of precision here somewhere in the logic of this operator.
Hope this helps.

What's this H2SO4 doing in my fridge?? ( thud )

By failing to prepare, you are preparing to fail.<br />Benjamin Franklin (1706 - 1790)
I agree. Here's the link:
subject: java
It's not a secret anymore!