add math.isNaN (#16179)

* add math.isNaN
* isNaN now works with --passc:-ffast-math; tests
* Update lib/pure/math.nim

Co-authored-by: Andreas Rumpf <rumpf_a@web.de>
This commit is contained in:
Timothee Cour 2020-12-11 02:01:43 -08:00 • committed by GitHub
commit 0b73106ccf
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 5 deletions

View file

@ -58,6 +58,10 @@ import std/private/since
import bitops, fenv
when defined(c) or defined(cpp):
proc c_isnan(x: float): bool {.importc: "isnan", header: "<math.h>".}
# a generic like `x: SomeFloat` might work too if this is implemented via a C macro.
func binom*(n, k: int): int =
## Computes the `binomial coefficient <https://en.wikipedia.org/wiki/Binomial_coefficient>`_.
runnableExamples:
@ -133,10 +137,27 @@ type
fcInf, ## value is positive infinity
fcNegInf ## value is negative infinity
func isNaN*(x: SomeFloat): bool {.inline, since: (1,5,1).} =
## Returns whether `x` is a `NaN`, more efficiently than via `classify(x) == fcNan`.
## Works even with: `--passc:-ffast-math`.
runnableExamples:
doAssert NaN.isNaN
doAssert not Inf.isNaN
doAssert isNaN(Inf - Inf)
doAssert not isNan(3.1415926)
doAssert not isNan(0'f32)
template fn: untyped = result = x != x
when nimvm: fn()
else:
when defined(js): fn()
else: result = c_isnan(x)
func classify*(x: float): FloatClass =
## Classifies a floating point value.
##
## Returns ``x``'s class as specified by `FloatClass enum<#FloatClass>`_.
## Doesn't work with: `--passc:-ffast-math`.
runnableExamples:
doAssert classify(0.3) == fcNormal
doAssert classify(0.0) == fcZero