Skip to content

Commit b03a275

Browse files
committed
auto merge of #18713 : juxiliary/rust/master, r=alexcrichton
* `from_str_radix_float` gives incorrect results for negative float strings. Changes the accumulator used to start at -0.0 instead of -1.0. * Adds missing tests
2 parents 45cbdec + 18c328d commit b03a275

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

src/libstd/num/strconv.rs

+17-1
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub fn from_str_radix_float<T: Float>(src: &str, radix: uint) -> Option<T> {
451451
};
452452

453453
// The significand to accumulate
454-
let mut sig = if is_positive { _0 } else { -_1 };
454+
let mut sig = if is_positive { _0 } else { -_0 };
455455
// Necessary to detect overflow
456456
let mut prev_sig = sig;
457457
let mut cs = src.chars().enumerate();
@@ -647,6 +647,22 @@ mod test {
647647
let fe : Option<f32> = from_str_radix_float("1e40", 10);
648648
assert_eq!(fe, Some(Float::infinity()))
649649
}
650+
651+
#[test]
652+
fn test_from_str_radix_float() {
653+
let x1 : Option<f64> = from_str_radix_float("-123.456", 10);
654+
assert_eq!(x1, Some(-123.456));
655+
let x2 : Option<f32> = from_str_radix_float("123.456", 10);
656+
assert_eq!(x2, Some(123.456));
657+
let x3 : Option<f32> = from_str_radix_float("-0.0", 10);
658+
assert_eq!(x3, Some(-0.0));
659+
let x4 : Option<f32> = from_str_radix_float("0.0", 10);
660+
assert_eq!(x4, Some(0.0));
661+
let x4 : Option<f32> = from_str_radix_float("1.0", 10);
662+
assert_eq!(x4, Some(1.0));
663+
let x5 : Option<f32> = from_str_radix_float("-1.0", 10);
664+
assert_eq!(x5, Some(-1.0));
665+
}
650666
}
651667

652668
#[cfg(test)]

0 commit comments

Comments
 (0)