-1

I have odd problem in my java code. Why does every number lower than 0.4f multiplied by 0.0025f gives me wrong results.

Correct result:

0.4f * 0.0025f = 0.001f

Wrong results:

0.399999f * 0.0025f = 9.999975E-4

instead of 0.000999998f

0.33333f * 0.0025f = 8.33325E-4
instead of 0.000833325f

0.11111f * 0.0025f = 2.77775E-4
instead of 0.000277775f
Manzurul Hoque Rumi
  • 2,911
  • 4
  • 20
  • 43
lyx
  • 3
  • 1

1 Answers1

3

How are you printing your results. You should maybe look at that. It sounds like you don't understand exponential notation.

9.999975E-4 == 0.000999975

The E-4 just means shift the decimal 4 places to the right.

Furthermore, you're doing your own math wrong. You have a number ending in a 9 multiplied by a number ending in 5, which means the answer is going to end in a 5 (9 x 5 is 45, after all). So it's NOT going to be 0.000999998. You got that answer from something that rounded it, perhaps a calculator that won't show it all the way out.

You don't have a math problem. You have a display problem, and not really. It's that you don't understand the display.

Perhaps look up the printf methods and use a format string with lots of room for data after the decimal.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • I got that result from calculator. I don't need to print it. I need it to further calculation. Is it needed to do something with that number or it will work without any problems in further calculations. – lyx Feb 07 '19 at 18:05
  • The calculator is rounding off. The computer is not wrong. However you're printing it is confusing you. – Joseph Larson Feb 07 '19 at 19:39