diff --git a/changelog.md b/changelog.md index d62e96bbed78b..378fd81d2d791 100644 --- a/changelog.md +++ b/changelog.md @@ -52,6 +52,8 @@ - `writeStackTrace` is available in JS backend now. +- Added `math.isNaN`. + ## Language changes - `nimscript` now handles `except Exception as e`. diff --git a/lib/pure/math.nim b/lib/pure/math.nim index ccd766f46156a..949d06dbc235b 100644 --- a/lib/pure/math.nim +++ b/lib/pure/math.nim @@ -158,6 +158,14 @@ proc classify*(x: float): FloatClass = return fcSubnormal return fcNormal +proc isNaN*(x: float): bool = + ## Returns whether `x` is a `NaN`, more efficiently than via `classify(x) == fcNan`. + ## Does not work with: `--passc:-ffast-math`. + runnableExamples: + doAssert NaN.isNaN + doAssert not Inf.isNaN + result = x != x + proc almostEqual*[T: SomeFloat](x, y: T; unitsInLastPlace: Natural = 4): bool {. since: (1, 5), inline, noSideEffect.} = ## Checks if two float values are almost equal, using