Skip to content

Commit 854fc70

Browse files
committed
Touch up PR 1347
1 parent 80a5406 commit 854fc70

2 files changed

Lines changed: 7 additions & 11 deletions

File tree

‎src/number.rs‎

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -311,15 +311,13 @@ impl Number {
311311

312312
pub(crate) fn as_f32(&self) -> Option<f32> {
313313
#[cfg(not(feature = "arbitrary_precision"))]
314-
let float = match self.n {
315-
N::PosInt(n) => n as f32,
316-
N::NegInt(n) => n as f32,
317-
N::Float(n) => n as f32,
318-
};
314+
match self.n {
315+
N::PosInt(n) => Some(n as f32),
316+
N::NegInt(n) => Some(n as f32),
317+
N::Float(n) => Some(n as f32).filter(|float| float.is_finite()),
318+
}
319319
#[cfg(feature = "arbitrary_precision")]
320-
let float = self.n.parse::<f32>().ok()?;
321-
// A finite number outside the range of f32 rounds to infinity; it is not equal to any f32.
322-
Some(float).filter(|float| float.is_finite())
320+
self.n.parse::<f32>().ok().filter(|float| float.is_finite())
323321
}
324322

325323
pub(crate) fn from_f32(f: f32) -> Option<Number> {

‎tests/test.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,9 +2197,7 @@ fn test_partialeq_number() {
21972197

21982198
#[test]
21992199
fn test_partialeq_f32_out_of_range() {
2200-
// A number outside the range of f32 is not equal to an infinite f32 (JSON has no infinity,
2201-
// and the comparison with f64::INFINITY is already false). Without arbitrary_precision the
2202-
// value was cast with `as f32`, which rounds it to infinity.
2200+
// A number outside the range of f32 is not equal to an infinite f32.
22032201
for (text, inf) in [("1e300", f32::INFINITY), ("-1e39", f32::NEG_INFINITY)] {
22042202
let value: Value = from_str(text).unwrap();
22052203
assert_ne!(value, inf);

0 commit comments

Comments
 (0)