2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
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
|
|
|
|
2013-01-26 20:28:39 -06:00
|
|
|
use from_str;
|
2013-04-24 05:08:08 -05:00
|
|
|
use libc::c_int;
|
|
|
|
use num::strconv;
|
|
|
|
use prelude::*;
|
2013-04-21 10:58:53 -05:00
|
|
|
|
|
|
|
pub use f64::{add, sub, mul, quot, rem, lt, le, eq, ne, ge, gt};
|
2012-11-15 18:59:43 -06:00
|
|
|
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};
|
2013-04-03 11:08:53 -05:00
|
|
|
pub use f64::{modf, pow, powi, round, sinh, tanh, tgamma, trunc};
|
2012-11-15 18:59:43 -06:00
|
|
|
pub use f64::{j0, j1, jn, y0, y1, yn};
|
2011-12-22 19:31:24 -06:00
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static NaN: float = 0.0/0.0;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static infinity: float = 1.0/0.0;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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
|
2013-03-22 16:00:15 -05:00
|
|
|
// staticants from cmath.
|
|
|
|
/// Archimedes' staticant
|
|
|
|
pub static pi: float = 3.14159265358979323846264338327950288;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// pi/2.0
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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)
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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)
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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)
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static 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
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static e: float = 2.71828182845904523536028747135266250;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// log2(e)
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static log2_e: float = 1.44269504088896340735992468100189214;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// log10(e)
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static log10_e: float = 0.434294481903251827651128918916605082;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// ln(2.0)
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ln_2: float = 0.693147180559945309417232121458176568;
|
2012-06-04 19:26:17 -05:00
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/// ln(10.0)
|
2013-03-22 16:00:15 -05:00
|
|
|
pub static ln_10: float = 2.30258509299404568401799145468436421;
|
2012-06-04 19:26:17 -05:00
|
|
|
}
|
|
|
|
|
2013-01-26 20:28:39 -06: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
|
|
|
|
*/
|
2013-01-26 20:28:39 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_str(num: float) -> ~str {
|
2013-02-14 20:29:36 -06:00
|
|
|
let (r, _) = strconv::to_str_common(
|
2013-02-14 22:20:36 -06:00
|
|
|
&num, 10u, true, strconv::SignNeg, strconv::DigAll);
|
2013-01-26 20:28:39 -06:00
|
|
|
r
|
|
|
|
}
|
(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
|
|
|
|
2013-01-26 20:28:39 -06:00
|
|
|
/**
|
|
|
|
* Converts a float to a string in hexadecimal format
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - The float value
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_str_hex(num: float) -> ~str {
|
2013-02-14 20:29:36 -06:00
|
|
|
let (r, _) = strconv::to_str_common(
|
2013-02-14 22:20:36 -06:00
|
|
|
&num, 16u, true, strconv::SignNeg, strconv::DigAll);
|
2013-01-26 20:28:39 -06:00
|
|
|
r
|
|
|
|
}
|
(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
|
|
|
|
2013-01-26 20:28:39 -06:00
|
|
|
/**
|
|
|
|
* Converts a float to a string in a given radix
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - The float value
|
|
|
|
* * radix - The base to use
|
2013-02-03 10:27:01 -06:00
|
|
|
*
|
|
|
|
* # Failure
|
|
|
|
*
|
|
|
|
* Fails if called on a special value like `inf`, `-inf` or `NaN` due to
|
|
|
|
* possible misinterpretation of the result at higher bases. If those values
|
|
|
|
* are expected, use `to_str_radix_special()` instead.
|
2013-01-26 20:28:39 -06:00
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_str_radix(num: float, radix: uint) -> ~str {
|
2013-02-14 20:29:36 -06:00
|
|
|
let (r, special) = strconv::to_str_common(
|
2013-02-14 22:20:36 -06:00
|
|
|
&num, radix, true, strconv::SignNeg, strconv::DigAll);
|
2013-02-11 21:26:38 -06:00
|
|
|
if special { fail!(~"number has a special value, \
|
2013-04-15 10:08:52 -05:00
|
|
|
try to_str_radix_special() if those are expected") }
|
2013-01-26 20:28:39 -06:00
|
|
|
r
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2013-02-03 10:27:01 -06:00
|
|
|
/**
|
|
|
|
* Converts a float to a string in a given radix, and a flag indicating
|
|
|
|
* whether it's a special value
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - The float value
|
|
|
|
* * radix - The base to use
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
|
2013-02-14 22:20:36 -06:00
|
|
|
strconv::to_str_common(&num, radix, true,
|
2013-02-14 20:29:36 -06:00
|
|
|
strconv::SignNeg, strconv::DigAll)
|
2013-02-03 10:27:01 -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
|
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_str_exact(num: float, digits: uint) -> ~str {
|
2013-02-14 20:29:36 -06:00
|
|
|
let (r, _) = strconv::to_str_common(
|
2013-02-14 22:20:36 -06:00
|
|
|
&num, 10u, true, strconv::SignNeg, strconv::DigExact(digits));
|
2013-01-26 20:28:39 -06:00
|
|
|
r
|
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
|
|
|
|
*/
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn to_str_digits(num: float, digits: uint) -> ~str {
|
2013-02-14 20:29:36 -06:00
|
|
|
let (r, _) = strconv::to_str_common(
|
2013-02-14 22:20:36 -06:00
|
|
|
&num, 10u, true, strconv::SignNeg, strconv::DigMax(digits));
|
2013-01-26 20:28:39 -06:00
|
|
|
r
|
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl to_str::ToStr for float {
|
2013-01-26 20:28:39 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str(&self) -> ~str { to_str_digits(*self, 8) }
|
2013-01-26 20:28:39 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl num::ToStrRadix for float {
|
2013-01-26 20:28:39 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn to_str_radix(&self, radix: uint) -> ~str {
|
2013-01-26 20:28:39 -06:00
|
|
|
to_str_radix(*self, radix)
|
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
}
|
|
|
|
|
2012-07-04 16:53:12 -05:00
|
|
|
/**
|
2013-01-26 20:28:39 -06:00
|
|
|
* Convert a string in base 10 to a float.
|
|
|
|
* Accepts a optional decimal exponent.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* 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'
|
2013-01-26 20:28:39 -06:00
|
|
|
* * '.' (understood as 0)
|
2012-07-04 16:53:12 -05:00
|
|
|
* * '5.'
|
|
|
|
* * '.5', or, equivalently, '0.5'
|
2013-01-26 20:28:39 -06:00
|
|
|
* * '+inf', 'inf', '-inf', 'NaN'
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
2013-01-26 20:28:39 -06:00
|
|
|
* Leading and trailing whitespace represent an error.
|
2012-07-04 16:53:12 -05:00
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - A string
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `none` if the string did not represent a valid number. Otherwise,
|
2013-01-26 20:28:39 -06:00
|
|
|
* `Some(n)` where `n` is the floating-point number represented by `num`.
|
2012-07-04 16:53:12 -05:00
|
|
|
*/
|
2013-01-26 20:28:39 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn from_str(num: &str) -> Option<float> {
|
2013-02-14 20:29:36 -06:00
|
|
|
strconv::from_str_common(num, 10u, true, true, true,
|
2013-04-07 09:23:42 -05:00
|
|
|
strconv::ExpDec, false, false)
|
2013-01-26 20:28:39 -06:00
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-01-26 20:28:39 -06:00
|
|
|
/**
|
|
|
|
* Convert a string in base 16 to a float.
|
|
|
|
* Accepts a optional binary exponent.
|
|
|
|
*
|
|
|
|
* This function accepts strings such as
|
|
|
|
*
|
|
|
|
* * 'a4.fe'
|
|
|
|
* * '+a4.fe', equivalent to 'a4.fe'
|
|
|
|
* * '-a4.fe'
|
|
|
|
* * '2b.aP128', or equivalently, '2b.ap128'
|
|
|
|
* * '2b.aP-128'
|
|
|
|
* * '.' (understood as 0)
|
|
|
|
* * 'c.'
|
|
|
|
* * '.c', or, equivalently, '0.c'
|
|
|
|
* * '+inf', 'inf', '-inf', 'NaN'
|
|
|
|
*
|
|
|
|
* Leading and trailing whitespace represent an error.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - A string
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `none` if the string did not represent a valid number. Otherwise,
|
|
|
|
* `Some(n)` where `n` is the floating-point number represented by `[num]`.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn from_str_hex(num: &str) -> Option<float> {
|
2013-02-14 20:29:36 -06:00
|
|
|
strconv::from_str_common(num, 16u, true, true, true,
|
2013-04-07 09:23:42 -05:00
|
|
|
strconv::ExpBin, false, false)
|
2013-01-26 20:28:39 -06:00
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-01-26 20:28:39 -06:00
|
|
|
/**
|
|
|
|
* Convert a string in an given base to a float.
|
|
|
|
*
|
|
|
|
* Due to possible conflicts, this function does **not** accept
|
|
|
|
* the special values `inf`, `-inf`, `+inf` and `NaN`, **nor**
|
|
|
|
* does it recognize exponents of any kind.
|
|
|
|
*
|
|
|
|
* Leading and trailing whitespace represent an error.
|
|
|
|
*
|
|
|
|
* # Arguments
|
|
|
|
*
|
|
|
|
* * num - A string
|
|
|
|
* * radix - The base to use. Must lie in the range [2 .. 36]
|
|
|
|
*
|
|
|
|
* # Return value
|
|
|
|
*
|
|
|
|
* `none` if the string did not represent a valid number. Otherwise,
|
|
|
|
* `Some(n)` where `n` is the floating-point number represented by `num`.
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn from_str_radix(num: &str, radix: uint) -> Option<float> {
|
2013-02-14 20:29:36 -06:00
|
|
|
strconv::from_str_common(num, radix, true, true, false,
|
2013-04-07 09:23:42 -05:00
|
|
|
strconv::ExpNone, false, false)
|
2013-01-26 20:28:39 -06:00
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl from_str::FromStr for float {
|
2013-01-26 20:28:39 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn from_str(val: &str) -> Option<float> { from_str(val) }
|
2013-01-26 20:28:39 -06:00
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl num::FromStrRadix for float {
|
2013-01-26 20:28:39 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn from_str_radix(val: &str, radix: uint) -> Option<float> {
|
2013-01-26 20:28:39 -06:00
|
|
|
from_str_radix(val, radix)
|
|
|
|
}
|
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`
|
|
|
|
*/
|
2013-03-21 23:20:48 -05:00
|
|
|
pub 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
|
|
|
}
|
|
|
|
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_zero(x: float) -> bool { f64::is_zero(x as f64) }
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) }
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_finite(x: float) -> bool { f64::is_finite(x as f64) }
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) }
|
2012-09-26 20:16:44 -05:00
|
|
|
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn abs(x: float) -> float {
|
2013-04-09 00:31:23 -05:00
|
|
|
f64::abs(x as f64) as float
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn sqrt(x: float) -> float {
|
2013-04-09 00:31:23 -05:00
|
|
|
f64::sqrt(x as f64) as float
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn atan(x: float) -> float {
|
2013-04-09 00:31:23 -05:00
|
|
|
f64::atan(x as f64) as float
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn sin(x: float) -> float {
|
2013-04-09 00:31:23 -05:00
|
|
|
f64::sin(x as f64) as float
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn cos(x: float) -> float {
|
2013-04-09 00:31:23 -05:00
|
|
|
f64::cos(x as f64) as float
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
pub fn tan(x: float) -> float {
|
2013-04-09 00:31:23 -05:00
|
|
|
f64::tan(x as f64) as float
|
2013-01-10 23:23:07 -06:00
|
|
|
}
|
2011-12-13 18:25:51 -06:00
|
|
|
|
2013-04-24 05:08:08 -05:00
|
|
|
impl Num for float {}
|
|
|
|
|
2012-11-30 02:47:45 -06:00
|
|
|
#[cfg(notest)]
|
2013-02-14 13:47:00 -06:00
|
|
|
impl Eq for float {
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn eq(&self, other: &float) -> bool { (*self) == (*other) }
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
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-11-30 02:47:45 -06:00
|
|
|
#[cfg(notest)]
|
2013-02-14 13:47:00 -06:00
|
|
|
impl Ord for float {
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn lt(&self, other: &float) -> bool { (*self) < (*other) }
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn le(&self, other: &float) -> bool { (*self) <= (*other) }
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn ge(&self, other: &float) -> bool { (*self) >= (*other) }
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn gt(&self, other: &float) -> bool { (*self) > (*other) }
|
2012-09-19 20:00:26 -05:00
|
|
|
}
|
2012-08-27 18:26:35 -05:00
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl num::Zero for float {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn zero() -> float { 0.0 }
|
2012-12-20 09:14:38 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl num::One for float {
|
2013-01-13 08:37:30 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn one() -> float { 1.0 }
|
2012-12-20 09:14:38 -06:00
|
|
|
}
|
|
|
|
|
2013-02-14 13:47:00 -06:00
|
|
|
impl num::Round for float {
|
2013-01-26 20:05:20 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn round(&self, mode: num::RoundMode) -> float {
|
2013-01-26 20:05:20 -06:00
|
|
|
match mode {
|
|
|
|
num::RoundDown
|
|
|
|
=> f64::floor(*self as f64) as float,
|
|
|
|
num::RoundUp
|
|
|
|
=> f64::ceil(*self as f64) as float,
|
2013-04-23 02:59:49 -05:00
|
|
|
num::RoundToZero if self.is_negative()
|
2013-01-26 20:05:20 -06:00
|
|
|
=> f64::ceil(*self as f64) as float,
|
|
|
|
num::RoundToZero
|
|
|
|
=> f64::floor(*self as f64) as float,
|
2013-04-23 02:59:49 -05:00
|
|
|
num::RoundFromZero if self.is_negative()
|
2013-01-26 20:05:20 -06:00
|
|
|
=> f64::floor(*self as f64) as float,
|
|
|
|
num::RoundFromZero
|
|
|
|
=> f64::ceil(*self as f64) as float
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn floor(&self) -> float { f64::floor(*self as f64) as float}
|
2013-01-26 20:05:20 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn ceil(&self) -> float { f64::ceil(*self as f64) as float}
|
2013-01-26 20:05:20 -06:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn fract(&self) -> float {
|
2013-04-23 02:59:49 -05:00
|
|
|
if self.is_negative() {
|
2013-01-26 20:05:20 -06:00
|
|
|
(*self) - (f64::ceil(*self as f64) as float)
|
|
|
|
} else {
|
|
|
|
(*self) - (f64::floor(*self as f64) as float)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-12 19:07:26 -06:00
|
|
|
#[cfg(notest)]
|
2013-04-21 10:58:53 -05:00
|
|
|
impl Add<float,float> for float {
|
2013-04-23 12:56:49 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn add(&self, other: &float) -> float { *self + *other }
|
2013-02-12 19:07:26 -06:00
|
|
|
}
|
2013-04-23 12:56:49 -05:00
|
|
|
|
2013-02-12 19:07:26 -06:00
|
|
|
#[cfg(notest)]
|
2013-04-21 10:58:53 -05:00
|
|
|
impl Sub<float,float> for float {
|
2013-04-23 12:56:49 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn sub(&self, other: &float) -> float { *self - *other }
|
2013-02-12 19:07:26 -06:00
|
|
|
}
|
2013-04-23 12:56:49 -05:00
|
|
|
|
2013-02-12 19:07:26 -06:00
|
|
|
#[cfg(notest)]
|
2013-04-21 10:58:53 -05:00
|
|
|
impl Mul<float,float> for float {
|
2013-04-23 12:56:49 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn mul(&self, other: &float) -> float { *self * *other }
|
2013-02-12 19:07:26 -06:00
|
|
|
}
|
2013-04-23 12:56:49 -05:00
|
|
|
|
2013-04-21 10:58:53 -05:00
|
|
|
#[cfg(stage0,notest)]
|
|
|
|
impl Div<float,float> for float {
|
2013-04-23 12:56:49 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn div(&self, other: &float) -> float { *self / *other }
|
2013-02-12 19:07:26 -06:00
|
|
|
}
|
2013-04-21 10:58:53 -05:00
|
|
|
#[cfg(stage1,notest)]
|
|
|
|
#[cfg(stage2,notest)]
|
|
|
|
#[cfg(stage3,notest)]
|
|
|
|
impl Quot<float,float> for float {
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-04-21 10:58:53 -05:00
|
|
|
fn quot(&self, other: &float) -> float { *self / *other }
|
|
|
|
}
|
|
|
|
#[cfg(stage0,notest)]
|
|
|
|
impl Modulo<float,float> for float {
|
2013-04-23 12:56:49 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn modulo(&self, other: &float) -> float { *self % *other }
|
2013-02-12 19:07:26 -06:00
|
|
|
}
|
2013-04-21 10:58:53 -05:00
|
|
|
#[cfg(stage1,notest)]
|
|
|
|
#[cfg(stage2,notest)]
|
|
|
|
#[cfg(stage3,notest)]
|
|
|
|
impl Rem<float,float> for float {
|
2013-04-18 08:24:24 -05:00
|
|
|
#[inline(always)]
|
2013-04-21 10:58:53 -05:00
|
|
|
fn rem(&self, other: &float) -> float { *self % *other }
|
|
|
|
}
|
|
|
|
#[cfg(notest)]
|
|
|
|
impl Neg<float> for float {
|
2013-04-23 12:56:49 -05:00
|
|
|
#[inline(always)]
|
2013-03-21 23:20:48 -05:00
|
|
|
fn neg(&self) -> float { -*self }
|
2013-02-12 19:07:26 -06:00
|
|
|
}
|
|
|
|
|
2013-04-23 02:59:49 -05:00
|
|
|
impl Signed for float {
|
|
|
|
/// Computes the absolute value. Returns `NaN` if the number is `NaN`.
|
|
|
|
#[inline(always)]
|
|
|
|
fn abs(&self) -> float { abs(*self) }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* # Returns
|
|
|
|
*
|
|
|
|
* - `1.0` if the number is positive, `+0.0` or `infinity`
|
|
|
|
* - `-1.0` if the number is negative, `-0.0` or `neg_infinity`
|
|
|
|
* - `NaN` if the number is NaN
|
|
|
|
*/
|
|
|
|
#[inline(always)]
|
|
|
|
fn signum(&self) -> float {
|
|
|
|
if is_NaN(*self) { NaN } else { f64::copysign(1.0, *self as f64) as float }
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns `true` if the number is positive, including `+0.0` and `infinity`
|
|
|
|
#[inline(always)]
|
|
|
|
fn is_positive(&self) -> bool { *self > 0.0 || (1.0 / *self) == infinity }
|
|
|
|
|
|
|
|
/// Returns `true` if the number is negative, including `-0.0` and `neg_infinity`
|
|
|
|
#[inline(always)]
|
|
|
|
fn is_negative(&self) -> bool { *self < 0.0 || (1.0 / *self) == neg_infinity }
|
|
|
|
}
|
|
|
|
|
2013-04-15 10:08:52 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use prelude::*;
|
2013-04-23 02:59:49 -05:00
|
|
|
|
2013-04-24 05:08:08 -05:00
|
|
|
#[test]
|
|
|
|
fn test_num() {
|
|
|
|
num::test_num(10f, 2f);
|
|
|
|
}
|
|
|
|
|
2013-04-23 02:59:49 -05:00
|
|
|
#[test]
|
|
|
|
pub fn test_signed() {
|
|
|
|
assert_eq!(infinity.abs(), infinity);
|
|
|
|
assert_eq!(1f.abs(), 1f);
|
|
|
|
assert_eq!(0f.abs(), 0f);
|
|
|
|
assert_eq!((-0f).abs(), 0f);
|
|
|
|
assert_eq!((-1f).abs(), 1f);
|
|
|
|
assert_eq!(neg_infinity.abs(), infinity);
|
|
|
|
assert_eq!((1f/neg_infinity).abs(), 0f);
|
|
|
|
assert!(is_NaN(NaN.abs()));
|
|
|
|
|
|
|
|
assert_eq!(infinity.signum(), 1f);
|
|
|
|
assert_eq!(1f.signum(), 1f);
|
|
|
|
assert_eq!(0f.signum(), 1f);
|
|
|
|
assert_eq!((-0f).signum(), -1f);
|
|
|
|
assert_eq!((-1f).signum(), -1f);
|
|
|
|
assert_eq!(neg_infinity.signum(), -1f);
|
|
|
|
assert_eq!((1f/neg_infinity).signum(), -1f);
|
|
|
|
assert!(is_NaN(NaN.signum()));
|
|
|
|
|
|
|
|
assert!(infinity.is_positive());
|
|
|
|
assert!(1f.is_positive());
|
|
|
|
assert!(0f.is_positive());
|
|
|
|
assert!(!(-0f).is_positive());
|
|
|
|
assert!(!(-1f).is_positive());
|
|
|
|
assert!(!neg_infinity.is_positive());
|
|
|
|
assert!(!(1f/neg_infinity).is_positive());
|
|
|
|
assert!(!NaN.is_positive());
|
|
|
|
|
|
|
|
assert!(!infinity.is_negative());
|
|
|
|
assert!(!1f.is_negative());
|
|
|
|
assert!(!0f.is_negative());
|
|
|
|
assert!((-0f).is_negative());
|
|
|
|
assert!((-1f).is_negative());
|
|
|
|
assert!(neg_infinity.is_negative());
|
|
|
|
assert!((1f/neg_infinity).is_negative());
|
|
|
|
assert!(!NaN.is_negative());
|
|
|
|
}
|
|
|
|
|
2013-04-15 10:08:52 -05:00
|
|
|
#[test]
|
|
|
|
pub fn test_to_str_exact_do_decimal() {
|
|
|
|
let s = to_str_exact(5.0, 4u);
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(s, ~"5.0000");
|
2013-04-15 10:08:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_from_str() {
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(from_str(~"3"), Some(3.));
|
|
|
|
assert_eq!(from_str(~"3.14"), Some(3.14));
|
|
|
|
assert_eq!(from_str(~"+3.14"), Some(3.14));
|
|
|
|
assert_eq!(from_str(~"-3.14"), Some(-3.14));
|
|
|
|
assert_eq!(from_str(~"2.5E10"), Some(25000000000.));
|
|
|
|
assert_eq!(from_str(~"2.5e10"), Some(25000000000.));
|
|
|
|
assert_eq!(from_str(~"25000000000.E-10"), Some(2.5));
|
|
|
|
assert_eq!(from_str(~"."), Some(0.));
|
|
|
|
assert_eq!(from_str(~".e1"), Some(0.));
|
|
|
|
assert_eq!(from_str(~".e-1"), Some(0.));
|
|
|
|
assert_eq!(from_str(~"5."), Some(5.));
|
|
|
|
assert_eq!(from_str(~".5"), Some(0.5));
|
|
|
|
assert_eq!(from_str(~"0.5"), Some(0.5));
|
|
|
|
assert_eq!(from_str(~"-.5"), Some(-0.5));
|
|
|
|
assert_eq!(from_str(~"-5"), Some(-5.));
|
|
|
|
assert_eq!(from_str(~"inf"), Some(infinity));
|
|
|
|
assert_eq!(from_str(~"+inf"), Some(infinity));
|
|
|
|
assert_eq!(from_str(~"-inf"), Some(neg_infinity));
|
2013-04-15 10:08:52 -05:00
|
|
|
// note: NaN != NaN, hence this slightly complex test
|
|
|
|
match from_str(~"NaN") {
|
|
|
|
Some(f) => assert!(is_NaN(f)),
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
// note: -0 == 0, hence these slightly more complex tests
|
|
|
|
match from_str(~"-0") {
|
2013-04-23 02:59:49 -05:00
|
|
|
Some(v) if is_zero(v) => assert!(v.is_negative()),
|
2013-04-15 10:08:52 -05:00
|
|
|
_ => fail!()
|
|
|
|
}
|
|
|
|
match from_str(~"0") {
|
2013-04-23 02:59:49 -05:00
|
|
|
Some(v) if is_zero(v) => assert!(v.is_positive()),
|
2013-04-15 10:08:52 -05:00
|
|
|
_ => fail!()
|
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_from_str_hex() {
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(from_str_hex(~"a4"), Some(164.));
|
|
|
|
assert_eq!(from_str_hex(~"a4.fe"), Some(164.9921875));
|
|
|
|
assert_eq!(from_str_hex(~"-a4.fe"), Some(-164.9921875));
|
|
|
|
assert_eq!(from_str_hex(~"+a4.fe"), Some(164.9921875));
|
|
|
|
assert_eq!(from_str_hex(~"ff0P4"), Some(0xff00 as float));
|
|
|
|
assert_eq!(from_str_hex(~"ff0p4"), Some(0xff00 as float));
|
|
|
|
assert_eq!(from_str_hex(~"ff0p-4"), Some(0xff as float));
|
|
|
|
assert_eq!(from_str_hex(~"."), Some(0.));
|
|
|
|
assert_eq!(from_str_hex(~".p1"), Some(0.));
|
|
|
|
assert_eq!(from_str_hex(~".p-1"), Some(0.));
|
|
|
|
assert_eq!(from_str_hex(~"f."), Some(15.));
|
|
|
|
assert_eq!(from_str_hex(~".f"), Some(0.9375));
|
|
|
|
assert_eq!(from_str_hex(~"0.f"), Some(0.9375));
|
|
|
|
assert_eq!(from_str_hex(~"-.f"), Some(-0.9375));
|
|
|
|
assert_eq!(from_str_hex(~"-f"), Some(-15.));
|
|
|
|
assert_eq!(from_str_hex(~"inf"), Some(infinity));
|
|
|
|
assert_eq!(from_str_hex(~"+inf"), Some(infinity));
|
|
|
|
assert_eq!(from_str_hex(~"-inf"), Some(neg_infinity));
|
2013-04-15 10:08:52 -05:00
|
|
|
// note: NaN != NaN, hence this slightly complex test
|
|
|
|
match from_str_hex(~"NaN") {
|
|
|
|
Some(f) => assert!(is_NaN(f)),
|
|
|
|
None => fail!()
|
|
|
|
}
|
|
|
|
// note: -0 == 0, hence these slightly more complex tests
|
|
|
|
match from_str_hex(~"-0") {
|
2013-04-23 02:59:49 -05:00
|
|
|
Some(v) if is_zero(v) => assert!(v.is_negative()),
|
2013-04-15 10:08:52 -05:00
|
|
|
_ => fail!()
|
|
|
|
}
|
|
|
|
match from_str_hex(~"0") {
|
2013-04-23 02:59:49 -05:00
|
|
|
Some(v) if is_zero(v) => assert!(v.is_positive()),
|
2013-04-15 10:08:52 -05:00
|
|
|
_ => fail!()
|
|
|
|
}
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(from_str_hex(~"e"), Some(14.));
|
|
|
|
assert_eq!(from_str_hex(~"E"), Some(14.));
|
|
|
|
assert_eq!(from_str_hex(~"E1"), Some(225.));
|
|
|
|
assert_eq!(from_str_hex(~"1e1e1"), Some(123361.));
|
|
|
|
assert_eq!(from_str_hex(~"1e1.1"), Some(481.0625));
|
2013-04-15 10:08:52 -05:00
|
|
|
|
|
|
|
assert!(from_str_hex(~"").is_none());
|
|
|
|
assert!(from_str_hex(~"x").is_none());
|
|
|
|
assert!(from_str_hex(~" ").is_none());
|
|
|
|
assert!(from_str_hex(~" ").is_none());
|
|
|
|
assert!(from_str_hex(~"p").is_none());
|
|
|
|
assert!(from_str_hex(~"P").is_none());
|
|
|
|
assert!(from_str_hex(~"P1").is_none());
|
|
|
|
assert!(from_str_hex(~"1p1p1").is_none());
|
|
|
|
assert!(from_str_hex(~"1p1.1").is_none());
|
|
|
|
assert!(from_str_hex(~"1p1-1").is_none());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_to_str_hex() {
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(to_str_hex(164.), ~"a4");
|
|
|
|
assert_eq!(to_str_hex(164.9921875), ~"a4.fe");
|
|
|
|
assert_eq!(to_str_hex(-164.9921875), ~"-a4.fe");
|
|
|
|
assert_eq!(to_str_hex(0xff00 as float), ~"ff00");
|
|
|
|
assert_eq!(to_str_hex(-(0xff00 as float)), ~"-ff00");
|
|
|
|
assert_eq!(to_str_hex(0.), ~"0");
|
|
|
|
assert_eq!(to_str_hex(15.), ~"f");
|
|
|
|
assert_eq!(to_str_hex(-15.), ~"-f");
|
|
|
|
assert_eq!(to_str_hex(0.9375), ~"0.f");
|
|
|
|
assert_eq!(to_str_hex(-0.9375), ~"-0.f");
|
|
|
|
assert_eq!(to_str_hex(infinity), ~"inf");
|
|
|
|
assert_eq!(to_str_hex(neg_infinity), ~"-inf");
|
|
|
|
assert_eq!(to_str_hex(NaN), ~"NaN");
|
|
|
|
assert_eq!(to_str_hex(0.), ~"0");
|
|
|
|
assert_eq!(to_str_hex(-0.), ~"-0");
|
2013-04-15 10:08:52 -05:00
|
|
|
}
|
2013-01-30 03:27:08 -06:00
|
|
|
|
2013-04-15 10:08:52 -05:00
|
|
|
#[test]
|
|
|
|
pub fn test_to_str_radix() {
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(to_str_radix(36., 36u), ~"10");
|
|
|
|
assert_eq!(to_str_radix(8.125, 2u), ~"1000.001");
|
2013-04-15 10:08:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_from_str_radix() {
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(from_str_radix(~"10", 36u), Some(36.));
|
|
|
|
assert_eq!(from_str_radix(~"1000.001", 2u), Some(8.125));
|
2013-04-15 10:08:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_to_str_inf() {
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(to_str_digits(infinity, 10u), ~"inf");
|
|
|
|
assert_eq!(to_str_digits(-infinity, 10u), ~"-inf");
|
2013-04-15 10:08:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
pub fn test_round() {
|
2013-04-18 10:37:21 -05:00
|
|
|
assert_eq!(round(5.8), 6.0);
|
|
|
|
assert_eq!(round(5.2), 5.0);
|
|
|
|
assert_eq!(round(3.0), 3.0);
|
|
|
|
assert_eq!(round(2.5), 3.0);
|
|
|
|
assert_eq!(round(-3.5), -4.0);
|
2013-04-15 10:08:52 -05:00
|
|
|
}
|
|
|
|
}
|
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:
|
|
|
|
//
|