1
Fork 0

Merge pull request #1440 from boggle/kmath

Upgraded math to C99 + bessel functions and replaced wrappers with imports
This commit is contained in:
Graydon Hoare 2012-01-05 10:07:00 -08:00
commit e02ab2d65f
22 changed files with 792 additions and 771 deletions

View file

@ -129,7 +129,7 @@ fn get_relative_to(abs1: fs::path, abs2: fs::path) -> fs::path {
assert len1 > 0u;
assert len2 > 0u;
let max_common_path = float::min(len1, len2) - 1u;
let max_common_path = math::min(len1, len2) - 1u;
let start_idx = 0u;
while start_idx < max_common_path
&& split1[start_idx] == split2[start_idx] {

View file

@ -1765,7 +1765,7 @@ mod unify {
let vb = alt cx.st {
in_bindings(vb) { vb }
};
ufind::grow(vb.sets, float::max(set_a, set_b) + 1u);
ufind::grow(vb.sets, math::max(set_a, set_b) + 1u);
let root_a = ufind::find(vb.sets, set_a);
let root_b = ufind::find(vb.sets, set_b);

View file

@ -1,5 +1,4 @@
import core::{str, option};
import core::float::{max, min};
import math::{max, min};
import std::map::hashmap;
import option::{some};
import syntax::ast;

92
src/etc/cmathconsts.c Normal file
View file

@ -0,0 +1,92 @@
// This is a helper C program for generating required math constants
//
// Should only be required when porting to a different target architecture
// (or c compiler/libmath)
//
// Call with <rust machine type of c_float> <rust machine type of c_double>
// and ensure that libcore/cmath.rs complies to the output
//
// Requires a printf that supports "%a" specifiers
//
#include <float.h>
#include <math.h>
#include <stdio.h>
// must match core::ctypes
#define C_FLT(x) (float)x
#define C_DBL(x) (double)x
int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "%s <ctypes::c_float> <ctypes::c_double>\n", argv[0]);
return 1;
}
char* c_flt = argv[1];
char* c_dbl = argv[2];
printf("mod c_float_math_consts {\n");
printf(" const pi: c_float = %a_%s;\n", C_FLT(M_PI), c_flt);
printf(" const div_1_pi: c_float = %a_%s;\n", C_FLT(M_1_PI), c_flt);
printf(" const div_2_pi: c_float = %a_%s;\n", C_FLT(M_2_PI), c_flt);
printf(" const div_pi_2: c_float = %a_%s;\n", C_FLT(M_PI_2), c_flt);
printf(" const div_pi_4: c_float = %a_%s;\n", C_FLT(M_PI_4), c_flt);
printf(" const div_2_sqrtpi: c_float = %a_%s;\n",
C_FLT(M_2_SQRTPI), c_flt);
printf(" const e: c_float = %a_%s;\n", C_FLT(M_E), c_flt);
printf(" const log2_e: c_float = %a_%s;\n", C_FLT(M_LOG2E), c_flt);
printf(" const log10_e: c_float = %a_%s;\n", C_FLT(M_LOG10E), c_flt);
printf(" const ln_2: c_float = %a_%s;\n", C_FLT(M_LN2), c_flt);
printf(" const ln_10: c_float = %a_%s;\n", C_FLT(M_LN10), c_flt);
printf(" const sqrt2: c_float = %a_%s;\n", C_FLT(M_SQRT2), c_flt);
printf(" const div_1_sqrt2: c_float = %a_%s;\n",
C_FLT(M_SQRT1_2), c_flt);
printf("}\n\n");
printf("mod c_double_math_consts {\n");
printf(" const pi: c_double = %a_%s;\n", C_DBL(M_PI), c_dbl);
printf(" const div_1_pi: c_double = %a_%s;\n", C_DBL(M_1_PI), c_dbl);
printf(" const div_2_pi: c_double = %a_%s;\n", C_DBL(M_2_PI), c_dbl);
printf(" const div_pi_2: c_double = %a_%s;\n", C_DBL(M_PI_2), c_dbl);
printf(" const div_pi_4: c_double = %a_%s;\n", C_DBL(M_PI_4), c_dbl);
printf(" const div_2_sqrtpi: c_double = %a_%s;\n",
C_DBL(M_2_SQRTPI), c_dbl);
printf(" const e: c_double = %a_%s;\n", C_DBL(M_E), c_dbl);
printf(" const log2_e: c_double = %a_%s;\n", C_DBL(M_LOG2E), c_dbl);
printf(" const log10_e: c_double = %a_%s;\n", C_DBL(M_LOG10E), c_dbl);
printf(" const ln_2: c_double = %a_%s;\n", C_DBL(M_LN2), c_dbl);
printf(" const ln_10: c_double = %a_%s;\n", C_DBL(M_LN10), c_dbl);
printf(" const sqrt2: c_double = %a_%s;\n", C_DBL(M_SQRT2), c_dbl);
printf(" const div_1_sqrt2: c_double = %a_%s;\n",
C_DBL(M_SQRT1_2), c_dbl);
printf("}\n\n");
printf("mod c_float_targ_consts {\n");
printf(" const radix: uint = %uu;\n", FLT_RADIX);
printf(" const mantissa_digits: uint = %uu;\n", FLT_MANT_DIG);
printf(" const digits: uint = %uu;\n", FLT_DIG);
printf(" const min_exp: int = %i;\n", FLT_MIN_EXP);
printf(" const max_exp: int = %i;\n", FLT_MAX_EXP);
printf(" const min_10_exp: int = %i;\n", FLT_MIN_10_EXP);
printf(" const max_10_exp: int = %i;\n", FLT_MAX_10_EXP);
printf(" const min_value: c_float = %a_%s;\n", C_FLT(FLT_MIN), c_flt);
printf(" const max_value: c_float = %a_%s;\n", C_FLT(FLT_MAX), c_flt);
printf(" const epsilon: c_float = %a_%s;\n", C_FLT(FLT_EPSILON), c_flt);
printf("}\n\n");
printf("mod c_double_targ_consts {\n");
printf(" const radix: uint = %uu;\n", FLT_RADIX);
printf(" const mantissa_digits: uint = %uu;\n", DBL_MANT_DIG);
printf(" const digits: uint = %uu;\n", DBL_DIG);
printf(" const min_exp: int = %i;\n", DBL_MIN_EXP);
printf(" const max_exp: int = %i;\n", DBL_MAX_EXP);
printf(" const min_10_exp: int = %i;\n", DBL_MIN_10_EXP);
printf(" const max_10_exp: int = %i;\n", DBL_MAX_10_EXP);
printf(" const min_value: c_double = %a_%s;\n", C_DBL(DBL_MIN), c_dbl);
printf(" const max_value: c_double = %a_%s;\n", C_DBL(DBL_MAX), c_dbl);
printf(" const epsilon: c_double = %a_%s;\n", C_DBL(DBL_EPSILON), c_dbl);
printf("}\n");
return 0;
}

View file

