1

i have a variable with initial value : 34-640.4-71.2.image1

It should appear 0,instead the result is negative. Could you please tell me the reason? thanks image2

1 Answers1

1

This has to do with how variables are stored in Java.

double variables are precise up to a certain number of decimals digits. In your equation, while none of its parts exceed 1 decimal, rounding is done in the binary format causing this very minor inaccuracy at the 16th decimal.

-1.776E-15 is equal to -0.000000000000001776.

Here is an interesting thread that can give you more insights on the topic:

Whats wrong with this simple 'double' calculation?

One thing you can do to overcome your problem is to round off the error using:

roundToDecimal( 34 - 64*0.4 -7*1.2 , 14 )

This would round your number to 14 decimal places thus rounding off the inaccuracy.

Emile Zankoul
  • 2,161
  • 2
  • 6
  • 18