0
 public class Myclass
 {
     public static void main(String[] args)
     {
        float  af=5.2345556f;
        System.out.println(af);
     }
 }

why the result is 5.2345557 not 5.2345556?

wanglugao
  • 281
  • 2
  • 10
  • 2
    ... because you're using `float`, which doesn't have as much precision as you're expecting. Bound to be a dupe, but I'm not sure I have the time to find it right now. – Jon Skeet Jul 23 '15 at 11:35

1 Answers1

1

Because, you can't represent the value 5.2345556 exactly in bits, so an approximate value will be used. This is characteristic of floating point arithmetic

Squeezing infinitely many real numbers into a finite number of bits requires an approximate representation. Although there are infinitely many integers, in most programs the result of integer computations can be stored in 32 bits.

In contrast, given any fixed number of bits, most calculations with real numbers will produce quantities that cannot be exactly represented using that many bits. Therefore the result of a floating-point calculation must often be rounded in order to fit back into its finite representation. This rounding error is the characteristic feature of floating-point computation.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
  • 1
    thanks!and i also found this [Floating_point](https://en.wikipedia.org/wiki/Floating_point),it may help understand is. – wanglugao Jul 26 '15 at 03:27