Skip to content

Commit 33afe9d

Browse files
committed
Simplify float rounding for use case
1 parent cfaf7f9 commit 33afe9d

File tree

1 file changed

+9
-21
lines changed

1 file changed

+9
-21
lines changed

src/parsing/iso8601.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -291,32 +291,20 @@ impl<const CONFIG: EncodedConfig> Iso8601<CONFIG> {
291291
/// implementation for `no_std`
292292
fn round(value: f64) -> f64 {
293293
#[cfg(feature = "std")]
294-
let rounded = value.round();
294+
{
295+
value.round()
296+
}
295297
#[cfg(not(feature = "std"))]
296-
let rounded = round_impl(value);
297-
rounded
298+
{
299+
round_impl(value)
300+
}
298301
}
299302

300303
#[cfg(not(feature = "std"))]
301304
#[allow(clippy::missing_docs_in_private_items)]
302-
// Based on num-trait for no_std feature
303-
#[inline]
304305
fn round_impl(value: f64) -> f64 {
305-
#[inline]
306-
fn fract(value: f64) -> f64 {
307-
if value == 0. { 0.0 } else { value % 1. }
308-
}
306+
debug_assert!(value.is_sign_positive() && !value.is_nan());
309307

310-
let one = 1.;
311-
let h = 0.5;
312-
let f = fract(value);
313-
if f.is_nan() || f == 0. {
314-
value
315-
} else if value > 0. {
316-
if f < h { value - f } else { value - f + one }
317-
} else if -f < h {
318-
value - f
319-
} else {
320-
value - f - one
321-
}
308+
let f = value % 1.;
309+
if f < 0.5 { value - f } else { value - f + 1. }
322310
}

0 commit comments

Comments
 (0)