std: factored f32 and f64 out from math

This commit is contained in:
Stefan Plantikow 2011-11-23 18:37:17 +01:00 committed by Brian Anderson
parent a611496ddf
commit 97fc39b214
6 changed files with 289 additions and 77 deletions

71
src/lib/cmath.rs Normal file
View File

@ -0,0 +1,71 @@
import ctypes::c_int;
#[link_name = "m"]
#[abi = "cdecl"]
native mod f64 {
// Alpabetically sorted by link_name
pure fn acos(n: f64) -> f64;
pure fn asin(n: f64) -> f64;
pure fn atan(n: f64) -> f64;
pure fn atan2(a: f64, b: f64) -> f64;
pure fn ceil(n: f64) -> f64;
pure fn cos(n: f64) -> f64;
pure fn cosh(n: f64) -> f64;
pure fn exp(n: f64) -> f64;
#[link_name="fabs"] pure fn abs(n: f64) -> f64;
pure fn floor(n: f64) -> f64;
pure fn fmod(x: f64, y: f64) -> f64;
pure fn frexp(n: f64, &value: c_int) -> f64;
pure fn ldexp(x: f64, n: c_int) -> f64;
#[link_name="log"] pure fn ln(n: f64) -> f64;
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
pure fn log10(n: f64) -> f64;
pure fn log2(n: f64) -> f64;
pure fn modf(n: f64, &iptr: f64) -> f64;
pure fn pow(n: f64, e: f64) -> f64;
pure fn rint(n: f64) -> f64;
pure fn round(n: f64) -> f64;
pure fn sin(n: f64) -> f64;
pure fn sinh(n: f64) -> f64;
pure fn sqrt(n: f64) -> f64;
pure fn tan(n: f64) -> f64;
pure fn tanh(n: f64) -> f64;
pure fn trunc(n: f64) -> f64;
}
#[link_name = "m"]
#[abi = "cdecl"]
native mod f32 {
// Alpabetically sorted by link_name
#[link_name="acosf"] pure fn acos(n: f32) -> f32;
#[link_name="asinf"] pure fn asin(n: f32) -> f32;
#[link_name="atanf"] pure fn atan(n: f32) -> f32;
#[link_name="atan2f"] pure fn atan2(a: f32, b: f32) -> f32;
#[link_name="ceilf"] pure fn ceil(n: f32) -> f32;
#[link_name="cosf"] pure fn cos(n: f32) -> f32;
#[link_name="coshf"] pure fn cosh(n: f32) -> f32;
#[link_name="expf"] pure fn exp(n: f32) -> f32;
#[link_name="fabsf"] pure fn abs(n: f32) -> f32;
#[link_name="floorf"] pure fn floor(n: f32) -> f32;
#[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32;
#[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32;
#[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32;
#[link_name="logf"] pure fn ln(n: f32) -> f32;
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
#[link_name="modff"] pure fn modf(n: f32, &iptr: f32) -> f32;
#[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32;
#[link_name="rintf"] pure fn rint(n: f32) -> f32;
#[link_name="roundf"] pure fn round(n: f32) -> f32;
#[link_name="sinf"] pure fn sin(n: f32) -> f32;
#[link_name="sinhf"] pure fn sinh(n: f32) -> f32;
#[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32;
#[link_name="tanf"] pure fn tan(n: f32) -> f32;
#[link_name="tanhf"] pure fn tanh(n: f32) -> f32;
#[link_name="truncf"] pure fn trunc(n: f32) -> f32;
}

102
src/lib/f32.rs Normal file
View File

