implement it - easier than expected
This commit is contained in:
@@ -41,8 +41,17 @@ pub fn ratio_from_float(value:f64)->Result<Ratio64,RatioFromFloatError>{
|
||||
// value = sign * mantissa * 2 ^ exponent
|
||||
let (mantissa, exponent, sign) = f64_into_parts(value);
|
||||
|
||||
if 0<=exponent{
|
||||
// we know value < i64::MIN, so it must just be an integer.
|
||||
return Ok(Ratio64::new((sign as i64*mantissa as i64)<<exponent,1).unwrap());
|
||||
}
|
||||
|
||||
if exponent< -62{
|
||||
// very small number is not representable
|
||||
return Ok(Ratio64::ZERO);
|
||||
}
|
||||
|
||||
// the idea: create exact float num/den ratio, and compute continued fraction if it doesn't fit
|
||||
|
||||
|
||||
return Ok(())
|
||||
// oh actually we can just straight up represent all floats exactly. ok.
|
||||
Ok(Ratio64::new(sign as i64*mantissa as i64, 1<<-exponent).unwrap())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user