Look at these tests I made on the browser console:
NaN / NaN
NaN
NaN * NaN
NaN
typeof NaN
"number"
NaN ** NaN
NaN
NaN ^ NaN
0
Why exactly does this happen?
Look at these tests I made on the browser console:
NaN / NaN
NaN
NaN * NaN
NaN
typeof NaN
"number"
NaN ** NaN
NaN
NaN ^ NaN
0
Why exactly does this happen?
According to MDN :
NaN - The global NaN property is a special value that means Not-A-Number (not a number).
According to the specifications ECMAScript 5 (index 9.5) and ECMA 262 (index 7.1.5), when we do a bitwise operation all the elements involved are converted to 32-bit integers ( ToInt32 ). If the value is one of the global properties NaN or infinity the value is converted to 0.
2 . If number is NaN , +0, -0, + ∞, or -∞, return +0.
Knowing that the ^
operator represents the logical operation XOR
...
NaN ^ NaN
is equivalent to 0 XOR 0
console.log("NaN ^ NaN: " + (NaN ^ NaN));
console.log("0 ^ 0: " + (0 ^ 0));
console.log("Infinity ^ 0: " + (Infinity ^ 0));
Concept:
What is NaN
NaN is a property of the global object.
The initial value of NaN is Not-A-Number - the same value as Number.NaN. In modern browsers, NaN is a read-only and non-configurable property. Even when this is not the case, avoid overwriting it.
Testing a NaN value
Equality operators (== and ===) can not be used to test a NaN value. Instead, use Number.isNaN()
or isNaN()
.
NaN === NaN; // falso
Number.NaN === NaN; // falso
isNaN(NaN); // verdadeiro
isNaN(Number.NaN); // verdadeiro
XOR Port
Exclusive or exclusive disjunction , generally known as XOR or EXOR , is a logical operation between two operands that results in a true logical value if and only if exactly one of the operands has true value. It can be synthesized as a difference detector between two logical operands. FontJavaScript
Bitwise Operators
XOR (a ^ b)
Returns a 0 for each position where the bits of the corresponding position are the same. [Returns a 1 for each position where the corresponding position bits are different.]
Conceptually, logical bitwise operators work as follows:
- Before: 11100110111110100000000000000110000000000001
- Next: 10100000000000000110000000000001
For example, the binary representation of nine is 1001 and the binary representation of fifteen is 1111. Thus, when bitwise operators are applied to these values, the results are as follows:
Example:
Expression: 15 ^ 9
Result: 6
Comparison in binary: 1111 ^ 1001
Binary result: 0110
ANSWER
Because NaN is Not-A-Number, it is not possible to perform arithmetic operations because it will always result in NaN.
NaN / NaN
NaN
NaN * NaN
NaN
NaN ** NaN
NaN
In bitwise operators, as an example, XOR will compare NaN by always being 0.
NaN ^ NaN // 0 ^ 0
0
And when comparing NaN ^ 2
will be comparing 00B with 10B (B = Binary), returning 10B which converting to decimal is 2
In the case of arithmetic operators, NaN is not calculable and any value with one of these operators used in partnership with NaN will return NaN. Since NaN is not a number.
NaN + 1 = NaN
NaN / 2 = NaN
In the case of XOR, which is a bitwise operator, you can test the calculation based on:
NaN ^ NaN = 0
Assuming:
NaN = 0
then:
0 ^ 0 = 0
If you test:
1 ^ 2 = 3
, why?
Let's look at the binary representation of 1 and 2:
Applying XOR logic:
0
and 1st pos 2 = 0
= 0
; 0
and 2nd pos 2 = 0
= 0
; 0
and 3rd pos 2 = 1
= 1
; 1
and 4th pos 2 = 0
= 1
; logo: 0001 ^ 0010 = 0011
, where 0011
is the binary representation of number 3.
The global NaN property is a special value that means Not-A-Number (not a number).
Unlike all other possibilities of values in JavaScript, it is not possible to trust equality operators (== and ===) to determine whether the value is NaN or not, because both NaN == NaN and NaN === NaN, returns: false. Hence the need for the isNAN 'isNAN ()' function.
NaN ^ NaN = 0
Why exactly does this happen?
For laziness, oblivion, bug, chance!
NaN ^ NaN
should give NaN
The NaN ^ NaN
operation does not make much sense - bitwise operations with
"uncontrolled" values were not predicted by those who did this language implementation, making the default implementation
was used.
The default implementation can be one of two assumptions:
h1) coercion comparison for integers:
int(NaN) ^ int(NaN) => 0 ^ 0 => 0
h2) xor bib direct bit: In the representation of floating point numbers, a conventionally represented representation for NaN, associated to a set of bits, was chosen. By doing bitwise XOR of two equal groups gives 0