@ -1,4 +1,3 @@
import core::{vec, str, int, uint, option, result};
import std::{fs, io};
import rustc::syntax::{ast, ast_util, fold, visit, codemap};
@ -241,9 +240,9 @@ fn check_variants_T<T: copy>(
let L = vec::len(things);
if L < 100u {
under(float::min(L, 20u)) {|i|
under(math::min(L, 20u)) {|i|
log(error, "Replacing... #" + uint::str(i));
under(float::min(L, 30u)) {|j|
under(math::min(L, 30u)) {|j|
log(error, "With... " + stringifier(@things[j]));
let crate2 = @replacer(crate, i, things[j], cx.mode);
// It would be best to test the *crate* for stability, but testing the

10
src/libcore/bessel.rs Normal file
View file

@ -0,0 +1,10 @@
// PORT import module that is based on cmath::c_double here
// (cant do better via libm; bessel functions only exist for c_double)
// code that wants to use bessel functions should use
// values of type bessel::t and cast from/to float/f32/f64
// when working with them at the peril of precision loss
// for platform neutrality
import f64::*;

View file

@ -1,81 +1,251 @@
export c_float;
export c_double;
// FIXME export c_float_math_consts;
// FIXME export c_double_math_consts;
export c_float_targ_consts;
export c_double_targ_consts;
import ctypes::c_int;
import ctypes::c_float;
import ctypes::c_double;
// function names are almost identical to C's libmath, a few have been
// renamed, grep for "rename:"
#[link_name = "m"]
#[abi = "cdecl"]
native mod f64 {
native mod c_double {
// 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 acos(n: c_double) -> c_double;
pure fn asin(n: c_double) -> c_double;
pure fn atan(n: c_double) -> c_double;
pure fn atan2(a: c_double, b: c_double) -> c_double;
pure fn cbrt(n: c_double) -> c_double;
pure fn ceil(n: c_double) -> c_double;
pure fn copysign(x: c_double, y: c_double) -> c_double;
pure fn cos(n: c_double) -> c_double;
pure fn cosh(n: c_double) -> c_double;
pure fn erf(n: c_double) -> c_double;
pure fn erfc(n: c_double) -> c_double;
pure fn exp(n: c_double) -> c_double;
pure fn expm1(n: c_double) -> c_double;
pure fn exp2(n: c_double) -> c_double;
#[link_name="fabs"] pure fn abs(n: c_double) -> c_double;
// rename: for clarity and consistency with add/sub/mul/div
#[link_name="fdim"] pure fn abs_sub(a: c_double, b: c_double) -> c_double;
pure fn floor(n: c_double) -> c_double;
// rename: for clarity and consistency with add/sub/mul/div
#[link_name="fma"] pure fn mul_add(a: c_double, b: c_double,
c: c_double) -> c_double;
#[link_name="fmax"] pure fn fmax(a: c_double, b: c_double) -> c_double;
#[link_name="fmin"] pure fn fmin(a: c_double, b: c_double) -> c_double;
pure fn nextafter(x: c_double, y: c_double) -> c_double;
pure fn frexp(n: c_double, &value: c_int) -> c_double;
pure fn hypot(x: c_double, y: c_double) -> c_double;
pure fn ldexp(x: c_double, n: c_int) -> c_double;
#[link_name="lgamma_r"] pure fn lgamma(n: c_double,
&sign: c_int) -> c_double;
// renamed: log is a reserved keyword; ln seems more natural, too
#[link_name="log"] pure fn ln(n: c_double) -> c_double;
// renamed: "logb" /often/ is confused for log2 by beginners
#[link_name="logb"] pure fn log_radix(n: c_double) -> c_double;
// renamed: to be consitent with log as ln
#[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double;
pure fn log10(n: c_double) -> c_double;
#[cfg(target_os="linux")]
#[cfg(target_os="macos")]
#[cfg(target_os="win32")]
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;
pure fn log2(n: c_double) -> c_double;
#[link_name="ilogb"] pure fn ilog_radix(n: c_double) -> c_int;
pure fn modf(n: c_double, &iptr: c_double) -> c_double;
pure fn pow(n: c_double, e: c_double) -> c_double;
// FIXME enable when rounding modes become available
// pure fn rint(n: c_double) -> c_double;
pure fn round(n: c_double) -> c_double;
// rename: for consistency with logradix
#[link_name="scalbn"] pure fn ldexp_radix(n: c_double, i: c_int) ->
c_double;
pure fn sin(n: c_double) -> c_double;
pure fn sinh(n: c_double) -> c_double;
pure fn sqrt(n: c_double) -> c_double;
pure fn tan(n: c_double) -> c_double;
pure fn tanh(n: c_double) -> c_double;
pure fn tgamma(n: c_double) -> c_double;
pure fn trunc(n: c_double) -> c_double;
// These are commonly only available for doubles
pure fn j0(n: c_double) -> c_double;
pure fn j1(n: c_double) -> c_double;
pure fn jn(i: c_int, n: c_double) -> c_double;
pure fn y0(n: c_double) -> c_double;
pure fn y1(n: c_double) -> c_double;
pure fn yn(i: c_int, n: c_double) -> c_double;
}
#[link_name = "m"]
#[abi = "cdecl"]
native mod f32 {
native mod c_float {
// 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="acosf"] pure fn acos(n: c_float) -> c_float;
#[link_name="asinf"] pure fn asin(n: c_float) -> c_float;
#[link_name="atanf"] pure fn atan(n: c_float) -> c_float;
#[link_name="atan2f"] pure fn atan2(a: c_float, b: c_float) -> c_float;
#[link_name="cbrtf"] pure fn cbrt(n: c_float) -> c_float;
#[link_name="ceilf"] pure fn ceil(n: c_float) -> c_float;
#[link_name="copysignf"] pure fn copysign(x: c_float,
y: c_float) -> c_float;
#[link_name="cosf"] pure fn cos(n: c_float) -> c_float;
#[link_name="coshf"] pure fn cosh(n: c_float) -> c_float;
#[link_name="erff"] pure fn erf(n: c_float) -> c_float;
#[link_name="erfcf"] pure fn erfc(n: c_float) -> c_float;
#[link_name="expf"] pure fn exp(n: c_float) -> c_float;
#[link_name="expm1f"]pure fn expm1(n: c_float) -> c_float;
#[link_name="exp2f"] pure fn exp2(n: c_float) -> c_float;
#[link_name="fabsf"] pure fn abs(n: c_float) -> c_float;
#[link_name="fdimf"] pure fn abs_sub(a: c_float, b: c_float) -> c_float;
#[link_name="floorf"] pure fn floor(n: c_float) -> c_float;
#[link_name="frexpf"] pure fn frexp(n: c_float,
&value: c_int) -> c_float;
#[link_name="fmaf"] pure fn mul_add(a: c_float,
b: c_float, c: c_float) -> c_float;
#[link_name="fmaxf"] pure fn fmax(a: c_float, b: c_float) -> c_float;
#[link_name="fminf"] pure fn fmin(a: c_float, b: c_float) -> c_float;
#[link_name="nextafterf"] pure fn nextafter(x: c_float,
y: c_float) -> c_float;
#[link_name="hypotf"] pure fn hypot(x: c_float, y: c_float) -> c_float;
#[link_name="ldexpf"] pure fn ldexp(x: c_float, n: c_int) -> c_float;
#[link_name="lgammaf_r"] pure fn lgamma(n: c_float,
&sign: c_int) -> c_float;
#[link_name="logf"] pure fn ln(n: c_float) -> c_float;
#[link_name="logbf"] pure fn log_radix(n: c_float) -> c_float;
#[link_name="log1pf"] pure fn ln1p(n: c_float) -> c_float;
#[cfg(target_os="linux")]
#[cfg(target_os="macos")]
#[cfg(target_os="win32")]
#[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;
#[link_name="log2f"] pure fn log2(n: c_float) -> c_float;
#[link_name="log10f"] pure fn log10(n: c_float) -> c_float;
#[link_name="ilogbf"] pure fn ilog_radix(n: c_float) -> c_int;
#[link_name="modff"] pure fn modf(n: c_float,
&iptr: c_float) -> c_float;
#[link_name="powf"] pure fn pow(n: c_float, e: c_float) -> c_float;
// FIXME enable when rounding modes become available
// #[link_name="rintf"] pure fn rint(n: c_float) -> c_float;
#[link_name="roundf"] pure fn round(n: c_float) -> c_float;
#[link_name="scalbnf"] pure fn ldexp_radix(n: c_float, i: c_int)
-> c_float;
#[link_name="sinf"] pure fn sin(n: c_float) -> c_float;
#[link_name="sinhf"] pure fn sinh(n: c_float) -> c_float;
#[link_name="sqrtf"] pure fn sqrt(n: c_float) -> c_float;
#[link_name="tanf"] pure fn tan(n: c_float) -> c_float;
#[link_name="tanhf"] pure fn tanh(n: c_float) -> c_float;
#[link_name="tgammaf"] pure fn tgamma(n: c_float) -> c_float;
#[link_name="truncf"] pure fn trunc(n: c_float) -> c_float;
}
// PORT check these by running src/etc/machconsts.c for your architecture
// FIXME obtain machine float/math constants automatically
mod c_float_targ_consts {
const radix: uint = 2u;
const mantissa_digits: uint = 24u;
const digits: uint = 6u;
const min_exp: uint = -125u;
const max_exp: uint = 128u;
const min_10_exp: int = -37;
const max_10_exp: int = 38;
// FIXME this is wrong! replace with hexadecimal (%a) constants below
const min_value: f32 = 1.175494e-38_f32;
const max_value: f32 = 3.402823e+38_f32;
const epsilon: f32 = 0.000000_f32;
}
mod c_double_targ_consts {
const radix: uint = 2u;
const mantissa_digits: uint = 53u;
const digits: uint = 15u;
const min_exp: uint = -1021u;
const max_exp: uint = 1024u;
const min_10_exp: int = -307;
const max_10_exp: int = 308;
// FIXME this is wrong! replace with hexadecimal (%a) constants below
const min_value: f64 = 2.225074e-308_f64;
const max_value: f64 = 1.797693e+308_f64;
const epsilon: f64 = 2.220446e-16_f64;
}
/*
FIXME use these once they can be parsed
mod c_float_math_consts {
const pi: c_float = 0x1.921fb6p+1_f32;
const div_1_pi: c_float = 0x1.45f306p-2_f32;
const div_2_pi: c_float = 0x1.45f306p-1_f32;
const div_pi_2: c_float = 0x1.921fb6p+0_f32;
const div_pi_4: c_float = 0x1.921fb6p-1_f32;
const div_2_sqrtpi: c_float = 0x1.20dd76p+0_f32;
const e: c_float = 0x1.5bf0a8p+1_f32;
const log2_e: c_float = 0x1.715476p+0_f32;
const log10_e: c_float = 0x1.bcb7b2p-2_f32;
const ln_2: c_float = 0x1.62e43p-1_f32;
const ln_10: c_float = 0x1.26bb1cp+1_f32;
const sqrt2: c_float = 0x1.6a09e6p+0_f32;
const div_1_sqrt2: c_float = 0x1.6a09e6p-1_f32;
}
mod c_double_math_consts {
const pi: c_double = 0x1.921fb54442d18p+1_f64;
const div_1_pi: c_double = 0x1.45f306dc9c883p-2_f64;
const div_2_pi: c_double = 0x1.45f306dc9c883p-1_f64;
const div_pi_2: c_double = 0x1.921fb54442d18p+0_f64;
const div_pi_4: c_double = 0x1.921fb54442d18p-1_f64;
const div_2_sqrtpi: c_double = 0x1.20dd750429b6dp+0_f64;
const e: c_double = 0x1.5bf0a8b145769p+1_f64;
const log2_e: c_double = 0x1.71547652b82fep+0_f64;
const log10_e: c_double = 0x1.bcb7b1526e50ep-2_f64;
const ln_2: c_double = 0x1.62e42fefa39efp-1_f64;
const ln_10: c_double = 0x1.26bb1bbb55516p+1_f64;
const sqrt2: c_double = 0x1.6a09e667f3bcdp+0_f64;
const div_1_sqrt2: c_double = 0x1.6a09e667f3bcdp-1_f64;
}
mod c_float_targ_consts {
const radix: uint = 2u;
const mantissa_digits: uint = 24u;
const digits: uint = 6u;
const min_exp: int = -125;
const max_exp: int = 128;
const min_10_exp: int = -37;
const max_10_exp: int = 38;
const min_value: c_float = 0x1p-126_f32;
const max_value: c_float = 0x1.fffffep+127_f32;
const epsilon: c_float = 0x1p-23_f32;
}
mod c_double_targ_consts {
const radix: uint = 2u;
const mantissa_digits: uint = 53u;
const digits: uint = 15u;
const min_exp: int = -1021;
const max_exp: int = 1024;
const min_10_exp: int = -307;
const max_10_exp: int = 308;
const min_value: c_double = 0x1p-1022_f64;
const max_value: c_double = 0x1.fffffffffffffp+1023_f64;
const epsilon: c_double = 0x1p-52_f64;
}
*/
//
// Local Variables:
// mode: rust

