std: added missing calls to math; covers C95 completely now, includes tests

This commit is contained in:
Stefan Plantikow 2011-11-23 01:39:17 +01:00 committed by Brian Anderson
parent bd405fb457
commit a611496ddf
2 changed files with 336 additions and 31 deletions

View File

@ -5,16 +5,18 @@ export min, max;
export f32, f64;
// Currently this module supports from -lmath
// C95 - frexp - ldexp - fmod - modf + log2 + log1p
// Currently this module supports from -lmath:
// C95 + log2 + log1p + trunc + round + rint
export
acos, asin, atan, atan2, ceil, cos, cosh, exp, abs, floor,
ln, ln1p, log10, log2, pow, sin, sinh, sqrt, tan, tanh;
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;
// These two must match in width according to architecture
import ctypes::c_float;
import ctypes::c_int;
import c_float = f64;
@ -34,16 +36,23 @@ native mod 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"]
@ -62,19 +71,25 @@ native mod 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="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="truncf"] pure fn trunc(n: f32) -> f32;
}
mod consts {
/*
Const: pi
@ -219,9 +234,7 @@ pure fn atan2(y: float, x: float) -> float
/*
Function: ceil
Returns:
The smallest integral value less than or equal to `n`
Returns the smallest integral value less than or equal to `n`
*/
pure fn ceil(n: float) -> float
{ c_float::ceil(n as c_float) as float }
@ -247,9 +260,7 @@ pure fn cosh(x: float) -> float
/*
Function: exp
Returns:
e to the power of `n*
Returns `consts::e` to the power of `n*
*/
pure fn exp(n: float) -> float
{ c_float::exp(n as c_float) as float }
@ -257,10 +268,7 @@ pure fn exp(n: float) -> float
/*
Function: abs
Returns:
The absolute value of `n`
Returns the absolute value of `n`
*/
pure fn abs(n: float) -> float
{ c_float::abs(n as c_float) as float }
@ -268,13 +276,19 @@ pure fn abs(n: float) -> float
/*
Function: floor
Returns:
The largest integral value less than or equal to `n`
Returns the largest integral value less than or equal to `n`
*/
pure fn floor(n: float) -> float
{ c_float::floor(n as c_float) as float }
/*
Function: fmod
Returns the floating-point remainder of `x/y`
*/
pure fn fmod(x: float, y: float) -> float
{ c_float::fmod(x as c_float, y as c_float) as float }
/*
Function: ln
@ -283,6 +297,14 @@ Returns the natural logaritm of `n`
pure fn ln(n: float) -> float
{ c_float::ln(n as c_float) as float }
/*
Function: ldexp
Returns `x` multiplied by 2 to the power of `n`
*/
pure fn ldexp(n: float, i: int) -> float
{ c_float::ldexp(n as c_float, i as c_int) as float }
/*
Function: ln1p
@ -308,6 +330,45 @@ Returns the logarithm to base 2 of `n`
pure fn log2(n: float) -> float
{ c_float::log2(n as c_float) as float }
/*
Function: modf
Breaks `n` into integral and fractional parts such that both
have the same sign as `n`
The integral part is stored in `iptr`.
Returns:
The fractional part of `n`
*/
pure fn modf(n: float, &iptr: float) -> float {
unchecked {
let f = iptr as c_float;
let r = c_float::modf(n as c_float, f) as float;
iptr = f as float;
ret r;
}
}
/*
Function: frexp
Breaks `n` into a normalized fraction and an integral power of 2
The inegral part is stored in iptr.
The functions return a number x such that x has a magnitude in the interval
[1/2, 1) or 0, and `n == x*(2 to the power of exp)`.
Returns:
The fractional part of `n`
*/
pure fn frexp(n: float, &exp: c_int) -> float
{ c_float::frexp(n as c_float, exp) as float }
/*
Function: pow
*/
@ -315,6 +376,25 @@ pure fn pow(v: float, e: float) -> float
{ c_float::pow(v as c_float, e as c_float) as float }
/*
Function: rint
Returns the integral value nearest to `x` (according to the
prevailing rounding mode) in floating-point format
*/
pure fn rint(x: float) -> float
{ c_float::rint(x as c_float) as float }
/*
Function: round
Return the integral value nearest to `x` rounding half-way
cases away from zero, regardless of the current rounding direction.
*/
pure fn round(x: float) -> float
{ c_float::round(x as c_float) as float }
/*
Function: sin
@ -357,6 +437,15 @@ Returns the hyperbolic tangent of an angle `x` (measured in rad)
pure fn tanh(x: float) -> float
{ c_float::tanh(x as c_float) as float }
/*
Function: trunc
Returns the integral value nearest to but no larger in magnitude than `x`
*/
pure fn trunc(x: float) -> float
{ c_float::trunc(x as c_float) as float }

View File

@ -1,14 +1,8 @@
use std;
import std::math::*;
import std::float;
#[test]
fn test_sqrt() {
assert sqrt(9.0) == 3.0;
assert sqrt(4.0) == 2.0;
assert sqrt(1.0) == 1.0;
assert sqrt(0.0) == 0.0;
}
import c_int = std::ctypes::c_int;
#[test]
fn test_max_min() {
@ -20,6 +14,229 @@ fn test_max_min() {
assert min(0.0, 1.0) == 0.0;
}
// FIXME use macros to execute the tests below for all float types
#[test]
fn test_trig() {
assert sin(0.0) == 0.0;
assert sin(-0.0) == 0.0;
assert float::isNaN(sin(float::infinity));
assert float::isNaN(sin(float::neg_infinity));
assert cos(0.0) == 1.0;
assert cos(-0.0) == 1.0;
assert float::isNaN(cos(float::infinity));
assert float::isNaN(cos(float::neg_infinity));
assert tan(0.0) == 0.0;
assert tan(-0.0) == 0.0;;
assert float::isNaN(tan(float::infinity));
assert float::isNaN(tan(float::neg_infinity));
}
#[test]
fn test_inv_trig() {
assert asin(0.0) == 0.0;
assert asin(-0.0) == -0.0;
assert float::isNaN(asin(1.1));
assert float::isNaN(asin(-1.1));
assert acos(1.0) == 0.0;
assert float::isNaN(acos(1.1));
assert float::isNaN(acos(-1.1));
assert atan(0.0) == 0.0;
assert atan(-0.0) == 0.0;
assert atan(float::infinity) == consts::frac_pi_2;
assert atan(float::neg_infinity) == - consts::frac_pi_2;
assert atan2(0.0, -0.0) == consts::pi;
assert atan2(-0.0, -0.0) == -consts::pi;
assert atan2(0.0, 0.0) == 0.0;
assert atan2(-0.0, 0.0) == -0.0;
assert atan2(0.0, -1.0) == consts::pi;
assert atan2(-0.0, -1.0) == -consts::pi;
assert atan2(0.0, 1.0) == 0.0;
assert atan2(-0.0, 1.0) == -0.0;
assert atan2(1.0, 0.0) == consts::frac_pi_2;
assert atan2(1.0, -0.0) == consts::frac_pi_2;
}
#[test]
fn test_pow() {
assert pow(2.0, 4.0) == 16.0;
assert pow(0.0, -3.0) == float::infinity;
assert pow(-0.0, -3.0) == float::neg_infinity;
assert pow(0.0, -4.0) == float::infinity;
assert pow(-0.0, -4.0) == float::infinity;
assert pow(0.0, 3.0) == 0.0;
assert pow(-0.0, 3.0) == -0.0;
assert pow(0.0, 4.0) == 0.0;
assert pow(-0.0, 4.0) == 0.0;
assert pow(-1.0, float::infinity) == 1.0;
assert pow(-1.0, float::neg_infinity) == 1.0;
assert pow(1.0, 4.0) == 1.0;
assert pow(1.0, 0.0) == 1.0;
assert pow(1.0, -0.0) == 1.0;
assert pow(1.0, float::NaN) == 1.0;
assert pow(1.0, float::infinity) == 1.0;
assert pow(1.0, float::neg_infinity) == 1.0;
assert pow(1.0, -3.0) == 1.0;
assert pow(1.0, -4.0) == 1.0;
assert pow(4.0, 0.0) == 1.0;
assert pow(0.0, 0.0) == 1.0;
assert pow(-0.0, 0.0) == 1.0;
assert pow(float::NaN, 0.0) == 1.0;
assert pow(float::infinity, 0.0) == 1.0;
assert pow(float::neg_infinity, 0.0) == 1.0;
assert pow(-3.0, 0.0) == 1.0;
assert pow(-4.0, 0.0) == 1.0;
assert pow(4.0, -0.0) == 1.0;
assert pow(0.0, -0.0) == 1.0;
assert pow(-0.0, -0.0) == 1.0;
assert pow(float::NaN, -0.0) == 1.0;
assert pow(float::infinity, -0.0) == 1.0;
assert pow(float::neg_infinity, -0.0) == 1.0;
assert pow(-3.0, -0.0) == 1.0;
assert pow(-4.0, -0.0) == 1.0;
assert float::isNaN(pow(-1.0, -1.5));
assert float::isNaN(pow(-1.0, 1.5));
assert float::isNaN(pow(-1.2, -1.5));
assert float::isNaN(pow(-1.2, 1.5));
assert pow(0.5, float::neg_infinity) == float::infinity;
assert pow(-0.5, float::neg_infinity) == float::infinity;
assert pow(1.5, float::neg_infinity) == 0.0;
assert pow(-1.5, float::neg_infinity) == 0.0;
assert pow(0.5, float::infinity) == 0.0;
assert pow(-0.5, float::infinity) == 0.0;
assert pow(-1.5, float::infinity) == float::infinity;
assert pow(1.5, float::infinity) == float::infinity;
assert pow(float::neg_infinity, -3.0) == -0.0;
assert pow(float::neg_infinity, -4.0) == 0.0;
assert pow(float::neg_infinity, 3.0) == float::neg_infinity;
assert pow(float::neg_infinity, 4.0) == float::infinity;
assert pow(float::infinity, -16.0) == 0.0;
assert pow(float::infinity, 16.0) == float::infinity;
}
#[test]
fn test_exp_and_mod() {
assert exp(0.0) == 1.0;
assert exp(-0.0) == 1.0;
assert exp(float::neg_infinity) == 0.0;
assert exp(float::infinity) == float::infinity;
let d1: c_int = 1 as c_int;
assert frexp(0.0, d1) == 0.0;
assert frexp(-0.0, d1) == 0.0;
assert frexp(float::infinity, d1) == float::infinity;
assert frexp(float::neg_infinity, d1) == float::neg_infinity;
assert float::isNaN(frexp(float::NaN, d1));
let d2: float = 1.0;
assert modf(float::infinity, d2) == 0.0;
assert modf(float::neg_infinity, d2) == -0.0;
assert float::isNaN(modf(float::NaN, d2));
}
#[test]
fn test_round_and_abs() {
assert abs(0.0) == 0.0;
assert abs(-0.0) == 0.0;
assert abs(float::infinity) == float::infinity;
assert abs(float::neg_infinity) == float::infinity;
assert abs(-2.5) == 2.5;
assert abs(2.5) == 2.5;
assert ceil(0.0) == 0.0;
assert ceil(-0.0) == -0.0;
assert ceil(float::infinity) == float::infinity;
assert ceil(float::neg_infinity) == float::neg_infinity;
assert ceil(1.9) == 2.0;
assert ceil(-1.9) == -1.0;
assert floor(0.0) == 0.0;
assert floor(-0.0) == -0.0;
assert floor(float::infinity) == float::infinity;
assert floor(float::neg_infinity) == float::neg_infinity;
assert floor(1.9) == 1.0;
assert floor(-1.9) == -2.0;
assert trunc(0.0) == 0.0;
assert trunc(-0.0) == -0.0;
assert trunc(float::infinity) == float::infinity;
assert trunc(float::neg_infinity) == float::neg_infinity;
assert trunc(1.5) == 1.0;
assert trunc(1.2) == 1.0;
assert trunc(1.0) == 1.0;
assert trunc(1.9) == 1.0;
assert trunc(-1.5) == -1.0;
assert trunc(-1.2) == -1.0;
assert trunc(-1.0) == -1.0;
assert trunc(-1.9) == -1.0;
assert round(0.0) == 0.0;
assert round(-0.0) == -0.0;
assert round(float::infinity) == float::infinity;
assert round(float::neg_infinity) == float::neg_infinity;
assert rint(0.0) == 0.0;
assert rint(-0.0) == -0.0;
assert rint(float::infinity) == float::infinity;
assert rint(float::neg_infinity) == float::neg_infinity;
}
#[test]
fn test_hyp_trig() {
assert sinh(0.0) == 0.0;
assert sinh(-0.0) == 0.0;
assert sinh(float::infinity) == float::infinity;
assert sinh(float::neg_infinity) == float::neg_infinity;
assert cosh(0.0) == 1.0;
assert cosh(-0.0) == 1.0;
assert cosh(float::infinity) == float::infinity;
assert cosh(float::neg_infinity) == float::infinity;
assert tanh(0.0) == 0.0;
assert tanh(-0.0) == 0.0;
assert tanh(float::infinity) == 1.0;
assert tanh(float::neg_infinity) == -1.0;
}
#[test]
fn test_sqrt() {
assert sqrt(9.0) == 3.0;
assert sqrt(4.0) == 2.0;
assert sqrt(1.0) == 1.0;
assert sqrt(0.0) == 0.0;
}
#[test]
fn test_angle() {
fn angle(vec: (float, float)) -> float {
@ -36,7 +253,6 @@ fn test_angle() {
#[test]
#[ignore]
fn test_log_functions() {
assert ln(1.0) == 0.0;
assert log2(1.0) == 0.0;