@ -0,0 +1,102 @@
import cmath::f32::*;
export
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod,
frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin,
sinh, sqrt, tan, tanh, trunc;
export consts;
mod consts {
/*
Const: pi
Archimedes' constant
*/
const pi: f32 = 3.14159265358979323846264338327950288f32;
/*
Const: frac_pi_2
pi/2.0
*/
const frac_pi_2: f32 = 1.57079632679489661923132169163975144f32;
/*
Const: frac_pi_4
pi/4.0
*/
const frac_pi_4: f32 = 0.785398163397448309615660845819875721f32;
/*
Const: frac_1_pi
1.0/pi
*/
const frac_1_pi: f32 = 0.318309886183790671537767526745028724f32;
/*
Const: frac_2_pi
2.0/pi
*/
const frac_2_pi: f32 = 0.636619772367581343075535053490057448f32;
/*
Const: frac_2_sqrtpi
2.0/sqrt(pi)
*/
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517f32;
/*
Const: sqrt2
sqrt(2.0)
*/
const sqrt2: f32 = 1.41421356237309504880168872420969808f32;
/*
Const: frac_1_sqrt2
1.0/sqrt(2.0)
*/
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039f32;
/*
Const: e
Euler's number
*/
const e: f32 = 2.71828182845904523536028747135266250f32;
/*
Const: log2_e
log2(e)
*/
const log2_e: f32 = 1.44269504088896340735992468100189214f32;
/*
Const: log10_e
log10(e)
*/
const log10_e: f32 = 0.434294481903251827651128918916605082f32;
/*
Const: ln_2
ln(2.0)
*/
const ln_2: f32 = 0.693147180559945309417232121458176568f32;
/*
Const: ln_10
ln(10.0)
*/
const ln_10: f32 = 2.30258509299404568401799145468436421f32;
}

102
src/lib/f64.rs Normal file
View File

@ -0,0 +1,102 @@
import cmath::f64::*;
export
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor, fmod,
frexp, ldexp, ln, ln1p, log10, log2, modf, rint, round, pow, sin,
sinh, sqrt, tan, tanh, trunc;
export consts;
mod consts {
/*
Const: pi
Archimedes' constant
*/
const pi: f64 = 3.14159265358979323846264338327950288f64;
/*
Const: frac_pi_2
pi/2.0
*/
const frac_pi_2: f64 = 1.57079632679489661923132169163975144f64;
/*
Const: frac_pi_4
pi/4.0
*/
const frac_pi_4: f64 = 0.785398163397448309615660845819875721f64;
/*
Const: frac_1_pi
1.0/pi
*/
const frac_1_pi: f64 = 0.318309886183790671537767526745028724f64;
/*
Const: frac_2_pi
2.0/pi
*/
const frac_2_pi: f64 = 0.636619772367581343075535053490057448f64;
/*
Const: frac_2_sqrtpi
2.0/sqrt(pi)
*/
const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517f64;
/*
Const: sqrt2
sqrt(2.0)
*/
const sqrt2: f64 = 1.41421356237309504880168872420969808f64;
/*
Const: frac_1_sqrt2
1.0/sqrt(2.0)
*/
const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039f64;
/*
Const: e
Euler's number
*/
const e: f64 = 2.71828182845904523536028747135266250f64;
/*
Const: log2_e
log2(e)
*/
const log2_e: f64 = 1.44269504088896340735992468100189214f64;
/*
Const: log10_e
log10(e)
*/
const log10_e: f64 = 0.434294481903251827651128918916605082f64;
/*
Const: ln_2
ln(2.0)
*/
const ln_2: f64 = 0.693147180559945309417232121458176568f64;
/*
Const: ln_10
ln(10.0)
*/
const ln_10: f64 = 2.30258509299404568401799145468436421f64;
}

View File

