Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
theuser47815yCan you explain? I went through each line and they all seem good to me according to the spec.
-
That's how Mafia works.
Nobody would actually use such comparisons in an actual program. If you need to check an arbitrary input for validity, you'd most likely check the type first
typeof myVariable === "number"
and then for a valid value
0 <= myVariable
Number.isNaN(...) is not a general function to abuse for abitrary inputs. NaN means it's not a valid number in the range of numbers. E.g. some bit pattern which is not a valid double-precision floating-point in IEEE 754. -
NaN is a special kind of a number it's like how null is an object.
isNaN(arg) will try to convert the arg to a Number via Number(arg), and then check if it's a NaN via it's special comparison.
?: Number.isNaN, on the other hand, only checks if the argument is NaN, without correcting the type. Which will make it return false unless the arg is NaN.
parseInt(arg) will try to return as many digits as it can until it reaches something not digit.
Try parseInt on "d123" and "123" and see the difference. It's like regexp /^\d+/ . [1] -
theuser47815y@Demolishun JS is compliant to IEE 754 which allow distinct NaNs. Some maffs guy can probably clarify further how they work.
-
Since NaN is an exceptional value, you can't just compare it with another thing.
That's why isNaN function(s) exist.
a NaN compared to a number (which includes the NaN value) will return false no matter what.
But to be honest, I have never noticed that there was parseInt and parseFloat functions on the global, I always went to the Number object's methods.[2][END]
[2] -
NaN is more like an exception object than a value. No two NaNs are the same, and they cannot be compared. It's confusing at first, but it has a straightforward and easy-to-google answer
-
@melezorus34 So maybe the comparison needs to check the type of NaN against type of returned value?
-
Extra:
?:How does one check if an argument is NaN without isNaN functions?
Easy, first of all, convert the argument to a number via Number() and save it somewhere.
Then, check if the number is not equal to 0 (I use type strict eq as a habit)
If it's not, return ! convertedArgument, NaN is a falsy value and ! operator will make it true.
If it's equal, return false as usual.
A shorter function can be written like this: @highlight
argument=>Number(argument)!=0 && !Number(argument); -
@Demolishun nope, it's a bit more complex. I just explained here without realizing you sent something btw.
As I said before, typeof NaN will give "number" too, so normal comparisons wouldn't cut. -
theuser47815y@melezorus34 I believe you are using the same parse functions, I cannot find any in the spec for Number so it might be resolving them in the prototype chain.
-
¯\_(ツ)_/¯ at least node is saying both functions are identical (===) and hasOwnProperty is giving true on both sides (Number, globalThis)
-
@Demolishun NaN is not comparable to NaN and it wouldn't make much sense anyway.
Let's say you want to compare two function parameters, a and b, which you expect to be numbers:
function someNums(a, b) {
if ( a === b ) { return 0; }
return a + b;
}
Since you expect both to be numbers, you should actually make sure of it, which is good practice:
function someNums(a, b) {
if (
typeof a !== 'number' || Number.isNaN(a)
&& typeof b !== 'number' || Number.isNaN(b)
) {
throw new Error( 'computer says "no"' );
}
if ( a === b ) { return 0; }
return a + b;
}
You don't need to do it everywhere but it helps a lot to find bugs quicker and the almighty V8 will optimize it anyway. -
I just read that you can check for NaN by comparing a variable to itself and it always returns false:
(function (){var x=NaN; return x===x;})()
If x is set to anything else it will return true.
This is a really interesting topic! -
theuser47815y@melezorus34 Looks like you were right, but according to MDN, Number's parse functions are not IE compatible.
-
-
Root797785yThere are many paths to creating NaN, such as imaginary numbers like 3+2i: `3 + (-4)**(1/2)`, undefined real-math values like `Infinity * 0`, or undefined real-javascript values like `Number(undefined)`
because of this, the hypothetical "value" of each NaN would differ (which we do not and cannot store), so all comparisons therefore must fail. All NaNs are created unequal.
It's easy to demonstrate: `NaN + 2` returns `NaN` -- and these are obviously different values! -
@Root But Infinity === Infinity is true. There are definitely different kinds of Infinity (positive and negative are 2).
-
Root797785y@Demolishun In js, `-Infinity == Infinity` is false, and `Infinity == Infinity` is true. This follows real math pretty accurately.
Infinity in math is a concept, not a value, so it cannot have a magnitude. (2*infinity) is infinity. therefore all infinities (in the same direction!) are equivalent.
The real mind-bending aspects come in when raising it to a power, performing operations on/between two infinities (or better still: infinite series!). As JS doesn't know how to approach these, it resorts to NaN instead of calling you out on your mathematical transgressions. Afterall, if you wanted to do true rigorous math, you'd use something like R or Haskell instead 😅 -
hack64145yWhen comparing a value to NaN I expected it works as comparing undefined values. I see now why there is isNaN function. But something is not clear for me. Why isNaN('') returns false?
-
theuser47815y@hack I assume you meant "". If you provide a non Number argument as the parameter, it is coerced to a Number. So a more explicit alternative to isNaN("") is isNaN(Number("")).
And what does Number("") return? It returns 0. -
hack64145y@Demolishun well, "yolo" is not a NaN object too but isNaN("yolo") returns true.
@theuser I see the problem lies under Number(""). Thanks. -
theuser47815y@hack Still not sure what the problem is though. Even without coercing to 0, an empty string still isn't a NaN.
Related Rants
Its true unless its javascript, then its false. What the fuck is this? Someone got explanation?
rant
js
jabbascript