View file

@ -7,17 +7,19 @@
#[license = "BSD"];
#[crate_type = "lib"];
export box, char, float, f32, f64, int, str, ptr;
export box, char, float, bessel, f32, f64, int, str, ptr;
export uint, u8, u32, u64, vec, bool;
export either, option, result;
export ctypes, mtypes, sys, unsafe, comm, task;
export ctypes, sys, unsafe, comm, task;
export extfmt;
export math;
// Built-in-type support modules
mod box;
mod char;
mod float;
mod bessel;
mod f32;
mod f64;
mod int;
@ -44,7 +46,7 @@ mod result;
// Runtime and language-primitive support
mod ctypes;
mod mtypes;
mod math;
mod cmath;
mod sys;
mod unsafe;

View file

@ -9,6 +9,8 @@ FIXME: Add a test that uses some native code to verify these sizes,
which are not obviously correct for all potential platforms.
*/
// PORT adapt to architecture
/*
Type: c_int
@ -72,6 +74,20 @@ when interoperating with C void pointers can help in documentation.
*/
type void = int;
/*
Type: c_float
A float value with the same size as a C `float`
*/
type c_float = f32;
/*
Type: c_float
A float value with the same size as a C `double`
*/
type c_double = f64;
/*
Type: size_t
@ -114,3 +130,4 @@ Type: enum
An unsigned integer with the same size as a C enum
*/
type enum = u32;