@ -3,8 +3,6 @@
export consts;
export min, max;
export f32, f64;
// Currently this module supports from -lmath:
// C95 + log2 + log1p + trunc + round + rint
@ -19,77 +17,7 @@ import ctypes::c_float;
import ctypes::c_int;
import c_float = f64;
#[link_name = "m"]
#[abi = "cdecl"]
native mod f64 {
// Alpabetically sorted by link_name
pure fn acos(n: f64) -> f64;
pure fn asin(n: f64) -> f64;
pure fn atan(n: f64) -> f64;
pure fn atan2(a: f64, b: f64) -> f64;
pure fn ceil(n: f64) -> f64;
pure fn cos(n: f64) -> f64;
pure fn cosh(n: f64) -> f64;
pure fn exp(n: f64) -> f64;
#[link_name="fabs"] pure fn abs(n: f64) -> f64;
pure fn floor(n: f64) -> f64;
pure fn fmod(x: f64, y: f64) -> f64;
pure fn frexp(n: f64, &value: c_int) -> f64;
pure fn ldexp(x: f64, n: c_int) -> f64;
#[link_name="log"] pure fn ln(n: f64) -> f64;
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
pure fn log10(n: f64) -> f64;
pure fn log2(n: f64) -> f64;
pure fn modf(n: f64, &iptr: f64) -> f64;
pure fn pow(n: f64, e: f64) -> f64;
pure fn rint(n: f64) -> f64;
pure fn round(n: f64) -> f64;
pure fn sin(n: f64) -> f64;
pure fn sinh(n: f64) -> f64;
pure fn sqrt(n: f64) -> f64;
pure fn tan(n: f64) -> f64;
pure fn tanh(n: f64) -> f64;
pure fn trunc(n: f64) -> f64;
}
#[link_name = "m"]
#[abi = "cdecl"]
native mod f32 {
// Alpabetically sorted by link_name
#[link_name="acosf"] pure fn acos(n: f32) -> f32;
#[link_name="asinf"] pure fn asin(n: f32) -> f32;
#[link_name="atanf"] pure fn atan(n: f32) -> f32;
#[link_name="atan2f"] pure fn atan2(a: f32, b: f32) -> f32;
#[link_name="ceilf"] pure fn ceil(n: f32) -> f32;
#[link_name="cosf"] pure fn cos(n: f32) -> f32;
#[link_name="coshf"] pure fn cosh(n: f32) -> f32;
#[link_name="expf"] pure fn exp(n: f32) -> f32;
#[link_name="fabsf"] pure fn abs(n: f32) -> f32;
#[link_name="floorf"] pure fn floor(n: f32) -> f32;
#[link_name="frexpf"] pure fn frexp(n: f64, &value: c_int) -> f32;
#[link_name="fmodf"] pure fn fmod(x: f32, y: f32) -> f32;
#[link_name="ldexpf"] pure fn ldexp(x: f32, n: c_int) -> f32;
#[link_name="logf"] pure fn ln(n: f32) -> f32;
#[link_name="log1p"] pure fn ln1p(n: f64) -> f64;
#[link_name="log2f"] pure fn log2(n: f32) -> f32;
#[link_name="log10f"] pure fn log10(n: f32) -> f32;
#[link_name="modff"] pure fn modf(n: f32, &iptr: f32) -> f32;
#[link_name="powf"] pure fn pow(n: f32, e: f32) -> f32;
#[link_name="rintf"] pure fn rint(n: f32) -> f32;
#[link_name="roundf"] pure fn round(n: f32) -> f32;
#[link_name="sinf"] pure fn sin(n: f32) -> f32;
#[link_name="sinhf"] pure fn sinh(n: f32) -> f32;
#[link_name="sqrtf"] pure fn sqrt(n: f32) -> f32;
#[link_name="tanf"] pure fn tan(n: f32) -> f32;
#[link_name="tanhf"] pure fn tanh(n: f32) -> f32;
#[link_name="truncf"] pure fn trunc(n: f32) -> f32;
}
// FIXME replace with redirect to c_float::consts::FOO as soon as it works
mod consts {
/*
Const: pi
@ -181,9 +109,12 @@ mod consts {
ln(10.0)
*/
const ln_10: float = 2.30258509299404568401799145468436421;
}
// FIXME min/max type specialize via libm when overloading works
// (in theory fmax/fmin, fmaxf, fminf /should/ be faster)
/*
Function: min

View File

@ -7,7 +7,8 @@
#[license = "BSD"];
export box, char, float, int, str, ptr, uint, u8, u32, u64, vec, bool;
export box, char, float, f32, f64, int, str, ptr;
export uint, u8, u32, u64, vec, bool;
export comm, fs, io, net, run, sys, task;
export ctypes, either, option, result, four, tri, util;
export bitv, deque, fun_treemap, list, map, smallintmap, sort, treemap, ufind;
@ -23,6 +24,8 @@ export generic_os, os, os_fs;
mod box;
mod char;
mod float;
mod f32;
mod f64;
mod int;
mod str;
mod ptr;
@ -49,6 +52,7 @@ mod task;
// Utility modules
mod ctypes;
mod cmath; /* unexported */
mod either;
mod option;
mod result;

View File

@ -258,11 +258,13 @@ fn test_log_functions() {
assert log2(1.0) == 0.0;
assert log10(1.0) == 0.0;
assert ln(consts::e) == 1.0;
// FIXME remove round-up due to valgrind weirdness
assert ceil(ln(consts::e)) /* ln(e) == 0.999.. under valgrind */
assert log2(2.0) == 1.0;
assert log10(10.0) == 1.0;
assert ln(consts::e*consts::e*consts::e*consts::e) == 4.0;
// FIXME remove round-up due to valgrind weirdness
assert ceil(ln(consts::e*consts::e*consts::e*consts::e)) == 4.0;
assert log2(256.0) == 8.0;
assert log10(1000.0) == 3.0;