Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java

 
Babar Saeed
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am moving this post to Java in general( Beginner ) forum. Please restrict the discussions in this forum to topics related to this site.
 
Dale DeMott
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;

System.out.println(myFloat);
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.
Dale

------------------
What's this H2SO4 doing in my fridge?? ( thud )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic