0

I have the following code for checking whether a number passed to the function is float or not.

function isFloat(n){ return (n % 1 === 0) ? false : true ; } isFloat(4.000000000000000002);

I am passing value 4.000000000000000002 the function is returning 0. Can you help me figure out what is missing ?

Vishwa
  • 134
  • 3
  • 3
  • 14
  • I think this number is out of range for javascript. – somethinghere Oct 22 '14 at 09:44
  • @somethinghere thanks for the reply. Do you know how to determine whether a number is in range in javascript ? – Vishwa Oct 22 '14 at 09:46
  • I think that's because of the precision : http://stackoverflow.com/questions/11695618/dealing-with-float-precision-in-javascript – Martin Oct 22 '14 at 09:50
  • Check this out: http://jsperf.com/big-integer-library-test#title-3 – Johan Karlsson Oct 22 '14 at 09:54
  • @Vishwa Martins got the right end of the stick. However, after taking a closer look at your code you should change things up a bit to make more sense (though the range is still a problem): `function isFloat(n){ return (n % 1 === 0) ? false : true ; }` -> if you return 1 when the number has no remainder after division by 1, your number is actually an integer, not a float. Returning `1` implies `true`, so if your `isFloat` returns `true` when it's an int, its a bit confusing. Just saying :) – somethinghere Oct 22 '14 at 10:14
  • @somethinghere yeah that's my bad, anyway i updated the question. – Vishwa Oct 22 '14 at 11:48

0 Answers0