From 16405386f0a843167e234d8d54855a537b0f261d Mon Sep 17 00:00:00 2001 From: Stefan Plantikow Date: Wed, 28 Dec 2011 02:20:14 +0100 Subject: [PATCH] core: added support for bessel functions --- src/libcore/bessel.rs | 10 ++++++ src/libcore/cmath.rs | 27 +++++++-------- src/libcore/core.rc | 3 +- src/libcore/f32.rs | 76 ++++++++++++++++++++++--------------------- src/libcore/f64.rs | 75 +++++++++++++++++++++--------------------- src/libcore/float.rs | 5 +-- 6 files changed, 104 insertions(+), 92 deletions(-) create mode 100644 src/libcore/bessel.rs diff --git a/src/libcore/bessel.rs b/src/libcore/bessel.rs new file mode 100644 index 00000000000..85f8a8ed49c --- /dev/null +++ b/src/libcore/bessel.rs @@ -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::*; + diff --git a/src/libcore/cmath.rs b/src/libcore/cmath.rs index 73cb9af8bdb..dd922ddf1bc 100644 --- a/src/libcore/cmath.rs +++ b/src/libcore/cmath.rs @@ -1,6 +1,5 @@ export c_double; export c_float; -export bessel; import ctypes::c_int; import ctypes::c_float; @@ -57,6 +56,16 @@ 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"] @@ -83,7 +92,7 @@ #[link_name="fabsf"] pure fn abs(n: c_float) -> c_float; #[link_name="fdimf"] pure fn sub_pos(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_double, + #[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; @@ -97,7 +106,7 @@ &sign: c_int) -> c_float; #[link_name="logf"] pure fn ln(n: c_float) -> c_float; #[link_name="logbf"] pure fn logb(n: c_float) -> c_float; - #[link_name="log1p"] pure fn ln1p(n: c_double) -> c_double; + #[link_name="log1pf"] pure fn ln1p(n: c_float) -> c_float; #[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 ilogb(n: c_float) -> c_int; @@ -116,18 +125,6 @@ #[link_name="truncf"] pure fn trunc(n: c_float) -> c_float; } -#[link_name = "m"] -#[abi = "cdecl"] -native mod bessel { - 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; -} - // // Local Variables: // mode: rust diff --git a/src/libcore/core.rc b/src/libcore/core.rc index 6315d32b6d4..e1e10789e4b 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -7,7 +7,7 @@ #[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, sys, unsafe, comm, task; @@ -18,6 +18,7 @@ export extfmt; mod box; mod char; mod float; +mod bessel; mod f32; mod f64; mod int; diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 192d3f14ea4..26ec20a2ed9 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -3,20 +3,43 @@ Floating point operations and constants for `f32` */ + // PORT import cmath::c_float::*; type t = f32; + +// These are not defined inside consts:: for consistency with +// the integer types + +// PORT check per architecture + +const radix: uint = 2u; + +const mantissa_digits: uint = 24u; +const digits: uint = 6u; + +const epsilon: f32 = 1.19209290e-07_f32; + +const min_value: f32 = 1.17549435e-38_f32; +const max_value: f32 = 3.40282347e+38_f32; + +const min_exp: int = -125; +const max_exp: int = 128; + +const min_10_exp: int = -37; +const max_10_exp: int = 38; + /* Const: NaN */ -const NaN: f32 = 0.0f32/0.0f32; +const NaN: f32 = 0.0_f32/0.0_f32; /* Const: infinity */ -const infinity: f32 = 1.0f32/0.0f32; +const infinity: f32 = 1.0_f32/0.0_f32; /* Const: neg_infinity */ -const neg_infinity: f32 = -1.0f32/0.0f32; +const neg_infinity: f32 = -1.0_f32/0.0_f32; /* Predicate: isNaN */ pure fn isNaN(f: f32) -> bool { f != f } @@ -98,114 +121,93 @@ mod consts { 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; } -// These are not defined inside consts:: for consistency with -// the integer types - -// PORT check per architecture - -const radix: uint = 2u; - -const mantissa_digits: uint = 24u; -const digits: uint = 6u; - -const epsilon: f32 = 1.19209290e-07f32; - -const min_value: f32 = 1.17549435e-38f32; -const max_value: f32 = 3.40282347e+38f32; - -const min_exp: int = -125; -const max_exp: int = 128; - -const min_10_exp: int = -37; -const max_10_exp: int = 38; - // // Local Variables: // mode: rust diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index 6413b5c5c16..c7ee7e1a5db 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -10,14 +10,36 @@ type t = f64; + +// These are not defined inside consts:: for consistency with +// the integer types + +// PORT check per architecture + +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.0f64/0.0f64; +const NaN: f64 = 0.0_f64/0.0_f64; /* Const: infinity */ -const infinity: f64 = 1.0f64/0.0f64; +const infinity: f64 = 1.0_f64/0.0_f64; /* Const: neg_infinity */ -const neg_infinity: f64 = -1.0f64/0.0f64; +const neg_infinity: f64 = -1.0_f64/0.0_f64; /* Predicate: isNaN */ pure fn isNaN(f: f64) -> bool { f != f } @@ -99,114 +121,93 @@ mod consts { 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; } -// These are not defined inside consts:: for consistency with -// the integer types - -// PORT check per architecture - -const radix: uint = 2u; - -const mantissa_digits: uint = 53u; -const digits: uint = 15u; - -const epsilon: f64 = 2.2204460492503131e-16f64; - -const min_value: f64 = 2.2250738585072014e-308f64; -const max_value: f64 = 1.7976931348623157e+308f64; - -const min_exp: int = -1021; -const max_exp: int = 1024; - -const min_10_exp: int = -307; -const max_10_exp: int = 308; - // // Local Variables: // mode: rust diff --git a/src/libcore/float.rs b/src/libcore/float.rs index b482f169fc2..cf8ea28a00b 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -2,7 +2,7 @@ Module: float */ -// PORT This must match in width according to architecture +// PORT this must match in width according to architecture import m_float = f64; import m_float::*; @@ -25,11 +25,12 @@ exact - Whether to enforce the exact number of significant digits */ fn to_str_common(num: float, digits: uint, exact: bool) -> str { + if isNaN(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); -- GitLab