2012-02-12 01:49:13 -06:00
|
|
|
#[doc = "Operations and constants for `float`"];
|
2012-02-12 01:18:26 -06:00
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
/*
|
|
|
|
Module: float
|
|
|
|
*/
|
|
|
|
|
2012-01-05 07:46:14 -06:00
|
|
|
// FIXME find out why these have to be exported explicitly
|
2011-12-13 19:52:02 -06:00
|
|
|
|
2012-01-05 10:11:34 -06:00
|
|
|
export to_str_common, to_str_exact, to_str, from_str;
|
2012-01-05 07:46:14 -06:00
|
|
|
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;
|
2011-12-13 19:52:02 -06:00
|
|
|
export consts;
|
2012-01-05 07:46:14 -06:00
|
|
|
export logarithm;
|
2012-02-08 00:02:55 -06:00
|
|
|
export acos, asin, atan, atan2, cbrt, ceil, copysign, cos, cosh, floor;
|
2012-01-05 07:46:14 -06:00
|
|
|
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;
|
2012-01-05 10:11:34 -06:00
|
|
|
export signbit;
|
2012-02-24 02:07:05 -06:00
|
|
|
export pow_with_uint;
|
2012-01-05 07:46:14 -06:00
|
|
|
|
|
|
|
// export when m_float == c_double
|
|
|
|
|
|
|
|
export j0, j1, jn, y0, y1, yn;
|
|
|
|
|
2011-12-27 19:20:14 -06:00
|
|
|
// PORT this must match in width according to architecture
|
2011-12-22 08:19:43 -06:00
|
|
|
|
2011-12-13 19:52:02 -06:00
|
|
|
import m_float = f64;
|
2012-01-05 07:46:14 -06:00
|
|
|
import f64::*;
|
2011-12-22 19:31:24 -06:00
|
|
|
|
|
|
|
type t = float;
|
2011-12-13 19:52:02 -06:00
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
/**
|
|
|
|
* Section: String Conversions
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: to_str_common
|
|
|
|
|
|
|
|
Converts a float to a string
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
num - The float value
|
|
|
|
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 {
|
2012-01-05 07:46:14 -06:00
|
|
|
if is_NaN(num) { ret "NaN"; }
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut (num, accum) = if num < 0.0 { (-num, "-") } else { (num, "") };
|
2011-12-13 18:25:51 -06:00
|
|
|
let trunc = num as uint;
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut frac = num - (trunc as float);
|
2011-12-13 18:25:51 -06:00
|
|
|
accum += uint::str(trunc);
|
2012-02-21 16:25:31 -06:00
|
|
|
if (frac < epsilon && !exact) || digits == 0u { ret accum; }
|
2011-12-13 18:25:51 -06:00
|
|
|
accum += ".";
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut i = digits;
|
|
|
|
let mut epsilon = 1. / pow_with_uint(10u, i);
|
2011-12-13 18:25:51 -06:00
|
|
|
while i > 0u && (frac >= epsilon || exact) {
|
|
|
|
frac *= 10.0;
|
|
|
|
epsilon *= 10.0;
|
|
|
|
let digit = frac as uint;
|
|
|
|
accum += uint::str(digit);
|
|
|
|
frac -= digit as float;
|
|
|
|
i -= 1u;
|
|
|
|
}
|
|
|
|
ret accum;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: to_str
|
|
|
|
|
|
|
|
Converts a float to a string with exactly the number of provided significant
|
|
|
|
digits
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
num - The float value
|
|
|
|
digits - The number of significant digits
|
|
|
|
*/
|
|
|
|
fn to_str_exact(num: float, digits: uint) -> str {
|
|
|
|
to_str_common(num, digits, true)
|
|
|
|
}
|
|
|
|
|
2012-02-21 16:25:31 -06:00
|
|
|
#[test]
|
|
|
|
fn test_to_str_exact_do_decimal() {
|
|
|
|
let s = to_str_exact(5.0, 4u);
|
|
|
|
assert s == "5.0000";
|
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
/*
|
|
|
|
Function: to_str
|
|
|
|
|
|
|
|
Converts a float to a string with a maximum number of significant digits
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
|
|
|
num - The float value
|
|
|
|
digits - The number of significant digits
|
|
|
|
*/
|
|
|
|
fn to_str(num: float, digits: uint) -> str {
|
|
|
|
to_str_common(num, digits, false)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Function: from_str
|
|
|
|
|
|
|
|
Convert a string to a float
|
|
|
|
|
|
|
|
This function accepts strings such as
|
|
|
|
* "3.14"
|
|
|
|
* "+3.14", equivalent to "3.14"
|
|
|
|
* "-3.14"
|
|
|
|
* "2.5E10", or equivalently, "2.5e10"
|
|
|
|
* "2.5E-10"
|
|
|
|
* "", or, equivalently, "." (understood as 0)
|
|
|
|
* "5."
|
|
|
|
* ".5", or, equivalently, "0.5"
|
|
|
|
|
|
|
|
Leading and trailing whitespace are ignored.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
2012-02-22 06:18:15 -06:00
|
|
|
num - A string
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
2012-02-22 06:18:15 -06:00
|
|
|
none if the string did not represent a valid number.
|
|
|
|
Otherwise, some(n) where n is the floating-point
|
|
|
|
number represented by [num].
|
2011-12-13 18:25:51 -06:00
|
|
|
*/
|
2012-02-22 06:18:15 -06:00
|
|
|
fn from_str(num: str) -> option<float> {
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut pos = 0u; //Current byte position in the string.
|
|
|
|
//Used to walk the string in O(n).
|
|
|
|
let len = str::len(num); //Length of the string, in bytes.
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-02-22 06:18:15 -06:00
|
|
|
if len == 0u { ret none; }
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut total = 0f; //Accumulated result
|
|
|
|
let mut c = 'z'; //Latest char.
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
//The string must start with one of the following characters.
|
|
|
|
alt str::char_at(num, 0u) {
|
|
|
|
'-' | '+' | '0' to '9' | '.' {}
|
2012-02-22 06:18:15 -06:00
|
|
|
_ { ret none; }
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//Determine if first char is '-'/'+'. Set [pos] and [neg] accordingly.
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut neg = false; //Sign of the result
|
2011-12-13 18:25:51 -06:00
|
|
|
alt str::char_at(num, 0u) {
|
|
|
|
'-' {
|
|
|
|
neg = true;
|
|
|
|
pos = 1u;
|
|
|
|
}
|
|
|
|
'+' {
|
|
|
|
pos = 1u;
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Examine the following chars until '.', 'e', 'E'
|
|
|
|
while(pos < len) {
|
|
|
|
let char_range = str::char_range_at(num, pos);
|
|
|
|
c = char_range.ch;
|
|
|
|
pos = char_range.next;
|
|
|
|
alt c {
|
|
|
|
'0' to '9' {
|
|
|
|
total = total * 10f;
|
|
|
|
total += ((c as int) - ('0' as int)) as float;
|
|
|
|
}
|
|
|
|
'.' | 'e' | 'E' {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ {
|
2012-02-22 06:18:15 -06:00
|
|
|
ret none;
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == '.' {//Examine decimal part
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut decimal = 1f;
|
2011-12-13 18:25:51 -06:00
|
|
|
while(pos < len) {
|
|
|
|
let char_range = str::char_range_at(num, pos);
|
|
|
|
c = char_range.ch;
|
|
|
|
pos = char_range.next;
|
|
|
|
alt c {
|
|
|
|
'0' | '1' | '2' | '3' | '4' | '5' | '6'| '7' | '8' | '9' {
|
2011-12-16 03:11:00 -06:00
|
|
|
decimal /= 10f;
|
2011-12-13 18:25:51 -06:00
|
|
|
total += (((c as int) - ('0' as int)) as float)*decimal;
|
|
|
|
}
|
|
|
|
'e' | 'E' {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ {
|
2012-02-22 06:18:15 -06:00
|
|
|
ret none;
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 'e') | (c == 'E') {//Examine exponent
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut exponent = 0u;
|
|
|
|
let mut neg_exponent = false;
|
2011-12-13 18:25:51 -06:00
|
|
|
if(pos < len) {
|
|
|
|
let char_range = str::char_range_at(num, pos);
|
|
|
|
c = char_range.ch;
|
|
|
|
alt c {
|
|
|
|
'+' {
|
|
|
|
pos = char_range.next;
|
|
|
|
}
|
|
|
|
'-' {
|
|
|
|
pos = char_range.next;
|
|
|
|
neg_exponent = true;
|
|
|
|
}
|
|
|
|
_ {}
|
|
|
|
}
|
|
|
|
while(pos < len) {
|
|
|
|
let char_range = str::char_range_at(num, pos);
|
|
|
|
c = char_range.ch;
|
|
|
|
alt c {
|
|
|
|
'0' | '1' | '2' | '3' | '4' | '5' | '6'| '7' | '8' | '9' {
|
|
|
|
exponent *= 10u;
|
|
|
|
exponent += ((c as uint) - ('0' as uint));
|
|
|
|
}
|
|
|
|
_ {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pos = char_range.next;
|
|
|
|
}
|
2012-02-24 02:07:05 -06:00
|
|
|
let multiplier = pow_with_uint(10u, exponent);
|
2011-12-13 18:25:51 -06:00
|
|
|
//Note: not [int::pow], otherwise, we'll quickly
|
|
|
|
//end up with a nice overflow
|
|
|
|
if neg_exponent {
|
|
|
|
total = total / multiplier;
|
|
|
|
} else {
|
|
|
|
total = total * multiplier;
|
|
|
|
}
|
|
|
|
} else {
|
2012-02-22 06:18:15 -06:00
|
|
|
ret none;
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pos < len) {
|
2012-02-22 06:18:15 -06:00
|
|
|
ret none;
|
2011-12-13 18:25:51 -06:00
|
|
|
} else {
|
|
|
|
if(neg) {
|
|
|
|
total *= -1f;
|
|
|
|
}
|
2012-02-22 06:18:15 -06:00
|
|
|
ret some(total);
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Section: Arithmetics
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2012-02-24 02:07:05 -06:00
|
|
|
Function: pow_with_uint
|
2011-12-13 18:25:51 -06:00
|
|
|
|
|
|
|
Compute the exponentiation of an integer by another integer as a float.
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
x - The base.
|
|
|
|
pow - The exponent.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
<NaN> of both `x` and `pow` are `0u`, otherwise `x^pow`.
|
|
|
|
*/
|
2012-02-24 02:07:05 -06:00
|
|
|
fn pow_with_uint(base: uint, pow: uint) -> float {
|
|
|
|
if base == 0u {
|
2011-12-13 18:25:51 -06:00
|
|
|
if pow == 0u {
|
|
|
|
ret NaN;
|
|
|
|
}
|
|
|
|
ret 0.;
|
|
|
|
}
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut my_pow = pow;
|
|
|
|
let mut total = 1f;
|
|
|
|
let mut multiplier = base as float;
|
2011-12-13 18:25:51 -06:00
|
|
|
while (my_pow > 0u) {
|
|
|
|
if my_pow % 2u == 1u {
|
|
|
|
total = total * multiplier;
|
|
|
|
}
|
|
|
|
my_pow /= 2u;
|
|
|
|
multiplier *= multiplier;
|
|
|
|
}
|
|
|
|
ret total;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
|
|
|
fn test_from_str() {
|
2012-02-22 06:18:15 -06:00
|
|
|
assert from_str("3") == some(3.);
|
|
|
|
assert from_str("3") == some(3.);
|
|
|
|
assert from_str("3.14") == some(3.14);
|
|
|
|
assert from_str("+3.14") == some(3.14);
|
|
|
|
assert from_str("-3.14") == some(-3.14);
|
|
|
|
assert from_str("2.5E10") == some(25000000000.);
|
|
|
|
assert from_str("2.5e10") == some(25000000000.);
|
|
|
|
assert from_str("25000000000.E-10") == some(2.5);
|
|
|
|
assert from_str(".") == some(0.);
|
|
|
|
assert from_str(".e1") == some(0.);
|
|
|
|
assert from_str(".e-1") == some(0.);
|
|
|
|
assert from_str("5.") == some(5.);
|
|
|
|
assert from_str(".5") == some(0.5);
|
|
|
|
assert from_str("0.5") == some(0.5);
|
|
|
|
assert from_str("0.5") == some(0.5);
|
|
|
|
assert from_str("0.5") == some(0.5);
|
|
|
|
assert from_str("-.5") == some(-0.5);
|
|
|
|
assert from_str("-.5") == some(-0.5);
|
|
|
|
assert from_str("-5") == some(-5.);
|
|
|
|
|
|
|
|
assert from_str("") == none;
|
|
|
|
assert from_str("x") == none;
|
|
|
|
assert from_str(" ") == none;
|
|
|
|
assert from_str(" ") == none;
|
|
|
|
assert from_str("e") == none;
|
|
|
|
assert from_str("E") == none;
|
|
|
|
assert from_str("E1") == none;
|
|
|
|
assert from_str("1e1e1") == none;
|
|
|
|
assert from_str("1e1.1") == none;
|
|
|
|
assert from_str("1e1-1") == none;
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_positive() {
|
|
|
|
assert(is_positive(infinity));
|
|
|
|
assert(is_positive(1.));
|
|
|
|
assert(is_positive(0.));
|
|
|
|
assert(!is_positive(-1.));
|
|
|
|
assert(!is_positive(neg_infinity));
|
|
|
|
assert(!is_positive(1./neg_infinity));
|
|
|
|
assert(!is_positive(NaN));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_negative() {
|
|
|
|
assert(!is_negative(infinity));
|
|
|
|
assert(!is_negative(1.));
|
|
|
|
assert(!is_negative(0.));
|
|
|
|
assert(is_negative(-1.));
|
|
|
|
assert(is_negative(neg_infinity));
|
|
|
|
assert(is_negative(1./neg_infinity));
|
|
|
|
assert(!is_negative(NaN));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonpositive() {
|
|
|
|
assert(!is_nonpositive(infinity));
|
|
|
|
assert(!is_nonpositive(1.));
|
|
|
|
assert(!is_nonpositive(0.));
|
|
|
|
assert(is_nonpositive(-1.));
|
|
|
|
assert(is_nonpositive(neg_infinity));
|
|
|
|
assert(is_nonpositive(1./neg_infinity));
|
|
|
|
assert(!is_nonpositive(NaN));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonnegative() {
|
|
|
|
assert(is_nonnegative(infinity));
|
|
|
|
assert(is_nonnegative(1.));
|
|
|
|
assert(is_nonnegative(0.));
|
|
|
|
assert(!is_nonnegative(-1.));
|
|
|
|
assert(!is_nonnegative(neg_infinity));
|
|
|
|
assert(!is_nonnegative(1./neg_infinity));
|
|
|
|
assert(!is_nonnegative(NaN));
|
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
//
|
|
|
|
// Local Variables:
|
|
|
|
// mode: rust
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
|
|
|
// End:
|
|
|
|
//
|
2011-12-22 05:54:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|