View file

@ -1,122 +1,250 @@
/*
Module: f32
Floating point operations and constants for `f32`
This exposes the same operations as `math`, just for `f32` even though
they do not show up in the docs right now!
*/
import cmath::f32::*;
// PORT
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, t;
export consts;
import cmath::c_float::*;
import cmath::c_float_targ_consts::*;
type t = f32;
// These are not defined inside consts:: for consistency with
// the integer types
/* Const: NaN */
const NaN: f32 = 0.0_f32/0.0_f32;
/* Const: infinity */
const infinity: f32 = 1.0_f32/0.0_f32;
/* Const: neg_infinity */
const neg_infinity: f32 = -1.0_f32/0.0_f32;
/* Predicate: isNaN */
pure fn is_NaN(f: f32) -> bool { f != f }
/* Function: add */
pure fn add(x: f32, y: f32) -> f32 { ret x + y; }
/* Function: sub */
pure fn sub(x: f32, y: f32) -> f32 { ret x - y; }
/* Function: mul */
pure fn mul(x: f32, y: f32) -> f32 { ret x * y; }
/* Function: div */
pure fn div(x: f32, y: f32) -> f32 { ret x / y; }
/* Function: rem */
pure fn rem(x: f32, y: f32) -> f32 { ret x % y; }
/* Predicate: lt */
pure fn lt(x: f32, y: f32) -> bool { ret x < y; }
/* Predicate: le */
pure fn le(x: f32, y: f32) -> bool { ret x <= y; }
/* Predicate: eq */
pure fn eq(x: f32, y: f32) -> bool { ret x == y; }
/* Predicate: ne */
pure fn ne(x: f32, y: f32) -> bool { ret x != y; }
/* Predicate: ge */
pure fn ge(x: f32, y: f32) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: f32, y: f32) -> bool { ret x > y; }
// FIXME replace the predicates below with llvm intrinsics or calls
// to the libmath macros in the rust runtime for performance
/*
Predicate: is_positive
Returns true if `x` is a positive number, including +0.0f320 and +Infinity.
*/
pure fn is_positive(x: f32) -> bool
{ ret x > 0.0f32 || (1.0f32/x) == infinity; }
/*
Predicate: is_negative
Returns true if `x` is a negative number, including -0.0f320 and -Infinity.
*/
pure fn is_negative(x: f32) -> bool
{ ret x < 0.0f32 || (1.0f32/x) == neg_infinity; }
/*
Predicate: is_nonpositive
Returns true if `x` is a negative number, including -0.0f320 and -Infinity.
(This is the same as `f32::negative`.)
*/
pure fn is_nonpositive(x: f32) -> bool {
ret x < 0.0f32 || (1.0f32/x) == neg_infinity;
}
/*
Predicate: nonnegative
Returns true if `x` is a positive number, including +0.0f320 and +Infinity.
(This is the same as `f32::positive`.)
*/
pure fn is_nonnegative(x: f32) -> bool {
ret x > 0.0f32 || (1.0f32/x) == infinity;
}
/*
Predicate: is_zero
Returns true if `x` is a zero number (positive or negative zero)
*/
pure fn is_zero(x: f32) -> bool {
ret x == 0.0f32 || x == -0.0f32;
}
/*
Predicate: is_infinite
Returns true if `x`is an infinite numer
*/
pure fn is_infinite(x: f32) -> bool {
ret x == infinity || x == neg_infinity;
}
/*
Predicate: is_finite
Returns true if `x`is a finite numer
*/
pure fn is_finite(x: f32) -> bool {
ret !(is_NaN(x) || is_infinite(x));
}
// FIXME add is_normal, is_subnormal, and fpclassify
/* Module: consts */
mod consts {
// FIXME replace with mathematical constants from cmath
/*
Const: pi
Archimedes' constant
*/
const pi: f32 = 3.14159265358979323846264338327950288f32;
const pi: f32 = 3.14159265358979323846264338327950288_f32;
/*
Const: frac_pi_2
pi/2.0
*/
const frac_pi_2: f32 = 1.57079632679489661923132169163975144f32;
const frac_pi_2: f32 = 1.57079632679489661923132169163975144_f32;
/*
Const: frac_pi_4
pi/4.0
*/
const frac_pi_4: f32 = 0.785398163397448309615660845819875721f32;
const frac_pi_4: f32 = 0.785398163397448309615660845819875721_f32;
/*
Const: frac_1_pi
1.0/pi
*/
const frac_1_pi: f32 = 0.318309886183790671537767526745028724f32;
const frac_1_pi: f32 = 0.318309886183790671537767526745028724_f32;
/*
Const: frac_2_pi
2.0/pi
*/
const frac_2_pi: f32 = 0.636619772367581343075535053490057448f32;
const frac_2_pi: f32 = 0.636619772367581343075535053490057448_f32;
/*
Const: frac_2_sqrtpi
2.0/sqrt(pi)
*/
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517f32;
const frac_2_sqrtpi: f32 = 1.12837916709551257389615890312154517_f32;
/*
Const: sqrt2
sqrt(2.0)
*/
const sqrt2: f32 = 1.41421356237309504880168872420969808f32;
const sqrt2: f32 = 1.41421356237309504880168872420969808_f32;
/*
Const: frac_1_sqrt2
1.0/sqrt(2.0)
*/
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039f32;
const frac_1_sqrt2: f32 = 0.707106781186547524400844362104849039_f32;
/*
Const: e
Euler's number
*/
const e: f32 = 2.71828182845904523536028747135266250f32;
const e: f32 = 2.71828182845904523536028747135266250_f32;
/*
Const: log2_e
log2(e)
*/
const log2_e: f32 = 1.44269504088896340735992468100189214f32;
const log2_e: f32 = 1.44269504088896340735992468100189214_f32;
/*
Const: log10_e
log10(e)
*/
const log10_e: f32 = 0.434294481903251827651128918916605082f32;
const log10_e: f32 = 0.434294481903251827651128918916605082_f32;
/*
Const: ln_2
ln(2.0)
*/
const ln_2: f32 = 0.693147180559945309417232121458176568f32;
const ln_2: f32 = 0.693147180559945309417232121458176568_f32;
/*
Const: ln_10
ln(10.0)
*/
const ln_10: f32 = 2.30258509299404568401799145468436421f32;
const ln_10: f32 = 2.30258509299404568401799145468436421_f32;
}
pure fn signbit(x: f32) -> int {
if is_negative(x) { ret 1; } else { ret 0; }
}
#[cfg(target_os="linux")]
#[cfg(target_os="macos")]
#[cfg(target_os="win32")]
pure fn logarithm(n: f32, b: f32) -> f32 {
// FIXME check if it is good to use log2 instead of ln here;
// in theory should be faster since the radix is 2
ret log2(n) / log2(b);
}
#[cfg(target_os="freebsd")]
pure fn logarithm(n: f32, b: f32) -> f32 {
ret ln(n) / ln(b);
}
#[cfg(target_os="freebsd")]
pure fn log2(n: f32) -> f32 {
ret ln(n) / ln(2f32)
ret ln(n) / consts::ln_2;
}
//

