Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extended the result of to_f32/to_f64 with infinity #163

Merged
merged 2 commits into from Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ categories = [ "algorithms", "data-structures", "science" ]
license = "MIT/Apache-2.0"
name = "num-bigint"
repository = "https://github.com/rust-num/num-bigint"
version = "0.3.0"
version = "0.3.1"
readme = "README.md"
build = "build.rs"
exclude = ["/bors.toml", "/ci/*", "/.github/*"]
Expand Down
93 changes: 46 additions & 47 deletions src/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1687,19 +1687,22 @@ impl Roots for BigUint {
let max_bits = bits / n64 + 1;

#[cfg(feature = "std")]
let guess = if let Some(f) = self.to_f64() {
// We fit in `f64` (lossy), so get a better initial guess from that.
BigUint::from_f64((f.ln() / f64::from(n)).exp()).unwrap()
} else {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2ⁿᵏ), its nth root ≈ (ⁿ√x * 2ᵏ)
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = extra_bits.div_ceil(&n64);
let scale = root_scale * n64;
if scale < bits && bits - scale > n64 {
(self >> scale).nth_root(n) << root_scale
} else {
BigUint::one() << max_bits
let guess = match self.to_f64() {
Some(f) if f.is_finite() => {
// We fit in `f64` (lossy), so get a better initial guess from that.
BigUint::from_f64((f.ln() / f64::from(n)).exp()).unwrap()
}
_ => {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2ⁿᵏ), its nth root ≈ (ⁿ√x * 2ᵏ)
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = extra_bits.div_ceil(&n64);
let scale = root_scale * n64;
if scale < bits && bits - scale > n64 {
(self >> scale).nth_root(n) << root_scale
} else {
BigUint::one() << max_bits
}
}
};

Expand Down Expand Up @@ -1730,16 +1733,19 @@ impl Roots for BigUint {
let max_bits = bits / 2 + 1;

#[cfg(feature = "std")]
let guess = if let Some(f) = self.to_f64() {
// We fit in `f64` (lossy), so get a better initial guess from that.
BigUint::from_f64(f.sqrt()).unwrap()
} else {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2²ᵏ), its sqrt ≈ (√x * 2ᵏ)
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = (extra_bits + 1) / 2;
let scale = root_scale * 2;
(self >> scale).sqrt() << root_scale
let guess = match self.to_f64() {
Some(f) if f.is_finite() => {
// We fit in `f64` (lossy), so get a better initial guess from that.
BigUint::from_f64(f.sqrt()).unwrap()
}
_ => {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2²ᵏ), its sqrt ≈ (√x * 2ᵏ)
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = (extra_bits + 1) / 2;
let scale = root_scale * 2;
(self >> scale).sqrt() << root_scale
}
};

#[cfg(not(feature = "std"))]
Expand All @@ -1766,16 +1772,19 @@ impl Roots for BigUint {
let max_bits = bits / 3 + 1;

#[cfg(feature = "std")]
let guess = if let Some(f) = self.to_f64() {
// We fit in `f64` (lossy), so get a better initial guess from that.
BigUint::from_f64(f.cbrt()).unwrap()
} else {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2³ᵏ), its cbrt ≈ (∛x * 2ᵏ)
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = (extra_bits + 2) / 3;
let scale = root_scale * 3;
(self >> scale).cbrt() << root_scale
let guess = match self.to_f64() {
Some(f) if f.is_finite() => {
// We fit in `f64` (lossy), so get a better initial guess from that.
BigUint::from_f64(f.cbrt()).unwrap()
}
_ => {
// Try to guess by scaling down such that it does fit in `f64`.
// With some (x * 2³ᵏ), its cbrt ≈ (∛x * 2ᵏ)
let extra_bits = bits - (f64::MAX_EXP as u64 - 1);
let root_scale = (extra_bits + 2) / 3;
let scale = root_scale * 3;
(self >> scale).cbrt() << root_scale
}
};

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -1870,14 +1879,9 @@ impl ToPrimitive for BigUint {
let exponent = self.bits() - u64::from(fls(mantissa));

if exponent > f32::MAX_EXP as u64 {
None
Some(f32::INFINITY)
} else {
let ret = (mantissa as f32) * 2.0f32.powi(exponent as i32);
if ret.is_infinite() {
None
} else {
Some(ret)
}
Some((mantissa as f32) * 2.0f32.powi(exponent as i32))
}
}

Expand All @@ -1887,14 +1891,9 @@ impl ToPrimitive for BigUint {
let exponent = self.bits() - u64::from(fls(mantissa));

if exponent > f64::MAX_EXP as u64 {
None
Some(f64::INFINITY)
} else {
let ret = (mantissa as f64) * 2.0f64.powi(exponent as i32);
if ret.is_infinite() {
None
} else {
Some(ret)
}
Some((mantissa as f64) * 2.0f64.powi(exponent as i32))
}
}
}
Expand Down
42 changes: 30 additions & 12 deletions tests/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,14 +448,23 @@ fn test_convert_f32() {
// largest BigInt that will round to a finite f32 value
let big_num = (BigInt::one() << 128u8) - 1u8 - (BigInt::one() << (128u8 - 25));
assert_eq!(big_num.to_f32(), Some(f32::MAX));
assert_eq!((&big_num + 1u8).to_f32(), None);
assert_eq!((&big_num + 1u8).to_f32(), Some(f32::INFINITY));
assert_eq!((-&big_num).to_f32(), Some(f32::MIN));
assert_eq!(((-&big_num) - 1u8).to_f32(), None);
assert_eq!(((-&big_num) - 1u8).to_f32(), Some(f32::NEG_INFINITY));

assert_eq!(((BigInt::one() << 128u8) - 1u8).to_f32(), None);
assert_eq!((BigInt::one() << 128u8).to_f32(), None);
assert_eq!((-((BigInt::one() << 128u8) - 1u8)).to_f32(), None);
assert_eq!((-(BigInt::one() << 128u8)).to_f32(), None);
assert_eq!(
((BigInt::one() << 128u8) - 1u8).to_f32(),
Some(f32::INFINITY)
);
assert_eq!((BigInt::one() << 128u8).to_f32(), Some(f32::INFINITY));
assert_eq!(
(-((BigInt::one() << 128u8) - 1u8)).to_f32(),
Some(f32::NEG_INFINITY)
);
assert_eq!(
(-(BigInt::one() << 128u8)).to_f32(),
Some(f32::NEG_INFINITY)
);
}

#[test]
Expand Down Expand Up @@ -529,14 +538,23 @@ fn test_convert_f64() {
// largest BigInt that will round to a finite f64 value
let big_num = (BigInt::one() << 1024u16) - 1u8 - (BigInt::one() << (1024u16 - 54));
assert_eq!(big_num.to_f64(), Some(f64::MAX));
assert_eq!((&big_num + 1u8).to_f64(), None);
assert_eq!((&big_num + 1u8).to_f64(), Some(f64::INFINITY));
assert_eq!((-&big_num).to_f64(), Some(f64::MIN));
assert_eq!(((-&big_num) - 1u8).to_f64(), None);
assert_eq!(((-&big_num) - 1u8).to_f64(), Some(f64::NEG_INFINITY));

assert_eq!(((BigInt::one() << 1024u16) - 1u8).to_f64(), None);
assert_eq!((BigInt::one() << 1024u16).to_f64(), None);
assert_eq!((-((BigInt::one() << 1024u16) - 1u8)).to_f64(), None);
assert_eq!((-(BigInt::one() << 1024u16)).to_f64(), None);
assert_eq!(
((BigInt::one() << 1024u16) - 1u8).to_f64(),
Some(f64::INFINITY)
);
assert_eq!((BigInt::one() << 1024u16).to_f64(), Some(f64::INFINITY));
assert_eq!(
(-((BigInt::one() << 1024u16) - 1u8)).to_f64(),
Some(f64::NEG_INFINITY)
);
assert_eq!(
(-(BigInt::one() << 1024u16)).to_f64(),
Some(f64::NEG_INFINITY)
);
}

#[test]
Expand Down
18 changes: 12 additions & 6 deletions tests/biguint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,13 @@ fn test_convert_f32() {
// largest BigUint that will round to a finite f32 value
let big_num = (BigUint::one() << 128u8) - 1u8 - (BigUint::one() << (128u8 - 25));
assert_eq!(big_num.to_f32(), Some(f32::MAX));
assert_eq!((big_num + 1u8).to_f32(), None);
assert_eq!((big_num + 1u8).to_f32(), Some(f32::INFINITY));

assert_eq!(((BigUint::one() << 128u8) - 1u8).to_f32(), None);
assert_eq!((BigUint::one() << 128u8).to_f32(), None);
assert_eq!(
((BigUint::one() << 128u8) - 1u8).to_f32(),
Some(f32::INFINITY)
);
assert_eq!((BigUint::one() << 128u8).to_f32(), Some(f32::INFINITY));
}

#[test]
Expand Down Expand Up @@ -746,10 +749,13 @@ fn test_convert_f64() {
// largest BigUint that will round to a finite f64 value
let big_num = (BigUint::one() << 1024u16) - 1u8 - (BigUint::one() << (1024u16 - 54));
assert_eq!(big_num.to_f64(), Some(f64::MAX));
assert_eq!((big_num + 1u8).to_f64(), None);
assert_eq!((big_num + 1u8).to_f64(), Some(f64::INFINITY));

assert_eq!(((BigUint::one() << 1024u16) - 1u8).to_f64(), None);
assert_eq!((BigUint::one() << 1024u16).to_f64(), None);
assert_eq!(
((BigUint::one() << 1024u16) - 1u8).to_f64(),
Some(f64::INFINITY)
);
assert_eq!((BigUint::one() << 1024u16).to_f64(), Some(f64::INFINITY));
}

#[test]
Expand Down