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?
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?
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.