A native Rust next after float function, which is provided as a trait for f32 and f64 types. It steps to the next representable floating point, even if it is subnormal. If a subnormal is undesired, users should handle that eventuality themselves.
In specific edge cases the following decisions have been made:
Please see the unit tests for the actual behavior in various other exceptional cases.
This code uses the ToBits and FromBits functions from f32 and f64. Those both simply wrap unsafe { mem::transmute(self) }
/ unsafe { mem::transmute(v) }
to convert a f32/f64 to u32/u64. The docs for those functions, however, claim that they are safe and that "the safety issues with sNaN were overblown!"
PR's and other helpful input are welcome.
```rust use floatnextafter::NextAfter;
// Large numbers let bignum = 16237485966.00000437586943f64; let next = bignum.nextafter(&std::f64::INFINITY); asserteq!(next, 16237485966.000006f64);
// Expected handling of 1.0 let one = 1f64; let next = one.nextafter(&std::f64::INFINITY); asserteq!(next, 1f64 + std::f64::EPSILON);
// Tiny (negative) numbers let zero = 0f32; let next = zero.nextafter(&std::f32::NEGINFINITY); asserteq!(next, -0.000000000000000000000000000000000000000000001_f32);
// Equal source/dest (even -0 == 0) let zero = 0f64; let next = zero.nextafter(&-0f64); asserteq!(next, -0_f64); ```