#sorting #floats #compare #order

no-std float-ord

A total ordering for floating-point numbers

7 releases

Uses old Rust 2015

0.3.2 Jul 27, 2021
0.3.1 Apr 8, 2021
0.2.0 Jan 30, 2018
0.1.2 Jul 7, 2017
0.1.1 Mar 24, 2017

#74 in Rust patterns

Download history 38436/week @ 2024-07-21 39920/week @ 2024-07-28 40354/week @ 2024-08-04 43335/week @ 2024-08-11 36867/week @ 2024-08-18 44330/week @ 2024-08-25 45139/week @ 2024-09-01 41976/week @ 2024-09-08 36331/week @ 2024-09-15 41703/week @ 2024-09-22 40338/week @ 2024-09-29 45583/week @ 2024-10-06 40639/week @ 2024-10-13 42583/week @ 2024-10-20 42742/week @ 2024-10-27 41368/week @ 2024-11-03

170,199 downloads per month
Used in 463 crates (40 directly)

MIT/Apache

10KB
144 lines

Total ordering for floats

Build Status Crates.IO Documentation

A wrapper for floats that uses this ordering:

NaN | -Infinity | x < 0 | -0 | +0 | x > 0 | +Infinity | NaN

How does it work?

There is an old family magic spell that allows one to compare floating-point values without any floating-point work. Simply interpret the fp value as an unsigned integer, flip its sign bit (if positive) or all bits (if negative), and do the comparison normally.

The trick was developed on f32 and f64, but it should work on anything structured like the IEEE floats. Even the IEEE decimal formats.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps