0

I looked at this answer and was not satisfied:

How to check if a JavaScript number is a real, valid number?

I thought it would be faster and more concise to do:

Pub.isRealNum = function (check) {
    return ( check === +check ) && (check !== Infinity) && (check !== -Infinity);
};

Are there any cases I'm missing?

It works for

1 ( true )
1.1 ( true )
0 ( true )
Infinity ( false )
NaN ( false )
"foo" ( false )
function(){} ( false )

Does underscore have an equivalent?

I looked briefly here at the API but did not see any thing:

http://underscorejs.org/

Community
  • 1
  • 1
  • Does this work when you input strings or other objects? – Samie Bencherif Sep 24 '16 at 19:29
  • I'd like to mention that your usage of _real_ is a bit confusing - I thought you wanted to detect numbers that are only real, not just to see if it's a number. – VLAZ Sep 24 '16 at 19:30
  • @vlaz I thought the same, but you would just be passing in NaN if you tried to give it an imaginary number like Math.sqrt(-1). – Jecoms Sep 24 '16 at 19:31
  • @Samy - yes it does. –  Sep 24 '16 at 19:32
  • @Jecoms I was thinking more along the lines of separating reals from integers and/or rational numbers or something. – VLAZ Sep 24 '16 at 19:35
  • @vlaz Real numbers include both rational and irrational numbers. – Jecoms Sep 24 '16 at 19:37
  • Use the built in methods `isNaN` and `isFinite`. Your requirements for a "real" number value are not defined so this question is too vague to answer correctly. – Andy Ray Sep 24 '16 at 19:38
  • A computer can not represent an irrational number. I think I should have called the method `isRationalNumber` –  Sep 24 '16 at 19:40
  • @Jecoms and integers. But that's not the point, I can very well see a use for figuring if a number is only in the real set, but not in any of the others. – VLAZ Sep 24 '16 at 19:40
  • @C Aaker but you would want Math.PI to return true, right? – Jecoms Sep 24 '16 at 19:42
  • @Jecoms - It is a rational number as it an estimate of PI, not actual PI. –  Sep 24 '16 at 19:44
  • @C Aaker You're right. I suppose I've never tried to work with irrational numbers in code before. – Jecoms Sep 24 '16 at 19:46
  • I have updated the question to say ... isRational, but perhaps it should be isFinite and discarded as it is part of the API now. –  Sep 24 '16 at 19:48

1 Answers1

4

Just use Number.isFinite.

  1. If Type(number) is not Number, return false.
  2. If number is NaN, +∞, or −∞, return false.
  3. Otherwise, return true.

assert('Number.isFinite(1)', true);
assert('Number.isFinite(1.1)', true);
assert('Number.isFinite(+0)', true);
assert('Number.isFinite(-0)', true);
assert('Number.isFinite(-1.1)', true);
assert('Number.isFinite(+Infinity)', false);
assert('Number.isFinite(-Infinity)', false);
assert('Number.isFinite(NaN)', false);
assert('Number.isFinite(null)', false);
assert('Number.isFinite(undefined)', false);
assert('Number.isFinite(true)', false);
assert('Number.isFinite(false)', false);
assert('Number.isFinite("123")', false);
assert('Number.isFinite("foo")', false);
assert('Number.isFinite(new Number(1))', false);
assert('Number.isFinite([])', false);
assert('Number.isFinite({})', false);
assert('Number.isFinite(function(){})', false);
function assert(code, expected) {
  var result = eval(code);
  console.log('Test ' + (result===expected ? 'PASS' : 'FAIL') + ': ', code, ' -> ', result);
}
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • or, `Number.isSafeInteger` if you're interested in `int` only. – Hitmands Sep 24 '16 at 19:37
  • This seems like the best solution, but semantically, I think it can be vague since irrational numbers are not really "finite" but they are Real numbers. – Jecoms Sep 24 '16 at 19:40
  • Wow, what was the purpose of http://stackoverflow.com/questions/8525899/how-to-check-if-a-javascript-number-is-a-real-valid-number –  Sep 24 '16 at 19:43
  • @CAaker it was posted in 2011 and I don't think `Number.isFinite` was around then – VLAZ Sep 24 '16 at 19:46
  • 1
    @Jecoms Irrationals are finite numbers. Because they are not infinity. Their decimal representation is not finite, but that's a different thing. You can even express certain irrationals in a finite representation in certain basis. – Oriol Sep 24 '16 at 19:46
  • @Oriol by this function's definition, sure. – Jecoms Sep 24 '16 at 19:47
  • 1
    @Jecoms No, I mean they are finite in math. – Oriol Sep 24 '16 at 19:48
  • 2
    @CAaker Here is a finite representation of `π` using base `π`: `10`. It does not go on forever. – Oriol Sep 24 '16 at 19:53
  • 3
    Going on forever has nothing to do with being infinite. E.g. 1/3 "goes on forever" when written decimal, but is neither irrational nor infinite. This debate is off topic. – ASDFGerte Sep 24 '16 at 19:53
  • The representation of PI in decimal goes on forever and never approaches a single value. –  Sep 24 '16 at 20:23
  • @CAaker `π` approaches `π`, just like `2` approaches `2`. Numbers are abstract things, their properties do not depend on their representations. – Oriol Sep 24 '16 at 20:24
  • This is ES6, where can I get an overview of browser that support this. –  Oct 02 '16 at 15:12
  • @CAaker Use `typeof value === "number" && isFinite(value)`. Will work even with ES1. – Oriol Oct 02 '16 at 20:33