mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
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:
parent
974d5ac1e0
commit
eeb89c5012
@ -375,14 +375,36 @@ pub pure fn to_str_hex(num: f32) -> ~str {
|
|||||||
*
|
*
|
||||||
* * num - The float value
|
* * num - The float value
|
||||||
* * radix - The base to use
|
* * 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)]
|
#[inline(always)]
|
||||||
pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str {
|
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);
|
&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
|
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
|
* Converts a float to a string with exactly the number of
|
||||||
* provided significant digits
|
* provided significant digits
|
||||||
|
@ -399,14 +399,36 @@ pub pure fn to_str_hex(num: f64) -> ~str {
|
|||||||
*
|
*
|
||||||
* * num - The float value
|
* * num - The float value
|
||||||
* * radix - The base to use
|
* * 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)]
|
#[inline(always)]
|
||||||
pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str {
|
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);
|
&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
|
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
|
* Converts a float to a string with exactly the number of
|
||||||
* provided significant digits
|
* provided significant digits
|
||||||
|
@ -136,14 +136,36 @@ pub pure fn to_str_hex(num: float) -> ~str {
|
|||||||
*
|
*
|
||||||
* * num - The float value
|
* * num - The float value
|
||||||
* * radix - The base to use
|
* * 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)]
|
#[inline(always)]
|
||||||
pub pure fn to_str_radix(num: float, radix: uint) -> ~str {
|
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);
|
&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
|
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
|
* Converts a float to a string with exactly the number of
|
||||||
* provided significant digits
|
* provided significant digits
|
||||||
|
Loading…
Reference in New Issue
Block a user