I'm currently in the process of learning Java. I have just run the following line in Eclipse in Win7 using jre1.8.0_25:
System.out.println(4.5 * 7.9);
The console output is: 35.550000000000004
I'm just wondering why the output is wrong.
I'm currently in the process of learning Java. I have just run the following line in Eclipse in Win7 using jre1.8.0_25:
System.out.println(4.5 * 7.9);
The console output is: 35.550000000000004
I'm just wondering why the output is wrong.
When looking for accuracy, doubles are not the best choice, as they are actually not as perfect as they may seem due to some limitations in their design.
It's not only a Java issue (feature?), it's a common issue for all languages with floating point arithmetics. It's based on IEEE 754 standard, in case you're interested.
It's because of loss of precision due to operations made by the computer in binary. You can see an explanation of it here.