@@ -1687,19 +1687,22 @@ impl Roots for BigUint {
1687
1687
let max_bits = bits / n64 + 1 ;
1688
1688
1689
1689
#[ cfg( feature = "std" ) ]
1690
- let guess = if let Some ( f) = self . to_f64 ( ) {
1691
- // We fit in `f64` (lossy), so get a better initial guess from that.
1692
- BigUint :: from_f64 ( ( f. ln ( ) / f64:: from ( n) ) . exp ( ) ) . unwrap ( )
1693
- } else {
1694
- // Try to guess by scaling down such that it does fit in `f64`.
1695
- // With some (x * 2ⁿᵏ), its nth root ≈ (ⁿ√x * 2ᵏ)
1696
- let extra_bits = bits - ( f64:: MAX_EXP as u64 - 1 ) ;
1697
- let root_scale = extra_bits. div_ceil ( & n64) ;
1698
- let scale = root_scale * n64;
1699
- if scale < bits && bits - scale > n64 {
1700
- ( self >> scale) . nth_root ( n) << root_scale
1701
- } else {
1702
- BigUint :: one ( ) << max_bits
1690
+ let guess = match self . to_f64 ( ) {
1691
+ Some ( f) if f. is_finite ( ) => {
1692
+ // We fit in `f64` (lossy), so get a better initial guess from that.
1693
+ BigUint :: from_f64 ( ( f. ln ( ) / f64:: from ( n) ) . exp ( ) ) . unwrap ( )
1694
+ }
1695
+ _ => {
1696
+ // Try to guess by scaling down such that it does fit in `f64`.
1697
+ // With some (x * 2ⁿᵏ), its nth root ≈ (ⁿ√x * 2ᵏ)
1698
+ let extra_bits = bits - ( f64:: MAX_EXP as u64 - 1 ) ;
1699
+ let root_scale = extra_bits. div_ceil ( & n64) ;
1700
+ let scale = root_scale * n64;
1701
+ if scale < bits && bits - scale > n64 {
1702
+ ( self >> scale) . nth_root ( n) << root_scale
1703
+ } else {
1704
+ BigUint :: one ( ) << max_bits
1705
+ }
1703
1706
}
1704
1707
} ;
1705
1708
@@ -1730,16 +1733,19 @@ impl Roots for BigUint {
1730
1733
let max_bits = bits / 2 + 1 ;
1731
1734
1732
1735
#[ cfg( feature = "std" ) ]
1733
- let guess = if let Some ( f) = self . to_f64 ( ) {
1734
- // We fit in `f64` (lossy), so get a better initial guess from that.
1735
- BigUint :: from_f64 ( f. sqrt ( ) ) . unwrap ( )
1736
- } else {
1737
- // Try to guess by scaling down such that it does fit in `f64`.
1738
- // With some (x * 2²ᵏ), its sqrt ≈ (√x * 2ᵏ)
1739
- let extra_bits = bits - ( f64:: MAX_EXP as u64 - 1 ) ;
1740
- let root_scale = ( extra_bits + 1 ) / 2 ;
1741
- let scale = root_scale * 2 ;
1742
- ( self >> scale) . sqrt ( ) << root_scale
1736
+ let guess = match self . to_f64 ( ) {
1737
+ Some ( f) if f. is_finite ( ) => {
1738
+ // We fit in `f64` (lossy), so get a better initial guess from that.
1739
+ BigUint :: from_f64 ( f. sqrt ( ) ) . unwrap ( )
1740
+ }
1741
+ _ => {
1742
+ // Try to guess by scaling down such that it does fit in `f64`.
1743
+ // With some (x * 2²ᵏ), its sqrt ≈ (√x * 2ᵏ)
1744
+ let extra_bits = bits - ( f64:: MAX_EXP as u64 - 1 ) ;
1745
+ let root_scale = ( extra_bits + 1 ) / 2 ;
1746
+ let scale = root_scale * 2 ;
1747
+ ( self >> scale) . sqrt ( ) << root_scale
1748
+ }
1743
1749
} ;
1744
1750
1745
1751
#[ cfg( not( feature = "std" ) ) ]
@@ -1766,16 +1772,19 @@ impl Roots for BigUint {
1766
1772
let max_bits = bits / 3 + 1 ;
1767
1773
1768
1774
#[ cfg( feature = "std" ) ]
1769
- let guess = if let Some ( f) = self . to_f64 ( ) {
1770
- // We fit in `f64` (lossy), so get a better initial guess from that.
1771
- BigUint :: from_f64 ( f. cbrt ( ) ) . unwrap ( )
1772
- } else {
1773
- // Try to guess by scaling down such that it does fit in `f64`.
1774
- // With some (x * 2³ᵏ), its cbrt ≈ (∛x * 2ᵏ)
1775
- let extra_bits = bits - ( f64:: MAX_EXP as u64 - 1 ) ;
1776
- let root_scale = ( extra_bits + 2 ) / 3 ;
1777
- let scale = root_scale * 3 ;
1778
- ( self >> scale) . cbrt ( ) << root_scale
1775
+ let guess = match self . to_f64 ( ) {
1776
+ Some ( f) if f. is_finite ( ) => {
1777
+ // We fit in `f64` (lossy), so get a better initial guess from that.
1778
+ BigUint :: from_f64 ( f. cbrt ( ) ) . unwrap ( )
1779
+ }
1780
+ _ => {
1781
+ // Try to guess by scaling down such that it does fit in `f64`.
1782
+ // With some (x * 2³ᵏ), its cbrt ≈ (∛x * 2ᵏ)
1783
+ let extra_bits = bits - ( f64:: MAX_EXP as u64 - 1 ) ;
1784
+ let root_scale = ( extra_bits + 2 ) / 3 ;
1785
+ let scale = root_scale * 3 ;
1786
+ ( self >> scale) . cbrt ( ) << root_scale
1787
+ }
1779
1788
} ;
1780
1789
1781
1790
#[ cfg( not( feature = "std" ) ) ]
@@ -1870,14 +1879,9 @@ impl ToPrimitive for BigUint {
1870
1879
let exponent = self . bits ( ) - u64:: from ( fls ( mantissa) ) ;
1871
1880
1872
1881
if exponent > f32:: MAX_EXP as u64 {
1873
- None
1882
+ Some ( f32 :: INFINITY )
1874
1883
} else {
1875
- let ret = ( mantissa as f32 ) * 2.0f32 . powi ( exponent as i32 ) ;
1876
- if ret. is_infinite ( ) {
1877
- None
1878
- } else {
1879
- Some ( ret)
1880
- }
1884
+ Some ( ( mantissa as f32 ) * 2.0f32 . powi ( exponent as i32 ) )
1881
1885
}
1882
1886
}
1883
1887
@@ -1887,14 +1891,9 @@ impl ToPrimitive for BigUint {
1887
1891
let exponent = self . bits ( ) - u64:: from ( fls ( mantissa) ) ;
1888
1892
1889
1893
if exponent > f64:: MAX_EXP as u64 {
1890
- None
1894
+ Some ( f64 :: INFINITY )
1891
1895
} else {
1892
- let ret = ( mantissa as f64 ) * 2.0f64 . powi ( exponent as i32 ) ;
1893
- if ret. is_infinite ( ) {
1894
- None
1895
- } else {
1896
- Some ( ret)
1897
- }
1896
+ Some ( ( mantissa as f64 ) * 2.0f64 . powi ( exponent as i32 ) )
1898
1897
}
1899
1898
}
1900
1899
}
0 commit comments