Open timotheecour opened 3 years ago
doAssert $inf == "inf"
($Inf is already "inf", and "inf" is commonly used in other languages, not "Inf")abs(-0.0)
returns -0.0 instead of 0.0
(can be seen with signbit for eg)proc c_copysign(x, y: float64): float64 {.importc: "copysign", header: "<math.h>".}
should be:
proc c_copysign(x, y: cdouble): cdouble {.importc: "copysign", header: "<math.h>".}
refs https://github.com/nim-lang/Nim/pull/16406/files#r549490523
proc c_copysign(x, y: cdouble): cdouble {.importc: "copysign", header: "math.h".}
should be:
proc c_copysign(x, y: cdouble): cdouble {.importc: "copysign", header: "<math.h>".}
abs(SomeNumber)
more efficiently (at least on some platforms) using bit manipulation (seems like simply amounts to setting a bit)
maybe fabs is as efficient? refs https://en.cppreference.com/w/c/numeric/math/fabs
(but using fabs wouldn't allow inlining unless using --passc:-flto ?)eg: something along those lines (but adapted for the simpler case of abs)
## D20201228T203049
float copysignf(float x, float y)
{
union {float f; uint32_t i;} ux={x}, uy={y};
ux.i &= 0x7fffffff;
ux.i |= uy.i & 0x80000000;
return ux.f;
}
classify
doesn't work with --passc:-ffast-math => https://github.com/nim-lang/Nim/pull/23211template main
so it tests in VM + RT, and also test in js,c,cpp, using the usual disableVM (etc) as neededsignbit
=> https://github.com/nim-lang/Nim/pull/16592nextAfter
(it's neither in math nor in fenv right now)there are other related functions, eg nexttoward
, nextup
, nextdown
+ their variants but nextafter
seems like the most useful / versatile
refs https://www.gnu.org/software/libc/manual/html_node/FP-Bit-Twiddling.html
doAssert copySign(-1.0, -NaN) == 1.0
gives different results in VM vs RT
refs: https://github.com/timotheecour/Nim/pull/496#discussion_r549562123
refs: https://github.com/timotheecour/Nim/issues/499Object.is
for math.copySign and $float (for -0.0),
refs: https://github.com/nim-lang/Nim/pull/16505#discussion_r549826330
refs: https://github.com/nim-lang/Nim/pull/16406#discussion_r549831967refs: https://github.com/nim-lang/Nim/pull/16179#issuecomment-736787349 refs: https://github.com/timotheecour/Nim/issues/501#issuecomment-752768194
isNaN
to reflect reality:Returns whether x is a NaN, more efficiently than via classify(x) == fcNan. Works even with: --passc:-ffast-math.
=> (preferably) make this actually true, or specify where this holds true or say simply this depends on compilers/arthitectures
toBits
and fromBits
to math by xflywind · Pull Request #16403 · nim-lang/Nim()
outer parens /cc @xflywind (ok, i agree)func mod(x, y: float32): float32 {.importcpp: "# % #".}
func mod(x, y: float64): float64 {.importcpp: "# % #".}
uabs
or absUnsigned
:
proc absUnsigned*(a: SomeSigned): auto =
type T = toUnsigned(type(a))
if a >= 0: cast[T](a)
else: cast[T](not(a)) + 1
avoids overflow errors
^
(int pow) should also accepty<0
, in which case it uses 1/x ^ (-y)