This commit is contained in:
Quaternions 2023-10-14 11:22:36 -07:00
parent 8c90eb1d94
commit 8fba543684

View File

@ -114,31 +114,31 @@ impl Ratio64{
//from num_traits crate //from num_traits crate
#[inline] #[inline]
fn integer_decode_f32(f: f32) -> (u64, i16, i8) { fn integer_decode_f32(f: f32) -> (u64, i16, i8) {
let bits: u32 = f.to_bits(); let bits: u32 = f.to_bits();
let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 }; let sign: i8 = if bits >> 31 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 23) & 0xff) as i16; let mut exponent: i16 = ((bits >> 23) & 0xff) as i16;
let mantissa = if exponent == 0 { let mantissa = if exponent == 0 {
(bits & 0x7fffff) << 1 (bits & 0x7fffff) << 1
} else { } else {
(bits & 0x7fffff) | 0x800000 (bits & 0x7fffff) | 0x800000
}; };
// Exponent bias + mantissa shift // Exponent bias + mantissa shift
exponent -= 127 + 23; exponent -= 127 + 23;
(mantissa as u64, exponent, sign) (mantissa as u64, exponent, sign)
} }
#[inline] #[inline]
fn integer_decode_f64(f: f64) -> (u64, i16, i8) { fn integer_decode_f64(f: f64) -> (u64, i16, i8) {
let bits: u64 = f.to_bits(); let bits: u64 = f.to_bits();
let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 }; let sign: i8 = if bits >> 63 == 0 { 1 } else { -1 };
let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16; let mut exponent: i16 = ((bits >> 52) & 0x7ff) as i16;
let mantissa = if exponent == 0 { let mantissa = if exponent == 0 {
(bits & 0xfffffffffffff) << 1 (bits & 0xfffffffffffff) << 1
} else { } else {
(bits & 0xfffffffffffff) | 0x10000000000000 (bits & 0xfffffffffffff) | 0x10000000000000
}; };
// Exponent bias + mantissa shift // Exponent bias + mantissa shift
exponent -= 1023 + 52; exponent -= 1023 + 52;
(mantissa, exponent, sign) (mantissa, exponent, sign)
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Ratio64TryFromFloatError{ pub enum Ratio64TryFromFloatError{