33
Check out these tests I did on the browser console:
NaN / NaN
NaN
NaN * NaN
NaN
typeof NaN
"number"
NaN ** NaN
NaN
NaN ^ NaN
0
Because exactly that happens?
33
Check out these tests I did on the browser console:
NaN / NaN
NaN
NaN * NaN
NaN
typeof NaN
"number"
NaN ** NaN
NaN
NaN ^ NaN
0
Because exactly that happens?
30
According to the MDN:
Nan - The global property Nan is a special value meaning Not-A-Number (not a number).
According to the specifications Ecmascript 5 (index 9.5) and ECMA 262 (index 7.1.5), when we make a bit-by-bit operation all 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));
18
Concept:
What is Nan
Nan is a property of the global object. Nan’s initial value is Not-A-Number, the same value of Number.NaN
.
In browsers, Nan is a read only and not configurable property. Even when this is not the case, avoid overwriting it.
Testing a Nan value
Equality operators (== and ==) cannot be used to test an Nan value. Instead, use Number.isNaN()
or isNaN()
.
NaN === NaN; // falso
Number.NaN === NaN; // falso
isNaN(NaN); // verdadeiro
isNaN(Number.NaN); // verdadeiro
Port XOR
Or exclusive or exclusive disjunction, known generally for XOR or by EXOR (also XOU or EOU), 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. Can be synthesized as a difference detector between two logical operands. Source
Javascript
Bit-by-bit operators
XOR (a b)
Returns 0 for each position where the bits of the corresponding position are the same. [Returns a 1 for each position where the bits of the corresponding position are different.]
Conceptually, logical bit-to-bit operators work as follows:
- Before: 11100110111110100000000000000110000000000001
- Then: 10100000000000000110000000000001
For example, the binary representation of nine is 1001 and the binary representation of fifteen is 1111. Thus, when bit-by-bit operators are applied to these values, the results are as follows::
Example:
Expression: 15 9
Result: 6
Comparison in torque: 1111 1001
Result in binary: 0110
REPLY
Since Nan is Not-A-Number, it is not possible to perform arithmetic operations, as it will always result in Nan.
NaN / NaN
NaN
NaN * NaN
NaN
NaN ** NaN
NaN
In bit-by-bit operators, for example, XOR will compare Nan to 0.
NaN ^ NaN // 0 ^ 0
0
And when to compare NaN ^ 2
will be comparing 00B with 10B (B = Binary), returning 10B converting to decimal is 2
12
In the case of arithmetic operators, Nan is not computable 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 bit-by-bit operator, you can test the calculation based on:
NaN ^ NaN = 0
Supposing:
NaN = 0
then:
0 ^ 0 = 0
If you test:
1 ^ 2 = 3
, because?
Let’s see through the binary representation of 1 and 2:
Applying the XOR logic:
0
and 1º pos 2 = 0
= 0
; 0
and 2PP 2 = 0
= 0
; 0
and 3PP 2 = 1
= 1
; 1
and 4º pos 2 = 0
= 1
;soon: 0001 ^ 0010 = 0011
, where 0011
is the binary representation of the number 3.
7
The global property Nan is a special value meaning Not-A-Number (not a number).
Unlike all other possible values in Javascript, it is not possible to rely on equality operators (== and ===) to determine whether the value is Nan or not, because both, Nan == Nan and Nan === Nan, will have the return value: false. Hence the need for the isNAN 'isNAN function()'.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN
3
NaN ^ NaN = 0
Because exactly that happens?
By laziness, forgetfulness, bug, chance!
NaN ^ NaN
should give NaN
The operation NaN ^ NaN
does not make much sense -- bit-to-bit operations with
"uncontrolled" values were not predicted by whoever made this language implementation, causing the default implementation
were used.
Default implementation can be one of two hypotheses:
H1) coercion comparison for integers:
int(NaN) ^ int(NaN) => 0 ^ 0 => 0
H2) xor Bib a bit direct: In the floating comma number representation, a conventional representation for Nan, associated with a set of bits, was chosen. Doing XOR bit by bit of two equal groups gives 0
Browser other questions tagged javascript
You are not signed in. Login or sign up in order to post.
And why
NaN == NaN
givesfalse
?– Oralista de Sistemas
And why
NaN ^ 2 == 2
?– Artur Trapp
Where did we come from? Where are we going?
– Artur Trapp
My goodness! kkkkkkkkkkkk js has some bizarre ones too huh :p
– user28595
All question of checking how it is compared in binary that it is possible to verify why of this!
– Don't Panic
@diegofm http://jswtf.tumblr.com/
– Oralista de Sistemas