Num-cmp: Comparison between differently typed numbers


#1

I was previously asked about how to do that, and I could only reply that you have to convert one operand to another, with a care not to truncate or lose precision. I knew that some cases are tricky enough that I felt like writing a crate for that… Well, here it is:

https://crates.io/crates/num-cmp

Clearly this is a niche crate but cannot be done without lots of care anyway. It also supports i128 and u128 with --features i128 (requires nightly). (Interesting fact: f32's maximal finite exponent is 127, so f32 and u128's maximal values are roughly same.)

I have tested it with tons of edge values and I’m pretty confident that it is mostly correct, but please do not take my words granted. :smiley: Any idea or PR to test it better would be welcomed.


#2

It would probably a good idea to use quickcheck, or better cargo-fuzz. As a test I would probably compare results to a naive version that just casts everything too i128 and looks what happens (for the cases where this naive version is expected to work).