View file

@ -1,122 +1,267 @@
/*
Module: f64
Floating point operations and constants for `f64`s
This exposes the same operations as `math`, just for `f64` even though
they do not show up in the docs right now!
Floating point operations and constants for `f64`
*/
import cmath::f64::*;
// PORT
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, t;
export consts;
import cmath::c_double::*;
import cmath::c_double_targ_consts::*;
type t = f64;
// These are not defined inside consts:: for consistency with
// the integer types
// PORT check per architecture
// FIXME obtain these in a different way
const radix: uint = 2u;
const mantissa_digits: uint = 53u;
const digits: uint = 15u;
const epsilon: f64 = 2.2204460492503131e-16_f64;
const min_value: f64 = 2.2250738585072014e-308_f64;
const max_value: f64 = 1.7976931348623157e+308_f64;
const min_exp: int = -1021;
const max_exp: int = 1024;
const min_10_exp: int = -307;
const max_10_exp: int = 308;
/* Const: NaN */
const NaN: f64 = 0.0_f64/0.0_f64;
/* Const: infinity */
const infinity: f64 = 1.0_f64/0.0_f64;
/* Const: neg_infinity */
const neg_infinity: f64 = -1.0_f64/0.0_f64;
/* Predicate: isNaN */
pure fn is_NaN(f: f64) -> bool { f != f }
/* Function: add */
pure fn add(x: f64, y: f64) -> f64 { ret x + y; }
/* Function: sub */
pure fn sub(x: f64, y: f64) -> f64 { ret x - y; }
/* Function: mul */
pure fn mul(x: f64, y: f64) -> f64 { ret x * y; }
/* Function: div */
pure fn div(x: f64, y: f64) -> f64 { ret x / y; }
/* Function: rem */
pure fn rem(x: f64, y: f64) -> f64 { ret x % y; }
/* Predicate: lt */
pure fn lt(x: f64, y: f64) -> bool { ret x < y; }
/* Predicate: le */
pure fn le(x: f64, y: f64) -> bool { ret x <= y; }
/* Predicate: eq */
pure fn eq(x: f64, y: f64) -> bool { ret x == y; }
/* Predicate: ne */
pure fn ne(x: f64, y: f64) -> bool { ret x != y; }
/* Predicate: ge */
pure fn ge(x: f64, y: f64) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: f64, y: f64) -> bool { ret x > y; }
/*
Predicate: is_positive
Returns true if `x` is a positive number, including +0.0f640 and +Infinity.
*/
pure fn is_positive(x: f64) -> bool
{ ret x > 0.0f64 || (1.0f64/x) == infinity; }
/*
Predicate: is_negative
Returns true if `x` is a negative number, including -0.0f640 and -Infinity.
*/
pure fn is_negative(x: f64) -> bool
{ ret x < 0.0f64 || (1.0f64/x) == neg_infinity; }
/*
Predicate: is_nonpositive
Returns true if `x` is a negative number, including -0.0f640 and -Infinity.
(This is the same as `f64::negative`.)
*/
pure fn is_nonpositive(x: f64) -> bool {
ret x < 0.0f64 || (1.0f64/x) == neg_infinity;
}
/*
Predicate: is_nonnegative
Returns true if `x` is a positive number, including +0.0f640 and +Infinity.
(This is the same as `f64::positive`.)
*/
pure fn is_nonnegative(x: f64) -> bool {
ret x > 0.0f64 || (1.0f64/x) == infinity;
}
/*
Predicate: is_zero
Returns true if `x` is a zero number (positive or negative zero)
*/
pure fn is_zero(x: f64) -> bool {
ret x == 0.0f64 || x == -0.0f64;
}
/*
Predicate: is_infinite
Returns true if `x`is an infinite numer
*/
pure fn is_infinite(x: f64) -> bool {
ret x == infinity || x == neg_infinity;
}
/*
Predicate: is_finite
Returns true if `x`is a finite numer
*/
pure fn is_finite(x: f64) -> bool {
ret !(is_NaN(x) || is_infinite(x));
}
// FIXME add is_normal, is_subnormal, and fpclassify
/* Module: consts */
mod consts {
// FIXME replace with mathematical constants from cmath
/*
Const: pi
Archimedes' constant
*/
const pi: f64 = 3.14159265358979323846264338327950288f64;
const pi: f64 = 3.14159265358979323846264338327950288_f64;
/*
Const: frac_pi_2
pi/2.0
*/
const frac_pi_2: f64 = 1.57079632679489661923132169163975144f64;
const frac_pi_2: f64 = 1.57079632679489661923132169163975144_f64;
/*
Const: frac_pi_4
pi/4.0
*/
const frac_pi_4: f64 = 0.785398163397448309615660845819875721f64;
const frac_pi_4: f64 = 0.785398163397448309615660845819875721_f64;
/*
Const: frac_1_pi
1.0/pi
*/
const frac_1_pi: f64 = 0.318309886183790671537767526745028724f64;
const frac_1_pi: f64 = 0.318309886183790671537767526745028724_f64;
/*
Const: frac_2_pi
2.0/pi
*/
const frac_2_pi: f64 = 0.636619772367581343075535053490057448f64;
const frac_2_pi: f64 = 0.636619772367581343075535053490057448_f64;
/*
Const: frac_2_sqrtpi
2.0/sqrt(pi)
*/
const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517f64;
const frac_2_sqrtpi: f64 = 1.12837916709551257389615890312154517_f64;
/*
Const: sqrt2
sqrt(2.0)
*/
const sqrt2: f64 = 1.41421356237309504880168872420969808f64;
const sqrt2: f64 = 1.41421356237309504880168872420969808_f64;
/*
Const: frac_1_sqrt2
1.0/sqrt(2.0)
*/
const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039f64;
const frac_1_sqrt2: f64 = 0.707106781186547524400844362104849039_f64;
/*
Const: e
Euler's number
*/
const e: f64 = 2.71828182845904523536028747135266250f64;
const e: f64 = 2.71828182845904523536028747135266250_f64;
/*
Const: log2_e
log2(e)
*/
const log2_e: f64 = 1.44269504088896340735992468100189214f64;
const log2_e: f64 = 1.44269504088896340735992468100189214_f64;
/*
Const: log10_e
log10(e)
*/
const log10_e: f64 = 0.434294481903251827651128918916605082f64;
const log10_e: f64 = 0.434294481903251827651128918916605082_f64;
/*
Const: ln_2
ln(2.0)
*/
const ln_2: f64 = 0.693147180559945309417232121458176568f64;
const ln_2: f64 = 0.693147180559945309417232121458176568_f64;
/*
Const: ln_10
ln(10.0)
*/
const ln_10: f64 = 2.30258509299404568401799145468436421f64;
const ln_10: f64 = 2.30258509299404568401799145468436421_f64;
}
pure fn signbit(x: f64) -> int {
if is_negative(x) { ret 1; } else { ret 0; }
}
#[cfg(target_os="linux")]
#[cfg(target_os="macos")]
#[cfg(target_os="win32")]
pure fn logarithm(n: f64, b: f64) -> f64 {
// FIXME check if it is good to use log2 instead of ln here;
// in theory should be faster since the radix is 2
ret log2(n) / log2(b);
}
#[cfg(target_os="freebsd")]
pure fn logarithm(n: f64, b: f64) -> f64 {
ret ln(n) / ln(b);
}
#[cfg(target_os="freebsd")]
pure fn log2(n: f64) -> f64 {
ret ln(n) / ln(2f64)
ret ln(n) / consts::ln_2;
}
//

