2

This output should be true.but it give false always.

Number.isInteger(parseFloat('134965.83') / parseFloat('0.01'))
Shamil
  • 727
  • 1
  • 9
  • 17

3 Answers3

2

Floating point arithmetic in Javascript is broken and in general as well.

It has nothing to do with division, it will return false if you don't do division since you are checking float value.

Number.isInteger(parseFloat('134965.83') / parseFloat('0.01')) translates to Number.isInteger(13496582.999999998) -> false

Check these examples.

Number.isInteger(parseFloat('134965.83') )// outputs false without division

As per the spec

If Type(argument) is not Number, return false.

If floor(abs(argument)) ≠ abs(argument), return false.

Community
  • 1
  • 1
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • Expected value after division is `13496583`..That is why OP is expecting Integer.. – Rayon Mar 22 '16 at 07:06
  • @RayonDabre only if float point arithmetic worked that way :) try this `parseFloat(1111.1)/parseFloat(0.1)` – gurvinder372 Mar 22 '16 at 07:07
  • Expected value after division is 13496583..That is why OP is expecting Integer.. Exactly what you said..i am expecting the output as integer if it exactly divides. – Shamil Mar 22 '16 at 07:39
  • @shamil In that case you should give `Math.round(Number('134965.83') / Number('0.01'))` – gurvinder372 Mar 22 '16 at 08:16
  • @gurvinder372 very thanks. if(Number.isInteger(parseFloat('134965.80') / parseFloat('0.01'))) Any alternative for the above one.. which is if condition. the suggestion that you given do not fit over here. – Shamil Mar 22 '16 at 08:28
  • @shamil this is precisely the point. Please read the first line of my answer. Floating point arithmetic may give some strange results. Try `11.5 - 11.01` on your chrome – gurvinder372 Mar 22 '16 at 08:30
1

This happens because the outcome of the division is not an integer but something like 13496582.999999998.

Some floating numbers require a very small precision that is limited by the data type used. For example, the number 1/3 can never be expressed entirely as 0.333333333333 because there is a limitation to the data type size. Therefore there will always be a tiny rounding error involved in floating operations.

Edit: In response to the comment asking for a recommendation on how to deal eith this, actually there are several possibilities. It depends on the context and on accuracy required.

In short, to overcome this use a very small constant Number.EPSILON (see also this) and use it in comparisons. Disclaimer: this is just a quick example, read extensively the implications on the links provided.

var myIsInteger = function(n) {
  return (n % 1) > Number.EPSILON
};

We effectively check that the residual of the division with 1 is within the constant.

Community
  • 1
  • 1
Wtower
  • 18,848
  • 11
  • 103
  • 80
  • yes. please let me know how to overcome this.. i expecting the behaviour as like 134965.82/0.01 for 134965.83/0.01 – Shamil Mar 22 '16 at 07:43
  • Provided recommendation. – Wtower Mar 22 '16 at 08:28
  • very Thanks if(Number.isInteger(parseFloat('134965.83') / parseFloat('0.01')){ console.log("true"); } else{ console.log("false"); //do Nothing } how to alternate your suggestion with above one. – Shamil Mar 22 '16 at 08:59
  • Welcome. `if (myIsInteger(parseFloat('134965.83') / parseFloat('0.01'))) console.log('bingo');` – Wtower Mar 22 '16 at 09:00
  • function load(){if (myIsInteger(parseFloat('134965.82') / parseFloat('0.01'))) { console.log("true"); } else{ console.log("false"); }} load(); Answer : false – Shamil Mar 22 '16 at 09:55
  • `((parseFloat('134965.83') / parseFloat('0.01')) % 1) > Number.EPSILON` returns `true`, at least in my console. – Wtower Mar 22 '16 at 10:04
  • ((parseFloat('134965.83') / parseFloat('0.01')) % 1) > Number.EPSILON true ((parseFloat('134965.82') / parseFloat('0.01')) % 1) > Number.EPSILON false please conclude me on this.. both should have same response right.. – Shamil Mar 22 '16 at 10:23
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/107011/discussion-between-wtower-and-shamil). – Wtower Mar 22 '16 at 10:25
0

parseFloat('134965.83') / parseFloat('0.01') = 13496582.999999998

And when Number.isInteger(13496582.999999998) will always return false

Coder John
  • 776
  • 4
  • 7