Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: Comparing two float values

I have this JavaScript function:

Contrl.prototype.EvaluateStatement = function(acVal, cfVal) {

    var cv = parseFloat(cfVal).toFixed(2);
    var av = parseFloat(acVal).toFixed(2);

   if( av < cv) // do some thing
}

When i compare float numbers av=7.00 and cv=12.00 the result of 7.00<12.00 is false!

Any ideas why?

like image 242
Harold Sota Avatar asked Jul 27 '10 12:07

Harold Sota


People also ask

How do you compare two float values?

To compare two floating point values, we have to consider the precision in to the comparison. For example, if two numbers are 3.1428 and 3.1415, then they are same up to the precision 0.01, but after that, like 0.001 they are not same.

Can we compare two float values in Java?

The compare() method of Float Class is a built-in method in Java that compares the two specified float values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call.

Can we compare double with float?

Float has to round more than double, because it is smaller, so 1.1 rounded to the nearest valid Float is different to 1.1 rounded to the nearest valud Double.

How do you compare decimals in JavaScript?

To compare decimal numbers with JavaScript, we can round both numbers and then compare them. to multiply a and b by 100 and call Math. round with the returned number to get rid of the decimals. Then we use > to compare them.


5 Answers

toFixed returns a string, and you are comparing the two resulting strings. Lexically, the 1 in 12 comes before the 7 so 12 < 7.

I guess you want to compare something like:

(Math.round(parseFloat(acVal)*100)/100)

which rounds to two decimals

like image 80
second Avatar answered Oct 04 '22 12:10

second


Compare float numbers with precision:

var precision = 0.001;

if (Math.abs(n1 - n2) <= precision) {
  // equal
}
else {
  // not equal
}

UPD: Or, if one of the numbers is precise, compare precision with the relative error

var absoluteError = (Math.abs(nApprox - nExact)),
  relativeError = absoluteError / nExact;

return (relativeError <= precision);
like image 28
Edward Avatar answered Oct 03 '22 12:10

Edward


The Math.fround() function returns the nearest 32-bit single precision float representation of a Number.

And therefore is one of the best choices to compare 2 floats.

if (Math.fround(1.5) < Math.fround(1.6)) {
    console.log('yes')
} else {
    console.log('no')
}

>>> yes

// More examples:
console.log(Math.fround(0.9) < Math.fround(1));                            >>> true
console.log(Math.fround(1.5) < Math.fround(1.6));                          >>> true
console.log(Math.fround(0.005) < Math.fround(0.00006));                    >>> false
console.log(Math.fround(0.00000000009) < Math.fround(0.0000000000000009)); >>> false
like image 31
George C. Avatar answered Oct 04 '22 12:10

George C.


Comparing floats using short notation, also accepts floats as strings and integers:

var floatOne = 2, floatTwo = '1.456';

Math.floor(floatOne*100) > Math.floor(floatTwo*100) 

(!) Note: Comparison happens using integers. What actually happens behind the scenes: 200 > 145

Extend 100 with zero's for more decimal precision. For example use 1000 for 3 decimals precision.

Test:

var floatOne = 2, floatTwo = '1.456';
console.log(Math.floor(floatOne*100), '>', Math.floor(floatTwo*100), '=', Math.floor(floatOne*100) > Math.floor(floatTwo*100));
like image 41
Slava Avatar answered Oct 04 '22 12:10

Slava


Comparing of float values is tricky due to long "post dot" tail of the float value stored in the memory. The simplest (and in fact the best) way is: to multiply values, for reducing known amount of post dot digits to zero, and then round the value (to rid of the tail).

Obviously both compared values must be multiplied by the same rate.

F.i.: 1,234 * 1000 gives 1234 - which can be compared very easily. 5,67 can be multiplied by 100, as for reducing the float comparing problem in general, but then it couldn't be compared to the first value (1,234 vel 1234). So in this example it need to be multiplied by 1000.

Then the comparition code could look like (in meta code):

  var v1 = 1.234;
  var v2 = 5.67;

  if (Math.round(v1*1000) < Math.round(v2*1000)) ....
like image 24
Marek Wysmułek Avatar answered Oct 04 '22 12:10

Marek Wysmułek