about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMarvin Löbel <loebel.marvin@gmail.com>2013-02-03 17:27:01 +0100
committerBrian Anderson <banderson@mozilla.com>2013-02-03 15:37:25 -0800
commiteeb89c5012ef3e6a467835af67fed3ab8da3d84b (patch)
tree3dd73d5a8db05dc8b11e307873f5c75801128d00
parent974d5ac1e095379d63f546da7b8e9d61f7fdcc76 (diff)
downloadrust-eeb89c5012ef3e6a467835af67fed3ab8da3d84b.tar.gz
rust-eeb89c5012ef3e6a467835af67fed3ab8da3d84b.zip
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.
-rw-r--r--src/libcore/num/f32.rs24
-rw-r--r--src/libcore/num/f64.rs24
-rw-r--r--src/libcore/num/float.rs24
3 files changed, 69 insertions, 3 deletions
diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs
index a33a46192fd..68e7c3c9df2 100644
--- a/src/libcore/num/f32.rs
+++ b/src/libcore/num/f32.rs
@@ -375,15 +375,37 @@ 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 276aa13da71..85f44d1b94f 100644
--- a/src/libcore/num/f64.rs
+++ b/src/libcore/num/f64.rs
@@ -399,15 +399,37 @@ 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 a73b5b9236c..32c77174221 100644
--- a/src/libcore/num/float.rs
+++ b/src/libcore/num/float.rs
@@ -136,15 +136,37 @@ 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
  *