Skip to content

Commit 0dc843b

Browse files
committed
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.
1 parent 98a1eb2 commit 0dc843b

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

src/libcore/num/f32.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,36 @@ pub pure fn to_str_hex(num: f32) -> ~str {
375375
*
376376
* * num - The float value
377377
* * radix - The base to use
378+
*
379+
* # Failure
380+
*
381+
* Fails if called on a special value like `inf`, `-inf` or `NaN` due to
382+
* possible misinterpretation of the result at higher bases. If those values
383+
* are expected, use `to_str_radix_special()` instead.
378384
*/
379385
#[inline(always)]
380386
pub pure fn to_str_radix(num: f32, rdx: uint) -> ~str {
381-
let (r, _) = num::to_str_common(
387+
let (r, special) = num::to_str_common(
382388
&num, rdx, true, true, num::SignNeg, num::DigAll);
389+
if special { die!(~"number has a special value, \
390+
try to_str_radix_special() if those are expected") }
383391
r
384392
}
385393

394+
/**
395+
* Converts a float to a string in a given radix, and a flag indicating
396+
* whether it's a special value
397+
*
398+
* # Arguments
399+
*
400+
* * num - The float value
401+
* * radix - The base to use
402+
*/
403+
#[inline(always)]
404+
pub pure fn to_str_radix_special(num: f32, rdx: uint) -> (~str, bool) {
405+
num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll)
406+
}
407+
386408
/**
387409
* Converts a float to a string with exactly the number of
388410
* provided significant digits

src/libcore/num/f64.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,36 @@ pub pure fn to_str_hex(num: f64) -> ~str {
399399
*
400400
* * num - The float value
401401
* * radix - The base to use
402+
*
403+
* # Failure
404+
*
405+
* Fails if called on a special value like `inf`, `-inf` or `NaN` due to
406+
* possible misinterpretation of the result at higher bases. If those values
407+
* are expected, use `to_str_radix_special()` instead.
402408
*/
403409
#[inline(always)]
404410
pub pure fn to_str_radix(num: f64, rdx: uint) -> ~str {
405-
let (r, _) = num::to_str_common(
411+
let (r, special) = num::to_str_common(
406412
&num, rdx, true, true, num::SignNeg, num::DigAll);
413+
if special { die!(~"number has a special value, \
414+
try to_str_radix_special() if those are expected") }
407415
r
408416
}
409417

418+
/**
419+
* Converts a float to a string in a given radix, and a flag indicating
420+
* whether it's a special value
421+
*
422+
* # Arguments
423+
*
424+
* * num - The float value
425+
* * radix - The base to use
426+
*/
427+
#[inline(always)]
428+
pub pure fn to_str_radix_special(num: f64, rdx: uint) -> (~str, bool) {
429+
num::to_str_common(&num, rdx, true, true, num::SignNeg, num::DigAll)
430+
}
431+
410432
/**
411433
* Converts a float to a string with exactly the number of
412434
* provided significant digits

src/libcore/num/float.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,36 @@ pub pure fn to_str_hex(num: float) -> ~str {
136136
*
137137
* * num - The float value
138138
* * radix - The base to use
139+
*
140+
* # Failure
141+
*
142+
* Fails if called on a special value like `inf`, `-inf` or `NaN` due to
143+
* possible misinterpretation of the result at higher bases. If those values
144+
* are expected, use `to_str_radix_special()` instead.
139145
*/
140146
#[inline(always)]
141147
pub pure fn to_str_radix(num: float, radix: uint) -> ~str {
142-
let (r, _) = num::to_str_common(
148+
let (r, special) = num::to_str_common(
143149
&num, radix, true, true, num::SignNeg, num::DigAll);
150+
if special { die!(~"number has a special value, \
151+
try to_str_radix_special() if those are expected") }
144152
r
145153
}
146154
155+
/**
156+
* Converts a float to a string in a given radix, and a flag indicating
157+
* whether it's a special value
158+
*
159+
* # Arguments
160+
*
161+
* * num - The float value
162+
* * radix - The base to use
163+
*/
164+
#[inline(always)]
165+
pub pure fn to_str_radix_special(num: float, radix: uint) -> (~str, bool) {
166+
num::to_str_common(&num, radix, true, true, num::SignNeg, num::DigAll)
167+
}
168+
147169
/**
148170
* Converts a float to a string with exactly the number of
149171
* provided significant digits

0 commit comments

Comments
 (0)