2011-10-26 13:28:23 -05:00
|
|
|
/* Module: math */
|
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
export consts;
|
|
|
|
export min, max;
|
|
|
|
|
|
|
|
export f32, f64;
|
|
|
|
|
|
|
|
// Currently this module supports from -lmath
|
|
|
|
// C95 - frexp - ldexp - fmod - modf + log2 + log1p
|
|
|
|
|
|
|
|
export
|
|
|
|
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor,
|
|
|
|
ln, ln1p, log10, log2, pow, sin, sinh, sqrt, tan, tanh;
|
|
|
|
|
|
|
|
// These two must match in width according to architecture
|
|
|
|
|
|
|
|
import ctypes::c_float;
|
|
|
|
import c_float = f64;
|
|
|
|
|
|
|
|
|
|
|
|
#[link_name = "m"]
|
2011-11-16 22:49:38 -06:00
|
|
|
#[abi = "cdecl"]
|
2011-11-22 11:08:26 -06:00
|
|
|
native mod f64 {
|
|
|
|
|
|
|
|
// Alpabetically sorted by link_name
|
|
|
|
|
2011-11-22 13:03:37 -06:00
|
|
|
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;
|
|
|
|
#[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 pow(n: f64, e: 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;
|
2011-11-22 11:08:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[link_name = "m"]
|
|
|
|
#[abi = "cdecl"]
|
|
|
|
native mod f32 {
|
|
|
|
|
|
|
|
// Alpabetically sorted by link_name
|
|
|
|
|
2011-11-22 13:03:37 -06:00
|
|
|
#[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="powf"] pure fn pow(n: f32, e: 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="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;
|
2011-11-22 11:08:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mod consts {
|
|
|
|
/*
|
|
|
|
Const: pi
|
|
|
|
|
|
|
|
Archimedes' constant
|
|
|
|
*/
|
|
|
|
const pi: float = 3.14159265358979323846264338327950288;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: frac_pi_2
|
|
|
|
|
|
|
|
pi/2.0
|
|
|
|
*/
|
|
|
|
const frac_pi_2: float = 1.57079632679489661923132169163975144;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: frac_pi_4
|
|
|
|
|
|
|
|
pi/4.0
|
|
|
|
*/
|
|
|
|
const frac_pi_4: float = 0.785398163397448309615660845819875721;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: frac_1_pi
|
|
|
|
|
|
|
|
1.0/pi
|
|
|
|
*/
|
|
|
|
const frac_1_pi: float = 0.318309886183790671537767526745028724;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: frac_2_pi
|
|
|
|
|
|
|
|
2.0/pi
|
|
|
|
*/
|
|
|
|
const frac_2_pi: float = 0.636619772367581343075535053490057448;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: frac_2_sqrtpi
|
|
|
|
|
|
|
|
2.0/sqrt(pi)
|
|
|
|
*/
|
|
|
|
const frac_2_sqrtpi: float = 1.12837916709551257389615890312154517;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: sqrt2
|
|
|
|
|
|
|
|
sqrt(2.0)
|
|
|
|
*/
|
|
|
|
const sqrt2: float = 1.41421356237309504880168872420969808;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: frac_1_sqrt2
|
|
|
|
|
|
|
|
1.0/sqrt(2.0)
|
|
|
|
*/
|
|
|
|
const frac_1_sqrt2: float = 0.707106781186547524400844362104849039;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: e
|
|
|
|
|
|
|
|
Euler's number
|
|
|
|
*/
|
|
|
|
const e: float = 2.71828182845904523536028747135266250;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: log2_e
|
|
|
|
|
|
|
|
log2(e)
|
|
|
|
*/
|
|
|
|
const log2_e: float = 1.44269504088896340735992468100189214;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: log10_e
|
|
|
|
|
|
|
|
log10(e)
|
|
|
|
*/
|
|
|
|
const log10_e: float = 0.434294481903251827651128918916605082;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: ln_2
|
|
|
|
|
|
|
|
ln(2.0)
|
|
|
|
*/
|
|
|
|
const ln_2: float = 0.693147180559945309417232121458176568;
|
|
|
|
|
|
|
|
/*
|
|
|
|
Const: ln_10
|
|
|
|
|
|
|
|
ln(10.0)
|
|
|
|
*/
|
|
|
|
const ln_10: float = 2.30258509299404568401799145468436421;
|
|
|
|
|
2011-10-18 06:45:17 -05:00
|
|
|
}
|
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: min
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the minimum of two values
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn min<copy T>(x: T, y: T) -> T { x < y ? x : y }
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: max
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the maximum of two values
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn max<copy T>(x: T, y: T) -> T { x < y ? y : x }
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: acos
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the arccosine of an angle (measured in rad)
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn acos(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::acos(x as c_float) as float }
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: asin
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the arcsine of an angle (measured in rad)
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn asin(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::asin(x as c_float) as float }
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: atan
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the arctangents of an angle (measured in rad)
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn atan(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::atan(x as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: atan2
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the arctangent of an angle (measured in rad)
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn atan2(y: float, x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::atan2(y as c_float, x as c_float) as float }
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: ceil
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns:
|
|
|
|
|
|
|
|
The smallest integral value less than or equal to `n`
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn ceil(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::ceil(n as c_float) as float }
|
2011-10-18 06:45:17 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: cos
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the cosine of an angle `x` (measured in rad)
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn cos(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::cos(x as c_float) as float }
|
2011-10-18 06:45:17 -05:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: cosh
|
|
|
|
|
|
|
|
Returns the hyperbolic cosine of `x`
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn cosh(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::cosh(x as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
2011-10-26 18:24:31 -05:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: exp
|
2011-10-26 18:24:31 -05:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns:
|
|
|
|
|
|
|
|
e to the power of `n*
|
2011-10-26 18:24:31 -05:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn exp(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::exp(n as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: abs
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
The absolute value of `n`
|
|
|
|
|
|
|
|
*/
|
|
|
|
pure fn abs(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::abs(n as c_float) as float }
|
2011-11-20 11:44:37 -06:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: floor
|
|
|
|
|
|
|
|
Returns:
|
2011-11-20 11:44:37 -06:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
The largest integral value less than or equal to `n`
|
2011-11-20 11:44:37 -06:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn floor(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::floor(n as c_float) as float }
|
2011-11-20 11:44:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: ln
|
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the natural logaritm of `n`
|
2011-11-20 11:44:37 -06:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn ln(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::ln(n as c_float) as float }
|
2011-11-20 11:44:37 -06:00
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: ln1p
|
|
|
|
|
|
|
|
Returns the natural logarithm of `1+n` accurately,
|
|
|
|
even for very small values of `n`
|
|
|
|
*/
|
|
|
|
pure fn ln1p(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::ln1p(n as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: log10
|
2011-11-20 11:44:37 -06:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the logarithm to base 10 of `n`
|
2011-11-20 11:44:37 -06:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn log10(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::log10(n as c_float) as float }
|
2011-11-20 11:44:37 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: log2
|
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the logarithm to base 2 of `n`
|
|
|
|
*/
|
|
|
|
pure fn log2(n: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::log2(n as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: pow
|
2011-11-20 11:44:37 -06:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn pow(v: float, e: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::pow(v as c_float, e as c_float) as float }
|
2011-11-20 11:44:37 -06:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2011-11-22 11:08:26 -06:00
|
|
|
Function: sin
|
2011-11-20 11:44:37 -06:00
|
|
|
|
2011-11-22 11:08:26 -06:00
|
|
|
Returns the sine of an angle `x` (measured in rad)
|
|
|
|
*/
|
|
|
|
pure fn sin(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::sin(x as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: sinh
|
|
|
|
|
|
|
|
Returns the hyperbolic sine of an angle `x` (measured in rad)
|
2011-11-20 11:44:37 -06:00
|
|
|
*/
|
2011-11-22 11:08:26 -06:00
|
|
|
pure fn sinh(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::sinh(x as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: sqrt
|
|
|
|
|
|
|
|
Returns the square root of `x`
|
|
|
|
*/
|
|
|
|
pure fn sqrt(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::sqrt(x as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: tan
|
|
|
|
|
|
|
|
Returns the tangent of an angle `x` (measured in rad)
|
|
|
|
|
|
|
|
*/
|
|
|
|
pure fn tan(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::tan(x as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
/*
|
|
|
|
Function: tanh
|
|
|
|
|
|
|
|
Returns the hyperbolic tangent of an angle `x` (measured in rad)
|
|
|
|
|
|
|
|
*/
|
|
|
|
pure fn tanh(x: float) -> float
|
2011-11-22 13:03:37 -06:00
|
|
|
{ c_float::tanh(x as c_float) as float }
|
2011-11-22 11:08:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|