core: added support for bessel functions

This commit is contained in:
Stefan Plantikow 2011-12-28 02:20:14 +01:00
parent a72b1413ed
commit 16405386f0
6 changed files with 104 additions and 92 deletions

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,6 +1,5 @@
export c_double;
export c_float;
export bessel;
import ctypes::c_int;
import ctypes::c_float;
@ -57,6 +56,16 @@ native mod 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"]
@ -83,7 +92,7 @@ native mod c_float {
#[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 @@ native mod c_float {
&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 @@ native mod c_float {
#[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

View File

@ -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;

View File

@ -3,20 +3,43 @@ Module: f32
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

View File

@ -10,14 +10,36 @@ import cmath::c_double::*;
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

View File

@ -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 @@ 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 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);