File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Ranch Office and the fly likes java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » This Site » Ranch Office
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).
Peter Gragert
Ranch Hand

Joined: Jan 16, 2001
Posts: 424
Strange, I thaught that % (= modulus computation) is only defined for integers.
But experimenting shows that it works in Java too for floats
and then you can get rounding errors, because not all floats can be expressed exactly in (internal) binary format.
What is done looks like:
mimicing 'real modulus' computation.
So I would not use 'modulus' with floats.
I agree. Here's the link:
subject: java
jQuery in Action, 3rd edition