• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

java

 
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.

------------------
 
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.
 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
6.3f-Math.round(6.3f/2.1f)*2.1f
mimicing 'real modulus' computation.
So I would not use 'modulus' with floats.
 
permaculture is largely about replacing oil with people. And one tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!