In an application I am working some numbers get converted and saved from long(18 digits) to float/double. These numbers are like Reference/Id's but not used for calculations. Recently I noticed some discrepancies in data being stored as float/double. I am trying to understand if the behavior is due to what floating point numbers call significant digits and maybe a simple explanation for the same.
My questions based on below program are
- Output no : 5 shows a really big number(39 digits before decimal) as max value of float. Why then float cannot display anything above 7 digits accurately. It is because of is supports only 6-7 significant digits.
- Output no : 10 shows a really big number as max value of double. Why then double cannot display anything above 16 digits accurately. It is because of is supports only 15 significant digits.
- What is really meant by significant digits ? Does it mean any digits after this number will not be represented accurately regardless of whether it comes before or after decimal point ?
NOTE : After my research on this topic , I do now understand that floating point numbers are by their nature inaccurate and should not be used to represent things that require accurate representations. Still I feel a bit confused about the above behavior and significant digits.
public class Main
{
public static void main(String[] args) {
System.out.printf( "1. Float value of 50000000115 is : %,f. Expected output was 50000000115.000000 \n", 50000000115f );
System.out.printf( "2. Float value of 50000000116 is : %,f. Expected output was 50000000116.000000 \n", 50000000116f );
System.out.printf( "3. Float value of 50000000117 is : %,f. Expected output was 50000000117.000000 \n\n", 50000000117f );
System.out.printf( "4. Float value of 2175863596593954381 is : %,f. Expected output was 2175863596593954381.000000 \n\n", 2175863596593954381f );
System.out.printf( "5. Float.MAX_VALUE: %,f\n\n", Float.MAX_VALUE );
System.out.printf( "6. Double value of 50000000115 is : %,f\n", 50000000115d );
System.out.printf( "7. Double value of 50000000116 is : %,f\n", 50000000116d );
System.out.printf( "8. Double value of 50000000117 is : %,f\n\n", 50000000117d );
System.out.printf( "9. Double value of 2175863596593954381 is : %,f. Expected output was 2175863596593954381.000000 \n\n", 2175863596593954381d );
System.out.printf( "10. Double.MAX_VALUE: %,f\n\n", Double.MAX_VALUE );
System.out.printf( "11. Float value of number gives expected result till 7 digits ie 12345678 is : %,f\n", 12345678f );
System.out.printf( "12. Float value of number gives expected result till 7 digits ie 11111111 is : %,f\n", 11111111f );
System.out.printf( "13. Double value of number gives expected result till 16 digits ie 1122334455667788 is : %,f\n", 1122334455667788d );
System.out.printf( "14. Double value of number gives expected result till 16 digits ie 1111222233334444 is : %,f\n", 1111222233334444d );
}
}
Output of above program
- Float value of 50000000115 is : 49,999,998,976.000000. Expected output was 50000000115.000000
- Float value of 50000000116 is : 49,999,998,976.000000. Expected output was 50000000116.000000
Float value of 50000000117 is : 49,999,998,976.000000. Expected output was 50000000117.000000
Float value of 2175863596593954381 is : 2,175,863,554,941,386,750.000000. Expected output was 2175863596593954381.000 000
Float.MAX_VALUE: 340,282,346,638,528,860,000,000,000,000,000,000,000.000000
Double value of 50000000115 is : 50,000,000,115.000000
- Double value of 50000000116 is : 50,000,000,116.000000
Double value of 50000000117 is : 50,000,000,117.000000
Double value of 2175863596593954381 is : 2,175,863,596,593,954,300.000000. Expected output was 2175863596593954381.0 00000
Double.MAX_VALUE: 179,769,313,486,231,570,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00 0,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00 0,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,00 0,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000,000.000000
Float value of number gives expected result till 7 digits ie 12345678 is : 12,345,678.000000
- Float value of number gives expected result till 7 digits ie 11111111 is : 11,111,111.000000
- Double value of number gives expected result till 16 digits ie 1122334455667788 is : 1,122,334,455,667,788.000000
- Double value of number gives expected result till 16 digits ie 1111222233334444 is : 1,111,222,233,334,444.000000