2012-08-13 19:41:45 -05:00
|
|
|
// NB: transitionary, de-mode-ing.
|
|
|
|
#[forbid(deprecated_mode)];
|
|
|
|
#[forbid(deprecated_pattern)];
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
//! Operations and constants for `float`
|
2012-02-12 01:18:26 -06:00
|
|
|
|
2012-03-15 20:45:22 -05:00
|
|
|
// Even though this module exports everything defined in it,
|
|
|
|
// because it contains re-exports, we also have to explicitly
|
|
|
|
// export locally defined things. That's a bit annoying.
|
2012-09-27 16:23:03 -05:00
|
|
|
|
2012-01-05 07:46:14 -06:00
|
|
|
|
|
|
|
// export when m_float == c_double
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2012-09-04 13:12:17 -05:00
|
|
|
use m_float = f64;
|
|
|
|
|
2012-11-15 18:59:43 -06:00
|
|
|
pub use f64::{add, sub, mul, div, rem, lt, le, eq, ne, ge, gt};
|
|
|
|
pub use f64::logarithm;
|
|
|
|
pub use f64::{acos, asin, atan2, cbrt, ceil, copysign, cosh, floor};
|
|
|
|
pub use f64::{erf, erfc, exp, expm1, exp2, abs_sub};
|
|
|
|
pub use f64::{mul_add, fmax, fmin, nextafter, frexp, hypot, ldexp};
|
|
|
|
pub use f64::{lgamma, ln, log_radix, ln1p, log10, log2, ilog_radix};
|
|
|
|
pub use f64::{modf, pow, round, sinh, tanh, tgamma, trunc};
|
|
|
|
pub use f64::signbit;
|
|
|
|
pub use f64::{j0, j1, jn, y0, y1, yn};
|
2012-09-04 13:12:17 -05:00
|
|
|
use cmp::{Eq, Ord};
|
|
|
|
use num::from_int;
|
2011-12-22 19:31:24 -06:00
|
|
|
|
2012-09-27 16:23:03 -05:00
|
|
|
pub const NaN: float = 0.0/0.0;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-09-27 16:23:03 -05:00
|
|
|
pub const infinity: float = 1.0/0.0;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-09-27 16:23:03 -05:00
|
|
|
pub const neg_infinity: float = -1.0/0.0;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
|
|
|
/* Module: consts */
|
2012-09-26 20:16:44 -05:00
|
|
|
pub mod consts {
|
2012-06-21 18:44:10 -05:00
|
|
|
// FIXME (requires Issue #1433 to fix): replace with mathematical
|
|
|
|
// constants from cmath.
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Archimedes' constant
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const pi: float = 3.14159265358979323846264338327950288;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// pi/2.0
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const frac_pi_2: float = 1.57079632679489661923132169163975144;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// pi/4.0
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const frac_pi_4: float = 0.785398163397448309615660845819875721;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// 1.0/pi
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const frac_1_pi: float = 0.318309886183790671537767526745028724;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// 2.0/pi
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const frac_2_pi: float = 0.636619772367581343075535053490057448;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// 2.0/sqrt(pi)
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const frac_2_sqrtpi: float = 1.12837916709551257389615890312154517;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// sqrt(2.0)
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const sqrt2: float = 1.41421356237309504880168872420969808;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// 1.0/sqrt(2.0)
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const frac_1_sqrt2: float = 0.707106781186547524400844362104849039;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// Euler's number
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const e: float = 2.71828182845904523536028747135266250;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// log2(e)
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const log2_e: float = 1.44269504088896340735992468100189214;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// log10(e)
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const log10_e: float = 0.434294481903251827651128918916605082;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// ln(2.0)
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const ln_2: float = 0.693147180559945309417232121458176568;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// ln(10.0)
|
2012-09-26 20:16:44 -05:00
|
|
|
pub const ln_10: float = 2.30258509299404568401799145468436421;
|
2012-06-04 19:26:17 -05:00
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
/**
|
|
|
|
* Section: String Conversions
|
|
|
|
*/
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Converts a float to a string
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - The float value
|
|
|
|
* * digits - The number of significant digits
|
|
|
|
* * exact - Whether to enforce the exact number of significant digits
|
|
|
|
*/
|
2012-10-11 16:12:50 -05:00
|
|
|
pub pure fn to_str_common(num: float, digits: uint, exact: bool) -> ~str {
|
2012-08-01 19:30:05 -05:00
|
|
|
if is_NaN(num) { return ~"NaN"; }
|
|
|
|
if num == infinity { return ~"inf"; }
|
|
|
|
if num == neg_infinity { return ~"-inf"; }
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut (num, sign) = if num < 0.0 { (-num, ~"-") } else { (num, ~"") };
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
|
|
|
// truncated integer
|
2011-12-13 18:25:51 -06:00
|
|
|
let trunc = num as uint;
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
|
|
|
// decimal remainder
|
2012-03-06 22:48:40 -06:00
|
|
|
let mut frac = num - (trunc as float);
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
|
|
|
// stack of digits
|
2012-06-29 18:26:56 -05:00
|
|
|
let mut fractionalParts = ~[];
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
2012-06-14 18:30:18 -05:00
|
|
|
// FIXME: (#2608)
|
2012-06-29 18:26:56 -05:00
|
|
|
// This used to return right away without rounding, as "~[-]num",
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
// but given epsilon like in f64.rs, I don't see how the comparison
|
|
|
|
// to epsilon did much when only used there.
|
2012-08-01 19:30:05 -05:00
|
|
|
// if (frac < epsilon && !exact) || digits == 0u { return accum; }
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
//
|
|
|
|
// With something better, possibly weird results like this can be avoided:
|
|
|
|
// assert "3.14158999999999988262" == my_to_str_exact(3.14159, 20u);
|
|
|
|
|
|
|
|
let mut ii = digits;
|
|
|
|
let mut epsilon_prime = 1.0 / pow_with_uint(10u, ii);
|
|
|
|
|
|
|
|
// while we still need digits
|
|
|
|
// build stack of digits
|
2012-09-25 17:15:49 -05:00
|
|
|
while ii > 0 && (frac >= epsilon_prime || exact) {
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
// store the next digit
|
2011-12-13 18:25:51 -06:00
|
|
|
frac *= 10.0;
|
|
|
|
let digit = frac as uint;
|
2012-10-11 16:12:50 -05:00
|
|
|
// Bleh: not really unsafe.
|
|
|
|
unsafe { fractionalParts.push(digit); }
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
|
|
|
// calculate the next frac
|
2011-12-13 18:25:51 -06:00
|
|
|
frac -= digit as float;
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
epsilon_prime *= 10.0;
|
|
|
|
ii -= 1u;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut acc;
|
2012-07-14 00:57:48 -05:00
|
|
|
let mut racc = ~"";
|
2012-09-25 17:15:49 -05:00
|
|
|
let mut carry = if frac * 10.0 as uint >= 5 { 1 } else { 0 };
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
|
|
|
// turn digits into string
|
|
|
|
// using stack of digits
|
2012-09-25 17:15:49 -05:00
|
|
|
while fractionalParts.is_not_empty() {
|
2012-10-11 16:12:50 -05:00
|
|
|
// Bleh; shouldn't need to be unsafe
|
|
|
|
let mut adjusted_digit = carry + unsafe { fractionalParts.pop() };
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
|
2012-09-25 17:15:49 -05:00
|
|
|
if adjusted_digit == 10 {
|
|
|
|
carry = 1;
|
|
|
|
adjusted_digit %= 10
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
} else {
|
2012-09-25 17:15:49 -05:00
|
|
|
carry = 0;
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
racc = uint::str(adjusted_digit) + racc;
|
|
|
|
}
|
|
|
|
|
|
|
|
// pad decimals with trailing zeroes
|
2012-09-25 17:15:49 -05:00
|
|
|
while racc.len() < digits && exact {
|
2012-07-14 00:57:48 -05:00
|
|
|
racc += ~"0"
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// combine ints and decimals
|
|
|
|
let mut ones = uint::str(trunc + carry);
|
2012-07-14 00:57:48 -05:00
|
|
|
if racc == ~"" {
|
(float) fix some rounding errors when showing as str
This seems to fix issue #1876, and some of the superficial parts of
issue #1375. The #fmt macro and the to_str functions will round,
rather than truncate, floats as strings.
Other issues remain, and I wrote more code here than intended, but the
following should pass now.
```
fn x() {
assert "3.1416" == #fmt["%.4f", 3.14159];
assert "3" == #fmt["%.0f", 3.14159];
assert "99" == #fmt["%.0f", 98.5];
assert "7.0000" == #fmt["%.4f", 6.999999999];
assert "3.141590000" == #fmt["%.9f", 3.14159];
}
```
2012-06-02 02:15:29 -05:00
|
|
|
acc = sign + ones;
|
|
|
|
} else {
|
2012-07-14 00:57:48 -05:00
|
|
|
acc = sign + ones + ~"." + racc;
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
2012-09-05 21:19:04 -05:00
|
|
|
move acc
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Converts a float to a string with exactly the number of
|
|
|
|
* provided significant digits
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - The float value
|
|
|
|
* * digits - The number of significant digits
|
|
|
|
*/
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn to_str_exact(num: float, digits: uint) -> ~str {
|
2011-12-13 18:25:51 -06:00
|
|
|
to_str_common(num, digits, true)
|
|
|
|
}
|
|
|
|
|
2012-02-21 16:25:31 -06:00
|
|
|
#[test]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_to_str_exact_do_decimal() {
|
2012-02-21 16:25:31 -06:00
|
|
|
let s = to_str_exact(5.0, 4u);
|
2012-07-14 00:57:48 -05:00
|
|
|
assert s == ~"5.0000";
|
2012-02-21 16:25:31 -06:00
|
|
|
}
|
|
|
|
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Converts a float to a string with a maximum number of
|
|
|
|
* significant digits
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - The float value
|
|
|
|
* * digits - The number of significant digits
|
|
|
|
*/
|
2012-10-11 16:12:50 -05:00
|
|
|
pub pure fn to_str(num: float, digits: uint) -> ~str {
|
2011-12-13 18:25:51 -06:00
|
|
|
to_str_common(num, digits, false)
|
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* 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'
|
|
|
|
* * 'inf', '-inf', 'NaN'
|
|
|
|
*
|
|
|
|
* Leading and trailing whitespace are ignored.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - A string
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `none` if the string did not represent a valid number. Otherwise,
|
2012-08-20 14:23:37 -05:00
|
|
|
* `Some(n)` where `n` is the floating-point number represented by `[num]`.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn from_str(num: &str) -> Option<float> {
|
2012-08-13 19:41:45 -05:00
|
|
|
if num == "inf" {
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(infinity as float);
|
2012-08-13 19:41:45 -05:00
|
|
|
} else if num == "-inf" {
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(neg_infinity as float);
|
2012-08-13 19:41:45 -05:00
|
|
|
} else if num == "NaN" {
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(NaN as float);
|
2012-04-14 13:21:57 -05:00
|
|
|
}
|
|
|
|
|
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-08-20 14:23:37 -05:00
|
|
|
if len == 0u { return 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.
|
2012-08-06 14:34:08 -05:00
|
|
|
match str::char_at(num, 0u) {
|
2012-09-01 20:38:05 -05:00
|
|
|
'-' | '+' | '0' .. '9' | '.' => (),
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => return None
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-07-11 18:49:02 -05: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
|
2012-08-06 14:34:08 -05:00
|
|
|
match str::char_at(num, 0u) {
|
2012-08-03 21:59:04 -05:00
|
|
|
'-' => {
|
2011-12-13 18:25:51 -06:00
|
|
|
neg = true;
|
|
|
|
pos = 1u;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'+' => {
|
2011-12-13 18:25:51 -06:00
|
|
|
pos = 1u;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//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;
|
2012-08-06 14:34:08 -05:00
|
|
|
match c {
|
2012-09-01 20:38:05 -05:00
|
|
|
'0' .. '9' => {
|
2011-12-13 18:25:51 -06:00
|
|
|
total = total * 10f;
|
|
|
|
total += ((c as int) - ('0' as int)) as float;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'.' | 'e' | 'E' => break,
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => return 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;
|
2012-08-06 14:34:08 -05:00
|
|
|
match c {
|
2012-08-03 21:59:04 -05:00
|
|
|
'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;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'e' | 'E' => break,
|
2012-08-20 14:23:37 -05:00
|
|
|
_ => return None
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-27 18:26:35 -05: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;
|
2012-08-06 14:34:08 -05:00
|
|
|
match c {
|
2012-08-03 21:59:04 -05:00
|
|
|
'+' => {
|
2011-12-13 18:25:51 -06:00
|
|
|
pos = char_range.next;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
'-' => {
|
2011-12-13 18:25:51 -06:00
|
|
|
pos = char_range.next;
|
|
|
|
neg_exponent = true;
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => ()
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
while(pos < len) {
|
|
|
|
let char_range = str::char_range_at(num, pos);
|
|
|
|
c = char_range.ch;
|
2012-08-06 14:34:08 -05:00
|
|
|
match c {
|
2012-08-03 21:59:04 -05:00
|
|
|
'0' | '1' | '2' | '3' | '4' | '5' | '6'| '7' | '8' | '9' => {
|
2011-12-13 18:25:51 -06:00
|
|
|
exponent *= 10u;
|
|
|
|
exponent += ((c as uint) - ('0' as uint));
|
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ => break
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
pos = char_range.next;
|
|
|
|
}
|
2012-02-24 02:07:05 -06:00
|
|
|
let multiplier = pow_with_uint(10u, exponent);
|
2012-06-29 18:26:56 -05:00
|
|
|
//Note: not ~[int::pow], otherwise, we'll quickly
|
2011-12-13 18:25:51 -06:00
|
|
|
//end up with a nice overflow
|
|
|
|
if neg_exponent {
|
|
|
|
total = total / multiplier;
|
|
|
|
} else {
|
|
|
|
total = total * multiplier;
|
|
|
|
}
|
|
|
|
} else {
|
2012-08-20 14:23:37 -05:00
|
|
|
return None;
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pos < len) {
|
2012-08-20 14:23:37 -05:00
|
|
|
return None;
|
2011-12-13 18:25:51 -06:00
|
|
|
} else {
|
|
|
|
if(neg) {
|
|
|
|
total *= -1f;
|
|
|
|
}
|
2012-08-20 14:23:37 -05:00
|
|
|
return Some(total);
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Section: Arithmetics
|
|
|
|
*/
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
|
|
|
* Compute the exponentiation of an integer by another integer as a float
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * x - The base
|
|
|
|
* * pow - The exponent
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `NaN` if both `x` and `pow` are `0u`, otherwise `x^pow`
|
|
|
|
*/
|
2012-10-11 16:12:50 -05:00
|
|
|
pub pure fn pow_with_uint(base: uint, pow: uint) -> float {
|
2012-06-04 19:26:17 -05:00
|
|
|
if base == 0u {
|
|
|
|
if pow == 0u {
|
2012-08-01 19:30:05 -05:00
|
|
|
return NaN as float;
|
2012-06-04 19:26:17 -05:00
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return 0.;
|
2012-06-04 19:26:17 -05:00
|
|
|
}
|
|
|
|
let mut my_pow = pow;
|
|
|
|
let mut total = 1f;
|
|
|
|
let mut multiplier = base as float;
|
|
|
|
while (my_pow > 0u) {
|
|
|
|
if my_pow % 2u == 1u {
|
|
|
|
total = total * multiplier;
|
|
|
|
}
|
|
|
|
my_pow /= 2u;
|
|
|
|
multiplier *= multiplier;
|
|
|
|
}
|
2012-08-01 19:30:05 -05:00
|
|
|
return total;
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-09-26 20:16:44 -05:00
|
|
|
pub pure fn is_positive(x: float) -> bool { f64::is_positive(x as f64) }
|
|
|
|
pub pure fn is_negative(x: float) -> bool { f64::is_negative(x as f64) }
|
|
|
|
pub pure fn is_nonpositive(x: float) -> bool { f64::is_nonpositive(x as f64) }
|
|
|
|
pub pure fn is_nonnegative(x: float) -> bool { f64::is_nonnegative(x as f64) }
|
|
|
|
pub pure fn is_zero(x: float) -> bool { f64::is_zero(x as f64) }
|
|
|
|
pub pure fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) }
|
|
|
|
pub pure fn is_finite(x: float) -> bool { f64::is_finite(x as f64) }
|
|
|
|
pub pure fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }
|
|
|
|
|
|
|
|
pub pure fn abs(x: float) -> float { f64::abs(x as f64) as float }
|
|
|
|
pub pure fn sqrt(x: float) -> float { f64::sqrt(x as f64) as float }
|
|
|
|
pub pure fn atan(x: float) -> float { f64::atan(x as f64) as float }
|
|
|
|
pub pure fn sin(x: float) -> float { f64::sin(x as f64) as float }
|
|
|
|
pub pure fn cos(x: float) -> float { f64::cos(x as f64) as float }
|
|
|
|
pub pure fn tan(x: float) -> float { f64::tan(x as f64) as float }
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl float : Eq {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn eq(&self, other: &float) -> bool { (*self) == (*other) }
|
|
|
|
pure fn ne(&self, other: &float) -> bool { (*self) != (*other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-09-19 20:00:26 -05:00
|
|
|
impl float : Ord {
|
2012-11-14 20:59:30 -06:00
|
|
|
pure fn lt(&self, other: &float) -> bool { (*self) < (*other) }
|
|
|
|
pure fn le(&self, other: &float) -> bool { (*self) <= (*other) }
|
|
|
|
pure fn ge(&self, other: &float) -> bool { (*self) >= (*other) }
|
|
|
|
pure fn gt(&self, other: &float) -> bool { (*self) > (*other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2012-08-13 18:20:27 -05:00
|
|
|
impl float: num::Num {
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-09-27 16:23:03 -05:00
|
|
|
pub pure fn add(other: &float) -> float { return self + *other; }
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-09-27 16:23:03 -05:00
|
|
|
pub pure fn sub(other: &float) -> float { return self - *other; }
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-09-27 16:23:03 -05:00
|
|
|
pub pure fn mul(other: &float) -> float { return self * *other; }
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-09-27 16:23:03 -05:00
|
|
|
pub pure fn div(other: &float) -> float { return self / *other; }
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-09-25 17:15:49 -05:00
|
|
|
pure fn modulo(other: &float) -> float { return self % *other; }
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn neg() -> float { return -self; }
|
|
|
|
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-08-01 19:30:05 -05:00
|
|
|
pure fn to_int() -> int { return self as int; }
|
2012-11-23 15:27:02 -06:00
|
|
|
#[inline(always)]
|
2012-08-14 22:03:31 -05:00
|
|
|
static pure fn from_int(n: int) -> float { return n as float; }
|
2012-06-07 19:25:54 -05:00
|
|
|
}
|
|
|
|
|
2012-01-17 19:28:21 -06:00
|
|
|
#[test]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_from_str() {
|
2012-08-20 14:23:37 -05: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(~"-0") == Some(-0.);
|
|
|
|
assert from_str(~"0") == Some(0.);
|
|
|
|
assert from_str(~"inf") == Some(infinity);
|
|
|
|
assert from_str(~"-inf") == Some(neg_infinity);
|
2012-04-14 13:21:57 -05:00
|
|
|
// note: NaN != NaN, hence this slightly complex test
|
2012-08-06 14:34:08 -05:00
|
|
|
match from_str(~"NaN") {
|
2012-08-20 14:23:37 -05:00
|
|
|
Some(f) => assert is_NaN(f),
|
|
|
|
None => fail
|
2012-04-14 13:21:57 -05:00
|
|
|
}
|
2012-02-22 06:18:15 -06:00
|
|
|
|
2012-08-27 19:49:35 -05:00
|
|
|
assert from_str(~"").is_none();
|
|
|
|
assert from_str(~"x").is_none();
|
|
|
|
assert from_str(~" ").is_none();
|
|
|
|
assert from_str(~" ").is_none();
|
|
|
|
assert from_str(~"e").is_none();
|
|
|
|
assert from_str(~"E").is_none();
|
|
|
|
assert from_str(~"E1").is_none();
|
|
|
|
assert from_str(~"1e1e1").is_none();
|
|
|
|
assert from_str(~"1e1.1").is_none();
|
|
|
|
assert from_str(~"1e1-1").is_none();
|
2012-01-17 19:28:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_positive() {
|
2012-01-17 19:28:21 -06:00
|
|
|
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]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_negative() {
|
2012-01-17 19:28:21 -06:00
|
|
|
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]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_nonpositive() {
|
2012-01-17 19:28:21 -06:00
|
|
|
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]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_nonnegative() {
|
2012-01-17 19:28:21 -06:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2012-04-08 16:16:55 -05:00
|
|
|
#[test]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_to_str_inf() {
|
2012-07-14 00:57:48 -05:00
|
|
|
assert to_str(infinity, 10u) == ~"inf";
|
|
|
|
assert to_str(-infinity, 10u) == ~"-inf";
|
2012-04-08 16:16:55 -05:00
|
|
|
}
|
|
|
|
|
2012-06-07 19:25:54 -05:00
|
|
|
#[test]
|
2012-09-26 20:16:44 -05:00
|
|
|
pub fn test_traits() {
|
2012-08-27 18:26:35 -05:00
|
|
|
fn test<U:num::Num cmp::Eq>(ten: &U) {
|
2012-06-07 19:25:54 -05:00
|
|
|
assert (ten.to_int() == 10);
|
|
|
|
|
2012-08-14 23:13:05 -05:00
|
|
|
let two: U = from_int(2);
|
2012-06-07 19:25:54 -05:00
|
|
|
assert (two.to_int() == 2);
|
|
|
|
|
2012-09-25 17:15:49 -05:00
|
|
|
assert (ten.add(&two) == from_int(12));
|
|
|
|
assert (ten.sub(&two) == from_int(8));
|
|
|
|
assert (ten.mul(&two) == from_int(20));
|
|
|
|
assert (ten.div(&two) == from_int(5));
|
|
|
|
assert (ten.modulo(&two) == from_int(0));
|
2012-06-07 19:25:54 -05:00
|
|
|
}
|
|
|
|
|
2012-08-13 19:41:45 -05:00
|
|
|
test(&10.0);
|
2012-06-07 19:25:54 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|