Ver Fonte

Merge #201

201: Use standard library for float from_str_radix with radix 10 r=cuviper a=tspiteri

Fixes #198.

While at it, also make parsing of the `"-NaN"` string match the standard library.

Co-authored-by: Trevor Spiteri <tspiteri@ieee.org>
bors[bot] há 4 anos atrás
pai
commit
9c8f20ea24
1 ficheiros alterados com 8 adições e 0 exclusões
  1. 8 0
      src/lib.rs

+ 8 - 0
src/lib.rs

@@ -224,11 +224,19 @@ macro_rules! float_trait_impl {
                 use self::FloatErrorKind::*;
                 use self::ParseFloatError as PFE;
 
+                // Special case radix 10 to use more accurate standard library implementation
+                if radix == 10 {
+                    return src.parse().map_err(|_| PFE {
+                        kind: if src.is_empty() { Empty } else { Invalid },
+                    });
+                }
+
                 // Special values
                 match src {
                     "inf"   => return Ok(core::$t::INFINITY),
                     "-inf"  => return Ok(core::$t::NEG_INFINITY),
                     "NaN"   => return Ok(core::$t::NAN),
+                    "-NaN"  => return Ok(-core::$t::NAN),
                     _       => {},
                 }