Why Nan Nan == 0?

Asked

Viewed 1,273 times

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?

  • 2

    And why NaN == NaN gives false?

  • And why NaN ^ 2 == 2?

  • 5

    Where did we come from? Where are we going?

  • My goodness! kkkkkkkkkkkk js has some bizarre ones too huh :p

  • All question of checking how it is compared in binary that it is possible to verify why of this!

  • 1

    @diegofm http://jswtf.tumblr.com/

Show 1 more comment

5 answers

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

Source

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:

  • Operands are converted to 32-bit integers and expressed as a series of bits (zeros and ones). Numbers with representation greater than 32 bits will have their bits truncated. For example, the following integer has binary representation greater than 32 bits will be converted into a 32-bit integer.
  • Before: 11100110111110100000000000000110000000000001
  • Then: 10100000000000000110000000000001
  • Each bit of the first operand is paired with the corresponding bit of the second operand: first bit with first bit, second bit with second bit and so on.
  • The operator is applied to each pair of bits and the result is built bit by bit.

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

Source

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:

  • For each bit in the same position and with the same value, it results: 0;
  • For each bit in the same position and with different value, it results: 1.

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:

  • 1 = 0001;
  • 2 = 0010;

Applying the XOR logic:

  • 1st post 1 = 0 and 1º pos 2 = 0 = 0;
  • 2nd post 1 = 0 and 2PP 2 = 0 = 0;
  • 3rd post 1 = 0 and 3PP 2 = 1 = 1;
  • 4º pos 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

You are not signed in. Login or sign up in order to post.