RFCs icon indicating copy to clipboard operation
RFCs copied to clipboard

RFC: Nim Math functions

Open jlp765 opened this issue 9 years ago • 5 comments

Stock-take of Math functions

This is a comparison of math functions, with the aim of helping to identify if any functions are missing that may need to be added, etc. (in preparation for Nim V1.0)

Don't assume I correctly identified the Nim math functions (I may have missed some, or are available in other Nimble packages).

Function Nim POSIX old ISO ISO C99 Microsoft Notes
acos Y (arccos) Y Y Y Y
acosh Y (arccosh)\ Y N Y N
asin Y (arcsin) Y Y Y Y
asinh Y (arcsinh) Y N Y N
atan Y (arctan) Y Y Y Y
atan2 Y (arctan2) Y Y Y Y
atanh Y (arctanh) Y N Y N
cbrt Y Y N Y N
ceil Y Y Y Y Y
copysign N Y N Y Y _copysign
cos Y Y Y Y Y
cosh Y Y Y Y Y
erf Y Y N Y N
erfc Y Y N Y N
exp Y Y Y Y Y
exp2 N Y N Y N
expm1 N Y N Y N
fabs Y (abs) Y Y Y Y
fdim N Y N Y N
floor Y Y Y Y Y
fma N Y N Y N
fmax Y (max) Y N Y N
fmin Y (min) Y N Y N
fmod Y (mod) Y Y Y Y
fpclassify Y (classify) Y N Y Y _fpclass
frexp Y Y Y Y Y
hypot Y Y N Y Y _hypot
ilogb N Y N Y N
isfinite N Y N Y N
isgreater N Y N Y N
isgreaterequal N Y N Y N
isinf N Y N Y N
isless N Y N Y N
islessequal N Y N Y N
islessgreater N Y N Y N
isnan N Y N Y Y _isnan
isnormal N Y N Y N
isunordered N Y N Y N
j0 N Y N N Y _j0
j1 N Y N N Y _j1
jn N Y N N Y _jn
ldexp N Y Y Y Y
lgamma Y Y N Y N
llrint Y (round?) Y N Y N
llround N Y N Y N
log Y (ln) Y Y Y Y
log10 log10 Y Y Y Y
log1p Y N Y N
log2 Y Y N Y N
logb N Y N Y Y _logb
lrint Y (round) Y N Y N
lround N Y N Y N
modf Y (mod) Y Y Y Y
nan N (?) Y N Y N
nearbyint N Y N Y N
nextafter N Y N Y Y _nextafter
nexttoward N N N Y N
pow Y Y Y Y Y
remainder N (mod?) Y N Y N
remquo N Y N Y N
rint Y (round) Y N Y N
round N Y N Y N
scalbln N Y N Y N
scalbn N N N Y N
signbit N Y N Y N
sin Y Y Y Y Y
sinh Y Y Y Y Y
sqrt Y Y Y Y Y
tan Y Y Y Y Y
tanh Y Y Y Y Y
tgamma Y Y N Y N
trunc Y Y N Y N
y0 N Y N N Y _y0
y1 N Y N N Y _y1
yn N Y N N Y _yn

(the base table came from http://www.johndcook.com/blog/math_h/ )

links

  • https://en.wikipedia.org/wiki/C_mathematical_functions
  • http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf

jlp765 avatar Jan 19 '16 22:01 jlp765

I think it makes sense to include everything that is in ISO 99. Please create a PR to do so if you've got some time.

dom96 avatar Apr 04 '16 21:04 dom96

An equivalent to modf is now included in 46a2993917edd73fe55623a06999f4811067a754 (although it's called splitDecimal and returns the values as a tuple rather than passing a reference to an argument).

abudden avatar Jun 01 '16 12:06 abudden

we should have a std/cmath.nim that is a pure low-level wrapper around importc procs.

Then std/math can import it but also work cross-backend with high-level portable wrappers that work in c,cpp,js,vm,nimscript backends.

EDIT: refs https://github.com/nim-lang/Nim/pull/16179#discussion_r535493710

timotheecour avatar Nov 29 '20 03:11 timotheecour

Minor updated table:

Function Nim POSIX old ISO ISO C99 Microsoft Notes
acos Y (arccos) Y Y Y Y
acosh Y (arccosh)\ Y N Y N
asin Y (arcsin) Y Y Y Y
asinh Y (arcsinh) Y N Y N
atan Y (arctan) Y Y Y Y
atan2 Y (arctan2) Y Y Y Y
atanh Y (arctanh) Y N Y N
cbrt Y Y N Y N
ceil Y Y Y Y Y
copysign N Y N Y Y _copysign
cos Y Y Y Y Y
cosh Y Y Y Y Y
erf Y Y N Y N
erfc Y Y N Y N
exp Y Y Y Y Y
exp2 N Y N Y N
expm1 N Y N Y N
fabs Y (abs) Y Y Y Y
fdim N Y N Y N
floor Y Y Y Y Y
fma N Y N Y N
fmax Y (max) Y N Y N
fmin Y (min) Y N Y N
fmod Y (mod) Y Y Y Y
fpclassify Y (classify) Y N Y Y _fpclass
frexp Y Y Y Y Y
hypot Y Y N Y Y _hypot
ilogb N Y N Y N
isfinite N Y N Y N
isgreater N Y N Y N
isgreaterequal N Y N Y N
isinf N Y N Y N
isless N Y N Y N
islessequal N Y N Y N
islessgreater N Y N Y N
isnan N Y N Y Y _isnan
isnormal N Y N Y N
isunordered N Y N Y N
j0 N Y N N Y _j0
j1 N Y N N Y _j1
jn N Y N N Y _jn
ldexp N Y Y Y Y
lgamma Y Y N Y N
llrint Y (round?) Y N Y N
llround N Y N Y N
log Y (ln) Y Y Y Y
log10 log10 Y Y Y Y
log1p Y N Y N
log2 Y Y N Y N
logb N Y N Y Y _logb
lrint Y (round) Y N Y N
lround N Y N Y N
modf Y (mod) Y Y Y Y
nan N (?) Y N Y N
nearbyint N Y N Y N
nextafter N Y N Y Y _nextafter
nexttoward N N N Y N
pow Y Y Y Y Y
remainder N (mod?) Y N Y N
remquo N Y N Y N
rint Y (round) Y N Y N
round N Y N Y N
scalbln N Y N Y N
scalbn N N N Y N
signbit N Y N Y N
sin Y Y Y Y Y
sinh Y Y Y Y Y
sqrt Y Y Y Y Y
tan Y Y Y Y Y
tanh Y Y Y Y Y
tgamma Y Y N Y N
trunc Y Y N Y N
y0 N Y N N Y _y0
y1 N Y N N Y _y1
yn N Y N N Y _yn

ringabout avatar Nov 29 '20 04:11 ringabout

  • not everything in this list is equally valuable, we should focus on most common first (subjective, yes).

lrint | Y (round)

  • we actually don't have lrint and I wish we did; round is different. maybe something like:
type RoundMode = enum rTowardsZero, ...
proc roundTo(x: SomeFloat, T: typedesc[SomeNumber], mode: static RoundMode): T
# or:
proc roundToInt(x: SomeFloat, mode: static RoundMode): int

which would map to one of the many lrint, lrintl, llrintl, round...

timotheecour avatar Nov 29 '20 08:11 timotheecour