timotheecour / Nim

Nim is a compiled, garbage-collected systems programming language with a design that focuses on efficiency, expressiveness, and elegance (in that order of priority).
http://nim-lang.org/
Other
2 stars 0 forks source link

misc math #494

Open timotheecour opened 3 years ago

timotheecour commented 3 years ago
timotheecour commented 3 years ago
timotheecour commented 3 years ago

EDIT: => https://github.com/nim-lang/Nim/issues/16494

timotheecour commented 3 years ago

refs https://github.com/nim-lang/Nim/pull/16406/files#r549490523

timotheecour commented 3 years ago
timotheecour commented 3 years ago

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;
}
timotheecour commented 3 years ago
timotheecour commented 3 years ago
timotheecour commented 3 years ago
timotheecour commented 3 years ago
timotheecour commented 3 years ago

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

timotheecour commented 3 years ago
timotheecour commented 3 years ago
timotheecour commented 3 years ago

refs: https://github.com/nim-lang/Nim/pull/16179#issuecomment-736787349 refs: https://github.com/timotheecour/Nim/issues/501#issuecomment-752768194

timotheecour commented 3 years ago

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

timotheecour commented 3 years ago
timotheecour commented 3 years ago
func mod(x, y: float32): float32 {.importcpp: "# % #".}
func mod(x, y: float64): float64 {.importcpp: "# % #".}
timotheecour commented 3 years ago

refs https://github.com/nim-lang/Nim/pull/17106/files#diff-41e0f628f50989d7cd4eecdf69497f294772f292348ebd996c342ec294fe816bR342

avoids overflow errors