preserves/implementations/rust/oo/src/float.rs

26 lines
708 B
Rust
Raw Normal View History

2022-10-31 20:21:28 +00:00
use std::cmp::Ordering;
2022-11-02 16:17:46 +00:00
pub fn cmp_f32(a: f32, b: f32) -> Ordering {
let mut a: u32 = a.to_bits();
let mut b: u32 = b.to_bits();
if a & 0x8000_0000 != 0 { a ^= 0x7fff_ffff; }
if b & 0x8000_0000 != 0 { b ^= 0x7fff_ffff; }
(a as i32).cmp(&(b as i32))
2022-10-31 20:21:28 +00:00
}
2022-11-02 16:17:46 +00:00
pub fn eq_f32(a: f32, b: f32) -> bool {
a.to_bits() == b.to_bits()
2022-10-31 20:21:28 +00:00
}
2022-11-02 16:17:46 +00:00
pub fn cmp_f64(a: f64, b: f64) -> Ordering {
let mut a: u64 = a.to_bits();
let mut b: u64 = b.to_bits();
if a & 0x8000_0000_0000_0000 != 0 { a ^= 0x7fff_ffff_ffff_ffff; }
if b & 0x8000_0000_0000_0000 != 0 { b ^= 0x7fff_ffff_ffff_ffff; }
(a as i64).cmp(&(b as i64))
2022-10-31 20:21:28 +00:00
}
2022-11-02 16:17:46 +00:00
pub fn eq_f64(a: f64, b: f64) -> bool {
a.to_bits() == b.to_bits()
2022-10-31 20:21:28 +00:00
}