diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index a33a46192fdf..68e7c3c9df26 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -375,14 +375,36 @@ pub pure fn to_str_hex(num: f32) -> ~str { * * * num - The float value * * radix - The base to use + * + * # 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. */ #[inline(always)] pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str { - let (r, _) = num::to_str_common( + let (r, special) = num::to_str_common( &num, rdx, true, true, num::SignNeg, num::DigAll); + if special { die!(~"number has a special value, \ + try to_str_radix_special() if those are expected") } r } +/** + * 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)] +pub pure fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) { + num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll) +} + /** * Converts a float to a string with exactly the number of * provided significant digits diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 276aa13da71f..85f44d1b94f8 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -399,14 +399,36 @@ pub pure fn to_str_hex(num: f64) -> ~str { * * * num - The float value * * radix - The base to use + * + * # 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. */ #[inline(always)] pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str { - let (r, _) = num::to_str_common( + let (r, special) = num::to_str_common( &num, rdx, true, true, num::SignNeg, num::DigAll); + if special { die!(~"number has a special value, \ + try to_str_radix_special() if those are expected") } r } +/** + * 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)] +pub pure fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) { + num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll) +} + /** * Converts a float to a string with exactly the number of * provided significant digits diff --git a/src/libcore/num/float.rs b/src/libcore/num/float.rs index a73b5b9236c2..32c771742213 100644 --- a/src/libcore/num/float.rs +++ b/src/libcore/num/float.rs @@ -136,14 +136,36 @@ pub pure fn to_str_hex(num: float) -> ~str { * * * num - The float value * * radix - The base to use + * + * # 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. */ #[inline(always)] pub pure fn to_str_radix(num: float, radix: uint) -> ~str { - let (r, _) = num::to_str_common( + let (r, special) = num::to_str_common( &num, radix, true, true, num::SignNeg, num::DigAll); + if special { die!(~"number has a special value, \ + try to_str_radix_special() if those are expected") } r } +/** + * 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)] +pub pure fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) { + num::to_str_common(&num, radix, true, true, num::SignNeg, num::DigAll) +} + /** * Converts a float to a string with exactly the number of * provided significant digits