1

I used function :

fixedTo(1) 

to approximate a number like this:

-3.43321e-11

but the problem is that the result of approximation is:

-0.0

with minus sign.

This is a problem because in math doesn't exist 0 with minus sign and if I do:

if(-0.0 === 0.0){}

it returns me false insted of true. How can I resolve it?

Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96

1 Answers1

3

That is a common problem when comparing float values. Float values are almost never exactly like you write them. So your -0.0 is in reality more like -0.000001 or something.

If you want to compare float values, you have to deal with a certain amount of error, like this:

if(Math.abs(value1 - value2) < 0.001) {

So you basically treat all floats whose difference is smaller than 0.001 as equal.

Lars Ebert
  • 3,487
  • 2
  • 24
  • 46