• Post Reply Bookmark Topic Watch Topic
  • New Topic

int -> float -> int : Weird rounding  RSS feed

 
David Duran
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
  • 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?
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!