|
1 | 1 | use bstr::ByteSlice; |
2 | | -use num_bigint::{BigInt, BigUint, Sign}; |
3 | | -use num_traits::{ToPrimitive, Zero}; |
| 2 | +use malachite_base::{num::conversion::traits::RoundingInto, rounding_modes::RoundingMode}; |
| 3 | +use malachite_bigint::{BigInt, BigUint, Sign}; |
| 4 | +use malachite_q::Rational; |
| 5 | +use num_traits::{One, ToPrimitive, Zero}; |
| 6 | + |
| 7 | +pub fn true_div(numerator: &BigInt, denominator: &BigInt) -> f64 { |
| 8 | + let val: f64 = Rational::from_integers_ref(numerator.into(), denominator.into()) |
| 9 | + .rounding_into(RoundingMode::Nearest); |
| 10 | + |
| 11 | + if val == f64::MAX || val == f64::MIN { |
| 12 | + // FIXME: not possible for available ratio? |
| 13 | + return f64::INFINITY; |
| 14 | + } |
| 15 | + val |
| 16 | +} |
| 17 | + |
| 18 | +pub fn float_to_ratio(value: f64) -> Option<(BigInt, BigInt)> { |
| 19 | + let sign = match std::cmp::PartialOrd::partial_cmp(&value, &0.0)? { |
| 20 | + std::cmp::Ordering::Less => Sign::Minus, |
| 21 | + std::cmp::Ordering::Equal => return Some((BigInt::zero(), BigInt::one())), |
| 22 | + std::cmp::Ordering::Greater => Sign::Plus, |
| 23 | + }; |
| 24 | + Rational::try_from(value).ok().map(|x| { |
| 25 | + let (numer, denom) = x.into_numerator_and_denominator(); |
| 26 | + ( |
| 27 | + BigInt::from_biguint(sign, numer.into()), |
| 28 | + BigUint::from(denom).into(), |
| 29 | + ) |
| 30 | + }) |
| 31 | +} |
4 | 32 |
|
5 | 33 | pub fn bytes_to_int(lit: &[u8], mut base: u32) -> Option<BigInt> { |
6 | 34 | // split sign |
|
0 commit comments