<<< Using floating-point numbers | Index | Scientific Notation >>> |
You should be careful when comparing floating-point numbers for equality:
One way to check whether two floating-point numbers are equal is to check whether the absolute value of their difference is less than a certain tolerance.
For example, suppose the tolerance is .000001.
Then double x and y are equal if the absolute value of difference
(x – y)
is less than 0.000001.
To find the absolute value, you can use the function Math.abs of the Math class:
Math.abs(x – y) < 0.000001
The above expression determines whether the absolute value of (x – y) is less than 0.000001.
<<< Using floating-point numbers | Index | Scientific Notation >>> |