diff options
Diffstat (limited to 'src/libcore/num/f64.rs')
| -rw-r--r-- | src/libcore/num/f64.rs | 194 |
1 files changed, 194 insertions, 0 deletions
diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 851697012fc..276aa13da71 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -19,6 +19,9 @@ use cmp; use libc::{c_double, c_int}; use libc; use num; +use option::Option; +use to_str; +use from_str; pub use cmath::c_double_targ_consts::*; @@ -357,6 +360,197 @@ impl f64: num::Round { } } +/** + * Section: String Conversions + */ + +/** + * Converts a float to a string + * + * # Arguments + * + * * num - The float value + */ +#[inline(always)] +pub pure fn to_str(num: f64) -> ~str { + let (r, _) = num::to_str_common( + &num, 10u, true, true, num::SignNeg, num::DigAll); + r +} + +/** + * Converts a float to a string in hexadecimal format + * + * # Arguments + * + * * num - The float value + */ +#[inline(always)] +pub pure fn to_str_hex(num: f64) -> ~str { + let (r, _) = num::to_str_common( + &num, 16u, true, true, num::SignNeg, num::DigAll); + r +} + +/** + * Converts a float to a string in a given radix + * + * # Arguments + * + * * num - The float value + * * radix - The base to use + */ +#[inline(always)] +pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str { + let (r, _) = num::to_str_common( + &num, rdx, true, true, num::SignNeg, num::DigAll); + r +} + +/** + * 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 + */ +#[inline(always)] +pub pure fn to_str_exact(num: f64, dig: uint) -> ~str { + let (r, _) = num::to_str_common( + &num, 10u, true, true, num::SignNeg, num::DigExact(dig)); + r +} + +/** + * Converts a float to a string with a maximum number of + * significant digits + * + * # Arguments + * + * * num - The float value + * * digits - The number of significant digits + */ +#[inline(always)] +pub pure fn to_str_digits(num: f64, dig: uint) -> ~str { + let (r, _) = num::to_str_common( + &num, 10u, true, true, num::SignNeg, num::DigMax(dig)); + r +} + +impl f64: to_str::ToStr { + #[inline(always)] + pure fn to_str() -> ~str { to_str_digits(self, 8) } +} + +impl f64: num::ToStrRadix { + #[inline(always)] + pure fn to_str_radix(&self, rdx: uint) -> ~str { + to_str_radix(*self, rdx) + } +} + +/** + * Convert a string in base 10 to a float. + * Accepts a optional decimal exponent. + * + * 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' + * * '.' (understood as 0) + * * '5.' + * * '.5', or, equivalently, '0.5' + * * '+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)] +pub pure fn from_str(num: &str) -> Option<f64> { + num::from_str_common(num, 10u, true, true, true, num::ExpDec, false) +} + +/** + * 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)] +pub pure fn from_str_hex(num: &str) -> Option<f64> { + num::from_str_common(num, 16u, true, true, true, num::ExpBin, false) +} + +/** + * 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)] +pub pure fn from_str_radix(num: &str, rdx: uint) -> Option<f64> { + num::from_str_common(num, rdx, true, true, false, num::ExpNone, false) +} + +impl f64: from_str::FromStr { + #[inline(always)] + static pure fn from_str(val: &str) -> Option<f64> { from_str(val) } +} + +impl f64: num::FromStrRadix { + #[inline(always)] + static pure fn from_str_radix(val: &str, rdx: uint) -> Option<f64> { + from_str_radix(val, rdx) + } +} + // // Local Variables: // mode: rust |
