• 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

int -> float -> int : Weird rounding

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Results in:
i:111111111
f:1.11111112E8
i_f:111111112
What's up with that? How can I do the conversion of an int to a float and back to an int such that the last int value is the same as the first?
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's just the way floating points work... they goof around if you switch. If you really wanted to, you could look into BigDecimal, but I'd make sure you absolutely have to go from int to float to int first. Usually, you work around it keeping the numbers in ints except the exact point where you need a float.
Dig?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An int represents an exact number. A float, on the other hand, does not. In binary, 111111111 is 110100111110110101111000111. This is a 27-bit number. But the float type only offers 24 bits of precision. That means that 111111111 can't actually be represented precisely in a float, and so it will be rounded to the nearest exactly-representable value. 111111112 is 110100111110110101111001000 in binary; the low-order three bits are zero, so you only need 24 bits to represent it exactly. So when you store 111111111 (or 111111113, or 111111115, or any other nearby value) in a float, 111111112 is the value that will actually be stored. When you convert back to an int, the original value has been forgotten, and you get the stored value instead.
Here are some details from the Java Language Specification.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For a good understanding of how floating point numbers are represented, I recommend taking a look at the "Sum lack of precision!" conversation from the Cattle Drive forum, and following the various links it contains.
 
And then we all jump out and yell "surprise! we got you this tiny ad!"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic