0

I was writing a simple program that happens to multiply a double by 10 a few times dependent on a loop count variable and with everything i put in to it. It returned the correct value but for some reason when the double 0.802 was multiplied by 10 three times this is what happened. 0.802 * 10 = 8.02. 8.02 * 10 = 80.1999999999. 80.1999999999 * 10 = 801.99999999999. (dont mind how many nines are on the end of each of those).

Essentially i thought this had to do with something regarding the behavior of a double and the conversion from int to double so i tried casting entire expression and changing 10 to 10d but this dident work. I have a suspicion that i have to use Math.round but this program i was hoping to be independent from any outside method calls so alternative fixes would be appreciated.

This is some test code i used the first one spits out the correct answer while the second does not.

1.
double number = 0.802;
number = (double) number * 1000;

2.
double number = 0.802;
number = (double) number * 10;
number = (double) number * 10;
number = (double) number * 10;

This code when run should give the answer 802.

  • 1
    You are unlikely to solve this without any method calls. – Louis Wasserman Jul 07 '19 at 04:34
  • Maybe it is same like in C#, where **double** type you use for scientific calculations and **decimal** type you use for financial and it might be matter of decimal accuracy for different types. – bakunet Jul 07 '19 at 04:35
  • You have to do binary decimal multiplication to prove the answer you want – Joe Jul 07 '19 at 04:36
  • `System.out.println(0.05 + 0.05 + 0.05);` – Elliott Frisch Jul 07 '19 at 04:37
  • This is because decimal values can’t be represented exactly by float or double. One suggestion : Avoid float and double where exact answers are required. Use BigDecimal, int, or long instead I suggest reading the following article (especially the 3 paragraphs on "Rounding Errors"): https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – Pedro Coelho Jul 07 '19 at 04:38
  • What you are seeing here is an inherent property of binary floating point arithmetic. How do you "solve" it? Probably by doing what you are doing in another way; e.g. by displaying the number with fewer significant digits, or by using a different type. But you *also* need to understand *why* this is happening so that you can make the correct choices when solving the problem. – Stephen C Jul 07 '19 at 04:40
  • You assumption about what the answer should be is incorrect. Some values cannot be precisely represented using a double. That's why whenever we compare floating point values we compare them up to a certain precision epsilon like 10^-8. – LeffeBrune Jul 07 '19 at 04:40
  • `BigDecimal` is your friend. – Basil Bourque Jul 07 '19 at 06:19

0 Answers0