View file

@ -2,35 +2,32 @@
Module: float
*/
// Currently this module supports from -lm
// C95 + log2 + log1p + trunc + round + rint
export t;
export consts;
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;
// FIXME find out why these have to be exported explicitly
export to_str_common, to_str_exact, to_str, from_str;
export lt, le, eq, ne, gt, eq;
export NaN, isNaN, infinity, neg_infinity;
export pow_uint_to_uint_as_float;
export min, max;
export add, sub, mul, div;
export positive, negative, nonpositive, nonnegative;
export add, sub, mul, div, rem, lt, le, gt, eq, eq, ne;
export is_positive, is_negative, is_nonpositive, is_nonnegative;
export is_zero, is_infinite, is_finite;
export NaN, is_NaN, infinity, neg_infinity;
export consts;
export logarithm;
export acos, asin, atan, atan2, cbrt, ceil, copysign, cos, cosh;
export erf, erfc, exp, expm1, exp2, abs, abs_sub;
export mul_add, fmax, fmin, nextafter, frexp, hypot, ldexp;
export lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix;
export modf, pow, round, sin, sinh, sqrt, tan, tanh, tgamma, trunc;
export signbit;
import mtypes::m_float;
import ctypes::c_int;
import ptr;
// export when m_float == c_double
export j0, j1, jn, y0, y1, yn;
// PORT this must match in width according to architecture
// PORT This must match in width according to architecture
import f64;
import m_float = f64;
import f64::*;
type t = m_float;
type t = float;
/**
* Section: String Conversions
@ -48,11 +45,12 @@ digits - The number of significant digits
exact - Whether to enforce the exact number of significant digits
*/
fn to_str_common(num: float, digits: uint, exact: bool) -> str {
if is_NaN(num) { ret "NaN"; }
let (num, accum) = num < 0.0 ? (-num, "-") : (num, "");
let trunc = num as uint;
let frac = num - (trunc as float);
accum += uint::str(trunc);
if frac == 0.0 || digits == 0u { ret accum; }
if frac < epsilon || digits == 0u { ret accum; }
accum += ".";
let i = digits;
let epsilon = 1. / pow_uint_to_uint_as_float(10u, i);
@ -283,442 +281,6 @@ fn pow_uint_to_uint_as_float(x: uint, pow: uint) -> float {
}
/* Const: NaN */
const NaN: float = 0./0.;
/* Const: infinity */
const infinity: float = 1./0.;
/* Const: neg_infinity */
const neg_infinity: float = -1./0.;
/* Predicate: isNaN */
pure fn isNaN(f: float) -> bool { f != f }
/* Function: add */
pure fn add(x: float, y: float) -> float { ret x + y; }
/* Function: sub */
pure fn sub(x: float, y: float) -> float { ret x - y; }
/* Function: mul */
pure fn mul(x: float, y: float) -> float { ret x * y; }
/* Function: div */
pure fn div(x: float, y: float) -> float { ret x / y; }
/* Function: rem */
pure fn rem(x: float, y: float) -> float { ret x % y; }
/* Predicate: lt */
pure fn lt(x: float, y: float) -> bool { ret x < y; }
/* Predicate: le */
pure fn le(x: float, y: float) -> bool { ret x <= y; }
/* Predicate: eq */
pure fn eq(x: float, y: float) -> bool { ret x == y; }
/* Predicate: ne */
pure fn ne(x: float, y: float) -> bool { ret x != y; }
/* Predicate: ge */
pure fn ge(x: float, y: float) -> bool { ret x >= y; }
/* Predicate: gt */
pure fn gt(x: float, y: float) -> bool { ret x > y; }
/*
Predicate: positive
Returns true if `x` is a positive number, including +0.0 and +Infinity.
*/
pure fn positive(x: float) -> bool { ret x > 0. || (1./x) == infinity; }
/*
Predicate: negative
Returns true if `x` is a negative number, including -0.0 and -Infinity.
*/
pure fn negative(x: float) -> bool { ret x < 0. || (1./x) == neg_infinity; }
/*
Predicate: nonpositive
Returns true if `x` is a negative number, including -0.0 and -Infinity.
(This is the same as `float::negative`.)
*/
pure fn nonpositive(x: float) -> bool {
ret x < 0. || (1./x) == neg_infinity;
}
/*
Predicate: nonnegative
Returns true if `x` is a positive number, including +0.0 and +Infinity.
(This is the same as `float::positive`.)
*/
pure fn nonnegative(x: float) -> bool {
ret x > 0. || (1./x) == infinity;
}
/*
Module: consts
*/
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;
}
// FIXME min/max type specialize via libm when overloading works
// (in theory fmax/fmin, fmaxf, fminf /should/ be faster)
/*
Function: min
Returns the minimum of two values
*/
pure fn min<T: copy>(x: T, y: T) -> T { x < y ? x : y }
/*
Function: max
Returns the maximum of two values
*/
pure fn max<T: copy>(x: T, y: T) -> T { x < y ? y : x }
/*
Function: acos
Returns the arccosine of an angle (measured in rad)
*/
pure fn acos(x: float) -> float
{ ret m_float::acos(x as m_float) as float }
/*
Function: asin
Returns the arcsine of an angle (measured in rad)
*/
pure fn asin(x: float) -> float
{ ret m_float::asin(x as m_float) as float }
/*
Function: atan
Returns the arctangents of an angle (measured in rad)
*/
pure fn atan(x: float) -> float
{ ret m_float::atan(x as m_float) as float }
/*
Function: atan2
Returns the arctangent of an angle (measured in rad)
*/
pure fn atan2(y: float, x: float) -> float
{ ret m_float::atan2(y as m_float, x as m_float) as float }
/*
Function: ceil
Returns the smallest integral value less than or equal to `n`
*/
pure fn ceil(n: float) -> float
{ ret m_float::ceil(n as m_float) as float }
/*
Function: cos
Returns the cosine of an angle `x` (measured in rad)
*/
pure fn cos(x: float) -> float
{ ret m_float::cos(x as m_float) as float }
/*
Function: cosh
Returns the hyperbolic cosine of `x`
*/
pure fn cosh(x: float) -> float
{ ret m_float::cosh(x as m_float) as float }
/*
Function: exp
Returns `consts::e` to the power of `n*
*/
pure fn exp(n: float) -> float
{ ret m_float::exp(n as m_float) as float }
/*
Function: abs
Returns the absolute value of `n`
*/
pure fn abs(n: float) -> float
{ ret m_float::abs(n as m_float) as float }
/*
Function: floor
Returns the largest integral value less than or equal to `n`
*/
pure fn floor(n: float) -> float
{ ret m_float::floor(n as m_float) as float }
/*
Function: fmod
Returns the floating-point remainder of `x/y`
*/
pure fn fmod(x: float, y: float) -> float
{ ret m_float::fmod(x as m_float, y as m_float) as float }
/*
Function: ln
Returns the natural logaritm of `n`
*/
pure fn ln(n: float) -> float
{ ret m_float::ln(n as m_float) as float }
/*
Function: ldexp
Returns `x` multiplied by 2 to the power of `n`
*/
pure fn ldexp(n: float, i: int) -> float
{ ret m_float::ldexp(n as m_float, i as c_int) as float }
/*
Function: ln1p
Returns the natural logarithm of `1+n` accurately,
even for very small values of `n`
*/
pure fn ln1p(n: float) -> float
{ ret m_float::ln1p(n as m_float) as float }
/*
Function: log10
Returns the logarithm to base 10 of `n`
*/
pure fn log10(n: float) -> float
{ ret m_float::log10(n as m_float) as float }
/*
Function: log2
Returns the logarithm to base 2 of `n`
*/
pure fn log2(n: float) -> float
{ ret m_float::log2(n as m_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`
*/
#[no(warn_trivial_casts)] // FIXME Implement
pure fn modf(n: float, &iptr: float) -> float { unsafe {
ret m_float::modf(n as m_float, ptr::addr_of(iptr) as *m_float) as float
} }
/*
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
{ ret m_float::frexp(n as m_float, exp) as float }
/*
Function: pow
*/
pure fn pow(v: float, e: float) -> float
{ ret m_float::pow(v as m_float, e as m_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
{ ret m_float::rint(x as m_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
{ ret m_float::round(x as m_float) as float }
/*
Function: sin
Returns the sine of an angle `x` (measured in rad)
*/
pure fn sin(x: float) -> float
{ ret m_float::sin(x as m_float) as float }
/*
Function: sinh
Returns the hyperbolic sine of an angle `x` (measured in rad)
*/
pure fn sinh(x: float) -> float
{ ret m_float::sinh(x as m_float) as float }
/*
Function: sqrt
Returns the square root of `x`
*/
pure fn sqrt(x: float) -> float
{ ret m_float::sqrt(x as m_float) as float }
/*
Function: tan
Returns the tangent of an angle `x` (measured in rad)
*/
pure fn tan(x: float) -> float
{ ret m_float::tan(x as m_float) as float }
/*
Function: tanh
Returns the hyperbolic tangent of an angle `x` (measured in rad)
*/
pure fn tanh(x: float) -> float
{ ret m_float::tanh(x as m_float) as float }
/*
Function: trunc
Returns the integral value nearest to but no larger in magnitude than `x`
*/
pure fn trunc(x: float) -> float
{ ret m_float::trunc(x as m_float) as float }
//
// Local Variables:
// mode: rust
@ -728,3 +290,8 @@ pure fn trunc(x: float) -> float
// buffer-file-coding-system: utf-8-unix
// End:
//

18
src/libcore/math.rs Normal file
View file

@ -0,0 +1,18 @@
// Generic functions that have been defined for all numeric types
//
// (may very well go away again soon)
/*
Function: min
Returns the minimum of two values
*/
pure fn min<T: copy>(x: T, y: T) -> T { x < y ? x : y }
/*
Function: max
Returns the maximum of two values
*/
pure fn max<T: copy>(x: T, y: T) -> T { x < y ? y : x }

View file

@ -1,62 +0,0 @@
/*
Module: mtypes
Machine type equivalents of rust int, uint, float, and complex.
Types useful for interop with C when writing bindings that exist
for different types (float, f32, f64, ...; cf float.rs for an example)
*/
// PORT Change this when porting to a new architecture
/*
Type: m_int
Machine type equivalent of an int
*/
#[cfg(target_arch="x86")]
type m_int = i32;
#[cfg(target_arch="x86_64")]
type m_int = i64;
// PORT Change this when porting to a new architecture
/*
Type: m_uint
Machine type equivalent of a uint
*/
#[cfg(target_arch="x86")]
type m_uint = u32;
#[cfg(target_arch="x86_64")]
type m_uint = u64;
// PORT *must* match with "import m_float = fXX" in std::math per arch
/*
Type: m_float
Machine type equivalent of a float
*/
type m_float = f64;
// PORT *must* match "import m_complex = ..." in std::complex per arch
/*
FIXME Type m_complex
Machine type representing a complex value that uses floats for
both the real and the imaginary part.
*/
// type m_complex = complex_c64::t;
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//

View file

@ -1,64 +0,0 @@
/*
Module: mtypes
Machine type equivalents of rust int, uint, float, and complex.
Types useful for interop with C when writing bindings that exist
for different types (float, f32, f64, ...; cf float.rs for an example)
*/
export m_int, m_uint, m_float;
// PORT Change this when porting to a new architecture
/*
Type: m_int
Machine type equivalent of an int
*/
#[cfg(target_arch="x86")]
type m_int = i32;
#[cfg(target_arch="x86_64")]
type m_int = i64;
// PORT Change this when porting to a new architecture
/*
Type: m_uint
Machine type equivalent of a uint
*/
#[cfg(target_arch="x86")]
type m_uint = u32;
#[cfg(target_arch="x86_64")]
type m_uint = u64;
// PORT *must* match with "import m_float = fXX" in std::math per arch
/*
Type: m_float
Machine type equivalent of a float
*/
type m_float = f64;
// PORT *must* match "import m_complex = ..." in std::complex per arch
/*
FIXME Type m_complex
Machine type representing a complex value that uses floats for
both the real and the imaginary part.
*/
// type m_complex = complex_c64::t;
//
// Local Variables:
// mode: rust
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:
//

View file

@ -26,10 +26,6 @@ The following operations are algorithmically faster in ropes:
*/
import core::option;
/*
Type: rope
@ -1103,7 +1099,7 @@ mod node {
right : right,
char_len: char_len(left) + char_len(right),
byte_len: byte_len(left) + byte_len(right),
height: float::max(height(left), height(right)) + 1u
height: math::max(height(left), height(right)) + 1u
})
}

View file

@ -1,6 +1,6 @@
// Allow block arguments with ternary... why not, no chance of ambig.
fn main() {
let v = [-1f, 1f];
let foo = vec::any(v) { |e| float::negative(e) } ? true : false;
let foo = vec::any(v) { |e| float::is_negative(e) } ? true : false;
assert foo;
}

View file

@ -8,28 +8,28 @@ fn main() {
}
// Usable at all:
let any_negative = vec::any(v) { |e| float::negative(e) };
let any_negative = vec::any(v) { |e| float::is_negative(e) };
assert any_negative;
// Higher precedence than assignments:
any_negative = vec::any(v) { |e| float::negative(e) };
any_negative = vec::any(v) { |e| float::is_negative(e) };
assert any_negative;
// Higher precedence than unary operations:
let abs_v = vec::map(v) { |e| float::abs(e) };
assert vec::all(abs_v) { |e| float::nonnegative(e) };
assert !vec::any(abs_v) { |e| float::negative(e) };
assert vec::all(abs_v) { |e| float::is_nonnegative(e) };
assert !vec::any(abs_v) { |e| float::is_negative(e) };
// Usable in funny statement-like forms:
if !vec::any(v) { |e| float::positive(e) } {
if !vec::any(v) { |e| float::is_positive(e) } {
assert false;
}
alt vec::all(v) { |e| float::negative(e) } {
alt vec::all(v) { |e| float::is_negative(e) } {
true { fail "incorrect answer."; }
false { }
}
alt 3 {
_ when vec::any(v) { |e| float::negative(e) } {
_ when vec::any(v) { |e| float::is_negative(e) } {
}
_ {
fail "wrong answer.";
@ -46,7 +46,7 @@ fn main() {
// They are not allowed as the tail of a block without parentheses:
let w =
if true { vec::any(abs_v, { |e| float::nonnegative(e) }) }
if true { vec::any(abs_v, { |e| float::is_nonnegative(e) }) }
else { false };
assert w;
}

View file

@ -3,7 +3,7 @@ import float;
fn main() {
let nan = float::NaN;
assert(float::isNaN(nan));
assert(float::is_NaN(nan));
let inf = float::infinity;
assert(-inf == float::neg_infinity);
@ -61,22 +61,22 @@ fn main() {
assert(!(-inf < nan));
assert(!(-nan < nan));
assert(float::isNaN(nan + inf));
assert(float::isNaN(nan + -inf));
assert(float::isNaN(nan + 0.));
assert(float::isNaN(nan + 1.));
assert(float::isNaN(nan * 1.));
assert(float::isNaN(nan / 1.));
assert(float::isNaN(nan / 0.));
assert(float::isNaN(0. / 0.));
assert(float::isNaN(-inf + inf));
assert(float::isNaN(inf - inf));
assert(float::is_NaN(nan + inf));
assert(float::is_NaN(nan + -inf));
assert(float::is_NaN(nan + 0.));
assert(float::is_NaN(nan + 1.));
assert(float::is_NaN(nan * 1.));
assert(float::is_NaN(nan / 1.));
assert(float::is_NaN(nan / 0.));
assert(float::is_NaN(0. / 0.));
assert(float::is_NaN(-inf + inf));
assert(float::is_NaN(inf - inf));
assert(!float::isNaN(-1.));
assert(!float::isNaN(0.));
assert(!float::isNaN(0.1));
assert(!float::isNaN(1.));
assert(!float::isNaN(inf));
assert(!float::isNaN(-inf));
assert(!float::isNaN(1./-inf));
assert(!float::is_NaN(-1.));
assert(!float::is_NaN(0.));
assert(!float::is_NaN(0.1));
assert(!float::is_NaN(1.));
assert(!float::is_NaN(inf));
assert(!float::is_NaN(-inf));
assert(!float::is_NaN(1./-inf));
}

Binary file not shown.

View file

@ -26,60 +26,60 @@ fn test_from_str() {
assert ( float::from_str(" -.5 ") == -0.5 );
assert ( float::from_str(" -5 ") == -5. );
assert ( float::isNaN(float::from_str("x")) );
assert ( float::is_NaN(float::from_str("x")) );
assert ( float::from_str(" ") == 0. );
assert ( float::from_str(" ") == 0. );
assert ( float::from_str(" 0.5") == 0.5 );
assert ( float::from_str(" 0.5 ") == 0.5 );
assert ( float::from_str(" .1 ") == 0.1 );
assert ( float::isNaN(float::from_str("e")) );
assert ( float::isNaN(float::from_str("E")) );
assert ( float::isNaN(float::from_str("E1")) );
assert ( float::isNaN(float::from_str("1e1e1")) );
assert ( float::isNaN(float::from_str("1e1.1")) );
assert ( float::isNaN(float::from_str("1e1-1")) );
assert ( float::is_NaN(float::from_str("e")) );
assert ( float::is_NaN(float::from_str("E")) );
assert ( float::is_NaN(float::from_str("E1")) );
assert ( float::is_NaN(float::from_str("1e1e1")) );
assert ( float::is_NaN(float::from_str("1e1.1")) );
assert ( float::is_NaN(float::from_str("1e1-1")) );
}
#[test]
fn test_positive() {
assert(float::positive(float::infinity));
assert(float::positive(1.));
assert(float::positive(0.));
assert(!float::positive(-1.));
assert(!float::positive(float::neg_infinity));
assert(!float::positive(1./float::neg_infinity));
assert(!float::positive(float::NaN));
assert(float::is_positive(float::infinity));
assert(float::is_positive(1.));
assert(float::is_positive(0.));
assert(!float::is_positive(-1.));
assert(!float::is_positive(float::neg_infinity));
assert(!float::is_positive(1./float::neg_infinity));
assert(!float::is_positive(float::NaN));
}
#[test]
fn test_negative() {
assert(!float::negative(float::infinity));
assert(!float::negative(1.));
assert(!float::negative(0.));
assert(float::negative(-1.));
assert(float::negative(float::neg_infinity));
assert(float::negative(1./float::neg_infinity));
assert(!float::negative(float::NaN));
assert(!float::is_negative(float::infinity));
assert(!float::is_negative(1.));
assert(!float::is_negative(0.));
assert(float::is_negative(-1.));
assert(float::is_negative(float::neg_infinity));
assert(float::is_negative(1./float::neg_infinity));
assert(!float::is_negative(float::NaN));
}
#[test]
fn test_nonpositive() {
assert(!float::nonpositive(float::infinity));
assert(!float::nonpositive(1.));
assert(!float::nonpositive(0.));
assert(float::nonpositive(-1.));
assert(float::nonpositive(float::neg_infinity));
assert(float::nonpositive(1./float::neg_infinity));
assert(!float::nonpositive(float::NaN));
assert(!float::is_nonpositive(float::infinity));
assert(!float::is_nonpositive(1.));
assert(!float::is_nonpositive(0.));
assert(float::is_nonpositive(-1.));
assert(float::is_nonpositive(float::neg_infinity));
assert(float::is_nonpositive(1./float::neg_infinity));
assert(!float::is_nonpositive(float::NaN));
}
#[test]
fn test_nonnegative() {
assert(float::nonnegative(float::infinity));
assert(float::nonnegative(1.));
assert(float::nonnegative(0.));
assert(!float::nonnegative(-1.));
assert(!float::nonnegative(float::neg_infinity));
assert(!float::nonnegative(1./float::neg_infinity));
assert(!float::nonnegative(float::NaN));
assert(float::is_nonnegative(float::infinity));
assert(float::is_nonnegative(1.));
assert(float::is_nonnegative(0.));
assert(!float::is_nonnegative(-1.));
assert(!float::is_nonnegative(float::neg_infinity));
assert(!float::is_nonnegative(1./float::neg_infinity));
assert(!float::is_nonnegative(float::NaN));
}

View file

@ -2,6 +2,7 @@ import core::*;
use std;
import math::{min, max};
import float::*;
import float;
import c_int = ctypes::c_int;
@ -18,6 +19,7 @@ fn test_max_min() {
// FIXME use macros to execute the tests below for all float types
/*
#[test]
fn test_trig() {
assert sin(0.0) == 0.0;
@ -297,4 +299,6 @@ fn test_log_functions() {
assert ln1p(-1.0) == float::neg_infinity;
assert float::isNaN(ln1p(-2.0f));
assert ln1p(float::infinity) == float::infinity;
}
}
*/