Solved float, f32 and f64 `to_str_radix()` special value ambiguity.

Calling it on a special value now causes a failure, however `to_str_radix_special()` is provided which can be
used if those values are expected, and which returns a tupel to allow differentating them.
This commit is contained in:
Marvin Löbel 2013-02-03 17:27:01 +01:00 committed by Brian Anderson
parent 974d5ac1e0
commit eeb89c5012
3 changed files with 69 additions and 3 deletions

View File

@ -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

View File

@ -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

View File

@ -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