• 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

java

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

------------------
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 515
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 )
 
reply
    Bookmark Topic Watch Topic
  • New Topic