[JS] NaN(Not a Number)가 number 타입이라고?

Hyun Jin·2022년 12월 1일
0

JavaScript - 기타

목록 보기
1/2
post-thumbnail

typeof NaN = number;


JS typeof 연산자 강의를 듣다가 슥 나온 부분에 잘못 봤나 싶어서 더 찾아봤다.

왜 number 가 아니라는 뜻의 NaN 이 number type 인거지??

console.log(typeof NaN); // number

1.ECMAScript® Language Specification

4.3.23NaN

"number value that is a IEEE 754 “Not-a-Number” value"

NaN은 number 타입이라고만 정의되어 있다.

https://262.ecma-international.org/5.1/#sec-4.3.20


2. MDN

"The global NaN property is a value representing Not-A-Number."

MDN 조차 설명이 부족하다!

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/NaN


3. Stack overflow

12년 전에 같은 질문이 올라왔다.

Why does typeof NaN return 'number'?

Answer 1 :
A comparison with a NaN always returns an unordered result even when comparing with itself. The comparison predicates are either signaling or non-signaling, the signaling versions signal an invalid exception for such comparisons. The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN.

Answer 2 :
NaN just means the specific value cannot be represented within the limitations of the numeric type (although that could be said for all numbers that have to be rounded to fit, but NaN is a special case).

-> NaN은 numeric type 으로 분류되나, numeric type 의 한계 내에서 표현할 수 없는 numeric type을 표현한다.
-> 자기자신과 비교해도 console.log(NaN == NaN); false 로 나온다!
-> 따라서 isNaN 을 사용할 때에 혼선이 생길 수 있다.

  • MDN 에서는 isNaN 사용 시 혼선을 피하기 위한 방법으로 다음 두가지를 추천하고 있다.*
    1. Number.isNaN 사용
    2. (x != x) 으로 변수 x 가 NaN 인지 테스트

https://stackoverflow.com/questions/2801601/why-does-typeof-nan-return-number
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/isNaN *


4. Wikipedia

위키피디아에는 제일 상세한 설명이 나와있다.

NaN

In computing, NaN (/næn/), standing for Not a Number, is a member of a numeric data type that can be interpreted as a value that is undefined or unrepresentable, especially in floating-point arithmetic. Systematic use of NaNs was introduced by the IEEE 754 floating-point standard in 1985, along with the representation of other non-finite quantities such as infinities.

In mathematics, zero divided by zero is undefined[a] and is therefore represented by NaN in computing systems. The square root of a negative number is not a real number, and is therefore also represented by NaN in compliant computing systems. NaNs may also be used to represent missing values in computations.[1][2]

Two separate kinds of NaNs are provided, termed quiet NaNs and signaling NaNs. Quiet NaNs are used to propagate errors resulting from invalid operations or values. Signaling NaNs can support advanced features such as mixing numerical and symbolic computation or other extensions to basic floating-point arithmetic.

-> numeric type 에는 포함되지만 number 로 정의할 수 없는 numeric type 의 값이 NaN 으로 출력되는 듯 하다.

자신과 비교해도 false 를 return하는 NaN 의 특징을 어떻게 이용할 수 있을 것 같은데 아직은 모르겠고, 공부하다 보면 써먹을 곳을 알 수 있지 않을까?

https://en.wikipedia.org/wiki/NaN


추가 참고 사이트

NaN and typeof
An in-depth look at the NaN property, and why it is considered a number type

https://javascriptrefined.io/nan-and-typeof-36cd6e2a4e43

profile
새싹 프론트엔드 개발자

0개의 댓글