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?

4
  • possible duplicate of Are +0 and -0 the same? Commented Feb 2, 2015 at 15:15
  • please post your entire code, i mean the approximation method Commented Feb 2, 2015 at 15:15
  • The problem is that comparison is inside an iteraction and I need sign of number except for zero @FrédéricHamidi Commented Feb 2, 2015 at 15:15
  • If you Google JavaScript negative zero you'll find lots of posts. Commented Feb 2, 2015 at 15:15

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.