File tree Expand file tree Collapse file tree 4 files changed +47
-3
lines changed Expand file tree Collapse file tree 4 files changed +47
-3
lines changed Original file line number Diff line number Diff line change @@ -56,10 +56,12 @@ macro_rules! impl_mul_fraction {
56
56
self ,
57
57
rhs: F ,
58
58
) -> Result <Self , CheckedMultiplyFractionError > {
59
- let numerator = rhs. numerator( ) . into( ) ;
60
- let denominator = rhs. denominator( ) . into( ) ;
59
+ let divisor = rhs. denominator( ) . into( ) ;
60
+ let remainder = self
61
+ . full_mul( rhs. numerator( ) . into( ) )
62
+ . checked_rem( divisor. into( ) ) ?;
61
63
let floor_result = self . checked_mul_floored( rhs) ?;
62
- if !numerator . checked_rem ( denominator ) ? . is_zero( ) {
64
+ if !remainder . is_zero( ) {
63
65
Ok ( $Uint:: one( ) . checked_add( floor_result) ?)
64
66
} else {
65
67
Ok ( floor_result)
Original file line number Diff line number Diff line change @@ -1067,6 +1067,13 @@ mod tests {
1067
1067
assert_eq ! ( Uint128 :: new( 47030 ) , res)
1068
1068
}
1069
1069
1070
+ #[ test]
1071
+ fn mul_floored_does_not_round_on_even_divide ( ) {
1072
+ let fraction = ( 2u128 , 5u128 ) ;
1073
+ let res = Uint128 :: new ( 25 ) . mul_floored ( fraction) ;
1074
+ assert_eq ! ( Uint128 :: new( 10 ) , res)
1075
+ }
1076
+
1070
1077
#[ test]
1071
1078
fn mul_floored_works_when_operation_temporarily_takes_above_max ( ) {
1072
1079
let fraction = ( 8u128 , 21u128 ) ;
@@ -1143,6 +1150,13 @@ mod tests {
1143
1150
assert_eq ! ( Uint128 :: new( 47031 ) , res)
1144
1151
}
1145
1152
1153
+ #[ test]
1154
+ fn mul_ceil_does_not_round_on_even_divide ( ) {
1155
+ let fraction = ( 2u128 , 5u128 ) ;
1156
+ let res = Uint128 :: new ( 25 ) . mul_ceil ( fraction) ;
1157
+ assert_eq ! ( Uint128 :: new( 10 ) , res)
1158
+ }
1159
+
1146
1160
#[ test]
1147
1161
fn mul_ceil_works_when_operation_temporarily_takes_above_max ( ) {
1148
1162
let fraction = ( 8u128 , 21u128 ) ;
Original file line number Diff line number Diff line change @@ -1689,6 +1689,13 @@ mod tests {
1689
1689
assert_eq ! ( Uint256 :: from( 47030u32 ) , res)
1690
1690
}
1691
1691
1692
+ #[ test]
1693
+ fn mul_floored_does_not_round_on_even_divide ( ) {
1694
+ let fraction = ( 2u128 , 5u128 ) ;
1695
+ let res = Uint256 :: from ( 25u32 ) . mul_floored ( fraction) ;
1696
+ assert_eq ! ( Uint256 :: from( 10u32 ) , res)
1697
+ }
1698
+
1692
1699
#[ test]
1693
1700
fn mul_floored_works_when_operation_temporarily_takes_above_max ( ) {
1694
1701
let fraction = ( 8u128 , 21u128 ) ;
@@ -1777,6 +1784,13 @@ mod tests {
1777
1784
assert_eq ! ( Uint256 :: from( 47031u32 ) , res)
1778
1785
}
1779
1786
1787
+ #[ test]
1788
+ fn mul_ceil_does_not_round_on_even_divide ( ) {
1789
+ let fraction = ( 2u128 , 5u128 ) ;
1790
+ let res = Uint256 :: from ( 25u32 ) . mul_ceil ( fraction) ;
1791
+ assert_eq ! ( Uint256 :: from( 10u32 ) , res)
1792
+ }
1793
+
1780
1794
#[ test]
1781
1795
fn mul_ceil_works_when_operation_temporarily_takes_above_max ( ) {
1782
1796
let fraction = ( 8u128 , 21u128 ) ;
Original file line number Diff line number Diff line change @@ -981,6 +981,13 @@ mod tests {
981
981
assert_eq ! ( Uint64 :: new( 47030 ) , res)
982
982
}
983
983
984
+ #[ test]
985
+ fn mul_floored_does_not_round_on_even_divide ( ) {
986
+ let fraction = ( 2u64 , 5u64 ) ;
987
+ let res = Uint64 :: new ( 25 ) . mul_floored ( fraction) ;
988
+ assert_eq ! ( Uint64 :: new( 10 ) , res)
989
+ }
990
+
984
991
#[ test]
985
992
fn mul_floored_works_when_operation_temporarily_takes_above_max ( ) {
986
993
let fraction = ( 8u64 , 21u64 ) ;
@@ -1047,6 +1054,13 @@ mod tests {
1047
1054
assert_eq ! ( Uint64 :: new( 47031 ) , res)
1048
1055
}
1049
1056
1057
+ #[ test]
1058
+ fn mul_ceil_does_not_round_on_even_divide ( ) {
1059
+ let fraction = ( 2u64 , 5u64 ) ;
1060
+ let res = Uint64 :: new ( 25 ) . mul_ceil ( fraction) ;
1061
+ assert_eq ! ( Uint64 :: new( 10 ) , res)
1062
+ }
1063
+
1050
1064
#[ test]
1051
1065
fn mul_ceil_works_when_operation_temporarily_takes_above_max ( ) {
1052
1066
let fraction = ( 8u64 , 21u64 ) ;
You can’t perform that action at this time.
0 commit comments