Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misc math #494

Open
1 task
timotheecour opened this issue Dec 28, 2020 · 17 comments
Open
1 task

misc math #494

timotheecour opened this issue Dec 28, 2020 · 17 comments
Labels

Comments

@timotheecour
Copy link
Owner

  • ^ (int pow) should also accept y<0, in which case it uses 1/x ^ (-y)
@timotheecour
Copy link
Owner Author

  • should we rename Inf to inf so that doAssert $inf == "inf" ($Inf is already "inf", and "inf" is commonly used in other languages, not "Inf")

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 28, 2020

  • abs(-0.0) returns -0.0 instead of 0.0
    (can be seen with signbit for eg)

EDIT: => nim-lang#16494

@timotheecour
Copy link
Owner Author

  • check at least all importc math procs (ideally all importc procs) for correctness wrt cfloat vs float32, cdouble vs float (these aren't necessarily same types); 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

@timotheecour
Copy link
Owner Author

  • check all importc procs to use header angle brackets for stdlib headers, eg:
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>".}

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 28, 2020

  • check whether we can impement 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;
}

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 28, 2020

  • math.nim should use runnableExamples

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 28, 2020

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 28, 2020

  • tmath.nim should wrap all code inside template main so it tests in VM + RT, and also test in js,c,cpp, using the usual disableVM (etc) as needed

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 28, 2020

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 28, 2020

  • add nextAfter (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

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 29, 2020

@timotheecour
Copy link
Owner Author

@timotheecour
Copy link
Owner Author

timotheecour commented Dec 30, 2020

  • add math.isFinite

refs: nim-lang#16179 (comment)
refs: #501 (comment)

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 10, 2021

  • update this wording for 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

@timotheecour
Copy link
Owner Author

@timotheecour
Copy link
Owner Author

timotheecour commented Jan 15, 2021

  • fix this to add () outer parens /cc @xflywind (ok, i agree)
func mod(x, y: float32): float32 {.importcpp: "# % #".}
func mod(x, y: float64): float64 {.importcpp: "# % #".}

@timotheecour
Copy link
Owner Author

  • add to math: 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

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

avoids overflow errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant