@@ -57,6 +57,10 @@ pub struct Ratio<T> {
57
57
}
58
58
59
59
/// Alias for a `Ratio` of machine-sized integers.
60
+ #[ deprecated(
61
+ since = "0.4.0" ,
62
+ note = "it's better to use a specific size, like `Rational32` or `Rational64`"
63
+ ) ]
60
64
pub type Rational = Ratio < isize > ;
61
65
/// Alias for a `Ratio` of 32-bit-sized integers.
62
66
pub type Rational32 = Ratio < i32 > ;
@@ -1617,100 +1621,100 @@ mod test {
1617
1621
use super :: BigInt ;
1618
1622
#[ cfg( feature = "num-bigint" ) ]
1619
1623
use super :: BigRational ;
1620
- use super :: { Ratio , Rational , Rational64 } ;
1624
+ use super :: { Ratio , Rational64 } ;
1621
1625
1622
1626
use core:: f64;
1623
1627
use core:: i32;
1624
- use core:: isize ;
1628
+ use core:: i64 ;
1625
1629
use core:: str:: FromStr ;
1626
1630
use num_integer:: Integer ;
1627
1631
use num_traits:: ToPrimitive ;
1628
1632
use num_traits:: { FromPrimitive , One , Pow , Signed , Zero } ;
1629
1633
1630
- pub const _0: Rational = Ratio { numer : 0 , denom : 1 } ;
1631
- pub const _1: Rational = Ratio { numer : 1 , denom : 1 } ;
1632
- pub const _2: Rational = Ratio { numer : 2 , denom : 1 } ;
1633
- pub const _NEG2: Rational = Ratio {
1634
+ pub const _0: Rational64 = Ratio { numer : 0 , denom : 1 } ;
1635
+ pub const _1: Rational64 = Ratio { numer : 1 , denom : 1 } ;
1636
+ pub const _2: Rational64 = Ratio { numer : 2 , denom : 1 } ;
1637
+ pub const _NEG2: Rational64 = Ratio {
1634
1638
numer : -2 ,
1635
1639
denom : 1 ,
1636
1640
} ;
1637
- pub const _8: Rational = Ratio { numer : 8 , denom : 1 } ;
1638
- pub const _15: Rational = Ratio {
1641
+ pub const _8: Rational64 = Ratio { numer : 8 , denom : 1 } ;
1642
+ pub const _15: Rational64 = Ratio {
1639
1643
numer : 15 ,
1640
1644
denom : 1 ,
1641
1645
} ;
1642
- pub const _16: Rational = Ratio {
1646
+ pub const _16: Rational64 = Ratio {
1643
1647
numer : 16 ,
1644
1648
denom : 1 ,
1645
1649
} ;
1646
1650
1647
- pub const _1_2: Rational = Ratio { numer : 1 , denom : 2 } ;
1648
- pub const _1_8: Rational = Ratio { numer : 1 , denom : 8 } ;
1649
- pub const _1_15: Rational = Ratio {
1651
+ pub const _1_2: Rational64 = Ratio { numer : 1 , denom : 2 } ;
1652
+ pub const _1_8: Rational64 = Ratio { numer : 1 , denom : 8 } ;
1653
+ pub const _1_15: Rational64 = Ratio {
1650
1654
numer : 1 ,
1651
1655
denom : 15 ,
1652
1656
} ;
1653
- pub const _1_16: Rational = Ratio {
1657
+ pub const _1_16: Rational64 = Ratio {
1654
1658
numer : 1 ,
1655
1659
denom : 16 ,
1656
1660
} ;
1657
- pub const _3_2: Rational = Ratio { numer : 3 , denom : 2 } ;
1658
- pub const _5_2: Rational = Ratio { numer : 5 , denom : 2 } ;
1659
- pub const _NEG1_2: Rational = Ratio {
1661
+ pub const _3_2: Rational64 = Ratio { numer : 3 , denom : 2 } ;
1662
+ pub const _5_2: Rational64 = Ratio { numer : 5 , denom : 2 } ;
1663
+ pub const _NEG1_2: Rational64 = Ratio {
1660
1664
numer : -1 ,
1661
1665
denom : 2 ,
1662
1666
} ;
1663
- pub const _1_NEG2: Rational = Ratio {
1667
+ pub const _1_NEG2: Rational64 = Ratio {
1664
1668
numer : 1 ,
1665
1669
denom : -2 ,
1666
1670
} ;
1667
- pub const _NEG1_NEG2: Rational = Ratio {
1671
+ pub const _NEG1_NEG2: Rational64 = Ratio {
1668
1672
numer : -1 ,
1669
1673
denom : -2 ,
1670
1674
} ;
1671
- pub const _1_3: Rational = Ratio { numer : 1 , denom : 3 } ;
1672
- pub const _NEG1_3: Rational = Ratio {
1675
+ pub const _1_3: Rational64 = Ratio { numer : 1 , denom : 3 } ;
1676
+ pub const _NEG1_3: Rational64 = Ratio {
1673
1677
numer : -1 ,
1674
1678
denom : 3 ,
1675
1679
} ;
1676
- pub const _2_3: Rational = Ratio { numer : 2 , denom : 3 } ;
1677
- pub const _NEG2_3: Rational = Ratio {
1680
+ pub const _2_3: Rational64 = Ratio { numer : 2 , denom : 3 } ;
1681
+ pub const _NEG2_3: Rational64 = Ratio {
1678
1682
numer : -2 ,
1679
1683
denom : 3 ,
1680
1684
} ;
1681
- pub const _MIN: Rational = Ratio {
1682
- numer : isize :: MIN ,
1685
+ pub const _MIN: Rational64 = Ratio {
1686
+ numer : i64 :: MIN ,
1683
1687
denom : 1 ,
1684
1688
} ;
1685
- pub const _MIN_P1: Rational = Ratio {
1686
- numer : isize :: MIN + 1 ,
1689
+ pub const _MIN_P1: Rational64 = Ratio {
1690
+ numer : i64 :: MIN + 1 ,
1687
1691
denom : 1 ,
1688
1692
} ;
1689
- pub const _MAX: Rational = Ratio {
1690
- numer : isize :: MAX ,
1693
+ pub const _MAX: Rational64 = Ratio {
1694
+ numer : i64 :: MAX ,
1691
1695
denom : 1 ,
1692
1696
} ;
1693
- pub const _MAX_M1: Rational = Ratio {
1694
- numer : isize :: MAX - 1 ,
1697
+ pub const _MAX_M1: Rational64 = Ratio {
1698
+ numer : i64 :: MAX - 1 ,
1695
1699
denom : 1 ,
1696
1700
} ;
1697
- pub const _BILLION: Rational = Ratio {
1701
+ pub const _BILLION: Rational64 = Ratio {
1698
1702
numer : 1_000_000_000 ,
1699
1703
denom : 1 ,
1700
1704
} ;
1701
1705
1702
1706
#[ cfg( feature = "num-bigint" ) ]
1703
- pub fn to_big ( n : Rational ) -> BigRational {
1707
+ pub fn to_big ( n : Rational64 ) -> BigRational {
1704
1708
Ratio :: new (
1705
- FromPrimitive :: from_isize ( n. numer ) . unwrap ( ) ,
1706
- FromPrimitive :: from_isize ( n. denom ) . unwrap ( ) ,
1709
+ FromPrimitive :: from_i64 ( n. numer ) . unwrap ( ) ,
1710
+ FromPrimitive :: from_i64 ( n. denom ) . unwrap ( ) ,
1707
1711
)
1708
1712
}
1709
1713
#[ cfg( not( feature = "num-bigint" ) ) ]
1710
- pub fn to_big ( n : Rational ) -> Rational {
1714
+ pub fn to_big ( n : Rational64 ) -> Rational64 {
1711
1715
Ratio :: new (
1712
- FromPrimitive :: from_isize ( n. numer ) . unwrap ( ) ,
1713
- FromPrimitive :: from_isize ( n. denom ) . unwrap ( ) ,
1716
+ FromPrimitive :: from_i64 ( n. numer ) . unwrap ( ) ,
1717
+ FromPrimitive :: from_i64 ( n. denom ) . unwrap ( ) ,
1714
1718
)
1715
1719
}
1716
1720
@@ -1794,7 +1798,7 @@ mod test {
1794
1798
assert ! ( _0 >= _0 && _1 >= _1) ;
1795
1799
assert ! ( _1 >= _0 && !( _0 >= _1) ) ;
1796
1800
1797
- let _0_2: Rational = Ratio :: new_raw ( 0 , 2 ) ;
1801
+ let _0_2: Rational64 = Ratio :: new_raw ( 0 , 2 ) ;
1798
1802
assert_eq ! ( _0, _0_2) ;
1799
1803
}
1800
1804
@@ -2038,15 +2042,15 @@ mod test {
2038
2042
}
2039
2043
2040
2044
mod arith {
2041
- use super :: super :: { Ratio , Rational } ;
2045
+ use super :: super :: { Ratio , Rational64 } ;
2042
2046
use super :: { to_big, _0, _1, _1_2, _2, _3_2, _5_2, _MAX, _MAX_M1, _MIN, _MIN_P1, _NEG1_2} ;
2043
2047
use core:: fmt:: Debug ;
2044
2048
use num_integer:: Integer ;
2045
2049
use num_traits:: { Bounded , CheckedAdd , CheckedDiv , CheckedMul , CheckedSub , NumAssign } ;
2046
2050
2047
2051
#[ test]
2048
2052
fn test_add ( ) {
2049
- fn test ( a : Rational , b : Rational , c : Rational ) {
2053
+ fn test ( a : Rational64 , b : Rational64 , c : Rational64 ) {
2050
2054
assert_eq ! ( a + b, c) ;
2051
2055
assert_eq ! (
2052
2056
{
@@ -2060,7 +2064,7 @@ mod test {
2060
2064
assert_eq ! ( a. checked_add( & b) , Some ( c) ) ;
2061
2065
assert_eq ! ( to_big( a) . checked_add( & to_big( b) ) , Some ( to_big( c) ) ) ;
2062
2066
}
2063
- fn test_assign ( a : Rational , b : isize , c : Rational ) {
2067
+ fn test_assign ( a : Rational64 , b : i64 , c : Rational64 ) {
2064
2068
assert_eq ! ( a + b, c) ;
2065
2069
assert_eq ! (
2066
2070
{
@@ -2117,7 +2121,7 @@ mod test {
2117
2121
2118
2122
#[ test]
2119
2123
fn test_sub ( ) {
2120
- fn test ( a : Rational , b : Rational , c : Rational ) {
2124
+ fn test ( a : Rational64 , b : Rational64 , c : Rational64 ) {
2121
2125
assert_eq ! ( a - b, c) ;
2122
2126
assert_eq ! (
2123
2127
{
@@ -2131,7 +2135,7 @@ mod test {
2131
2135
assert_eq ! ( a. checked_sub( & b) , Some ( c) ) ;
2132
2136
assert_eq ! ( to_big( a) . checked_sub( & to_big( b) ) , Some ( to_big( c) ) ) ;
2133
2137
}
2134
- fn test_assign ( a : Rational , b : isize , c : Rational ) {
2138
+ fn test_assign ( a : Rational64 , b : i64 , c : Rational64 ) {
2135
2139
assert_eq ! ( a - b, c) ;
2136
2140
assert_eq ! (
2137
2141
{
@@ -2182,7 +2186,7 @@ mod test {
2182
2186
2183
2187
#[ test]
2184
2188
fn test_mul ( ) {
2185
- fn test ( a : Rational , b : Rational , c : Rational ) {
2189
+ fn test ( a : Rational64 , b : Rational64 , c : Rational64 ) {
2186
2190
assert_eq ! ( a * b, c) ;
2187
2191
assert_eq ! (
2188
2192
{
@@ -2196,7 +2200,7 @@ mod test {
2196
2200
assert_eq ! ( a. checked_mul( & b) , Some ( c) ) ;
2197
2201
assert_eq ! ( to_big( a) . checked_mul( & to_big( b) ) , Some ( to_big( c) ) ) ;
2198
2202
}
2199
- fn test_assign ( a : Rational , b : isize , c : Rational ) {
2203
+ fn test_assign ( a : Rational64 , b : i64 , c : Rational64 ) {
2200
2204
assert_eq ! ( a * b, c) ;
2201
2205
assert_eq ! (
2202
2206
{
@@ -2271,7 +2275,7 @@ mod test {
2271
2275
2272
2276
#[ test]
2273
2277
fn test_div ( ) {
2274
- fn test ( a : Rational , b : Rational , c : Rational ) {
2278
+ fn test ( a : Rational64 , b : Rational64 , c : Rational64 ) {
2275
2279
assert_eq ! ( a / b, c) ;
2276
2280
assert_eq ! (
2277
2281
{
@@ -2285,7 +2289,7 @@ mod test {
2285
2289
assert_eq ! ( a. checked_div( & b) , Some ( c) ) ;
2286
2290
assert_eq ! ( to_big( a) . checked_div( & to_big( b) ) , Some ( to_big( c) ) ) ;
2287
2291
}
2288
- fn test_assign ( a : Rational , b : isize , c : Rational ) {
2292
+ fn test_assign ( a : Rational64 , b : i64 , c : Rational64 ) {
2289
2293
assert_eq ! ( a / b, c) ;
2290
2294
assert_eq ! (
2291
2295
{
@@ -2360,7 +2364,7 @@ mod test {
2360
2364
2361
2365
#[ test]
2362
2366
fn test_rem ( ) {
2363
- fn test ( a : Rational , b : Rational , c : Rational ) {
2367
+ fn test ( a : Rational64 , b : Rational64 , c : Rational64 ) {
2364
2368
assert_eq ! ( a % b, c) ;
2365
2369
assert_eq ! (
2366
2370
{
@@ -2372,7 +2376,7 @@ mod test {
2372
2376
) ;
2373
2377
assert_eq ! ( to_big( a) % to_big( b) , to_big( c) )
2374
2378
}
2375
- fn test_assign ( a : Rational , b : isize , c : Rational ) {
2379
+ fn test_assign ( a : Rational64 , b : i64 , c : Rational64 ) {
2376
2380
assert_eq ! ( a % b, c) ;
2377
2381
assert_eq ! (
2378
2382
{
@@ -2429,7 +2433,7 @@ mod test {
2429
2433
2430
2434
#[ test]
2431
2435
fn test_neg ( ) {
2432
- fn test ( a : Rational , b : Rational ) {
2436
+ fn test ( a : Rational64 , b : Rational64 ) {
2433
2437
assert_eq ! ( -a, b) ;
2434
2438
assert_eq ! ( -to_big( a) , to_big( b) )
2435
2439
}
@@ -2624,7 +2628,7 @@ mod test {
2624
2628
2625
2629
#[ test]
2626
2630
fn test_pow ( ) {
2627
- fn test ( r : Rational , e : i32 , expected : Rational ) {
2631
+ fn test ( r : Rational64 , e : i32 , expected : Rational64 ) {
2628
2632
assert_eq ! ( r. pow( e) , expected) ;
2629
2633
assert_eq ! ( Pow :: pow( r, e) , expected) ;
2630
2634
assert_eq ! ( Pow :: pow( r, & e) , expected) ;
@@ -2635,7 +2639,7 @@ mod test {
2635
2639
}
2636
2640
2637
2641
#[ cfg( feature = "num-bigint" ) ]
2638
- fn test_big ( r : Rational , e : i32 , expected : Rational ) {
2642
+ fn test_big ( r : Rational64 , e : i32 , expected : Rational64 ) {
2639
2643
let r = BigRational :: new_raw ( r. numer . into ( ) , r. denom . into ( ) ) ;
2640
2644
let expected = BigRational :: new_raw ( expected. numer . into ( ) , expected. denom . into ( ) ) ;
2641
2645
assert_eq ! ( ( & r) . pow( e) , expected) ;
@@ -2661,7 +2665,7 @@ mod test {
2661
2665
#[ cfg( feature = "std" ) ]
2662
2666
fn test_to_from_str ( ) {
2663
2667
use std:: string:: { String , ToString } ;
2664
- fn test ( r : Rational , s : String ) {
2668
+ fn test ( r : Rational64 , s : String ) {
2665
2669
assert_eq ! ( FromStr :: from_str( & s) , Ok ( r) ) ;
2666
2670
assert_eq ! ( r. to_string( ) , s) ;
2667
2671
}
@@ -2675,7 +2679,7 @@ mod test {
2675
2679
#[ test]
2676
2680
fn test_from_str_fail ( ) {
2677
2681
fn test ( s : & str ) {
2678
- let rational: Result < Rational , _ > = FromStr :: from_str ( s) ;
2682
+ let rational: Result < Rational64 , _ > = FromStr :: from_str ( s) ;
2679
2683
assert ! ( rational. is_err( ) ) ;
2680
2684
}
2681
2685
@@ -2751,7 +2755,7 @@ mod test {
2751
2755
assert_eq ! ( _3_2. abs_sub( & _1_2) , _1) ;
2752
2756
assert_eq ! ( _1_2. abs_sub( & _3_2) , Zero :: zero( ) ) ;
2753
2757
assert_eq ! ( _1_2. signum( ) , One :: one( ) ) ;
2754
- assert_eq ! ( _NEG1_2. signum( ) , -<Ratio <isize >>:: one( ) ) ;
2758
+ assert_eq ! ( _NEG1_2. signum( ) , -<Ratio <i64 >>:: one( ) ) ;
2755
2759
assert_eq ! ( _0. signum( ) , Zero :: zero( ) ) ;
2756
2760
assert ! ( _NEG1_2. is_negative( ) ) ;
2757
2761
assert ! ( _1_NEG2. is_negative( ) ) ;
@@ -2772,13 +2776,13 @@ mod test {
2772
2776
assert ! ( crate :: hash( & _0) != crate :: hash( & _3_2) ) ;
2773
2777
2774
2778
// a == b -> hash(a) == hash(b)
2775
- let a = Rational :: new_raw ( 4 , 2 ) ;
2776
- let b = Rational :: new_raw ( 6 , 3 ) ;
2779
+ let a = Rational64 :: new_raw ( 4 , 2 ) ;
2780
+ let b = Rational64 :: new_raw ( 6 , 3 ) ;
2777
2781
assert_eq ! ( a, b) ;
2778
2782
assert_eq ! ( crate :: hash( & a) , crate :: hash( & b) ) ;
2779
2783
2780
- let a = Rational :: new_raw ( 123456789 , 1000 ) ;
2781
- let b = Rational :: new_raw ( 123456789 * 5 , 5000 ) ;
2784
+ let a = Rational64 :: new_raw ( 123456789 , 1000 ) ;
2785
+ let b = Rational64 :: new_raw ( 123456789 * 5 , 5000 ) ;
2782
2786
assert_eq ! ( a, b) ;
2783
2787
assert_eq ! ( crate :: hash( & a) , crate :: hash( & b) ) ;
2784
2788
}
0 commit comments