@@ -27,39 +27,39 @@ pub struct Price(NonZeroI64);
2727
2828impl Price {
2929 pub fn from_integer ( value : i64 , exponent : i16 ) -> Result < Price , PriceError > {
30- let value = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
30+ let mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
3131 ExponentFactor :: Mul ( coef) => value. checked_mul ( coef) . ok_or ( PriceError :: Overflow ) ?,
3232 ExponentFactor :: Div ( coef) => value. checked_div ( coef) . ok_or ( PriceError :: Overflow ) ?,
3333 } ;
34- let value = NonZeroI64 :: new ( value ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?;
35- Ok ( Self ( value ) )
34+ let mantissa = NonZeroI64 :: new ( mantissa ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?;
35+ Ok ( Self ( mantissa ) )
3636 }
3737
3838 pub fn parse_str ( value : & str , exponent : i16 ) -> Result < Price , PriceError > {
3939 let value: Decimal = value. parse ( ) ?;
40- let value = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
40+ let mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
4141 ExponentFactor :: Mul ( coef) => value
4242 . checked_mul ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
4343 . ok_or ( PriceError :: Overflow ) ?,
4444 ExponentFactor :: Div ( coef) => value
4545 . checked_div ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
4646 . ok_or ( PriceError :: Overflow ) ?,
4747 } ;
48- if !value . is_integer ( ) {
48+ if !mantissa . is_integer ( ) {
4949 return Err ( PriceError :: TooPrecise ) ;
5050 }
51- let value : i64 = value . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
52- let value = NonZeroI64 :: new ( value ) . ok_or ( PriceError :: Overflow ) ?;
53- Ok ( Self ( value ) )
51+ let mantissa : i64 = mantissa . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
52+ let mantissa = NonZeroI64 :: new ( mantissa ) . ok_or ( PriceError :: Overflow ) ?;
53+ Ok ( Self ( mantissa ) )
5454 }
5555
5656 pub const fn from_nonzero_mantissa ( mantissa : NonZeroI64 ) -> Self {
5757 Self ( mantissa)
5858 }
5959
6060 pub const fn from_mantissa ( mantissa : i64 ) -> Result < Self , PriceError > {
61- if let Some ( value ) = NonZeroI64 :: new ( mantissa) {
62- Ok ( Self ( value ) )
61+ if let Some ( mantissa ) = NonZeroI64 :: new ( mantissa) {
62+ Ok ( Self ( mantissa ) )
6363 } else {
6464 Err ( PriceError :: ZeroPriceUnsupported )
6565 }
@@ -75,81 +75,86 @@ impl Price {
7575
7676 pub fn to_f64 ( self , exponent : i16 ) -> Result < f64 , PriceError > {
7777 match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
78- // Mul/div is reversed for this conversion
78+ // Mul/div is reversed for converting mantissa to value
7979 ExponentFactor :: Mul ( coef) => Ok ( self . 0 . get ( ) as f64 / coef as f64 ) ,
8080 ExponentFactor :: Div ( coef) => Ok ( self . 0 . get ( ) as f64 * coef as f64 ) ,
8181 }
8282 }
8383
8484 pub fn from_f64 ( value : f64 , exponent : i16 ) -> Result < Self , PriceError > {
8585 let value = Decimal :: from_f64 ( value) . ok_or ( PriceError :: Overflow ) ?;
86- let value = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
86+ let mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
8787 ExponentFactor :: Mul ( coef) => value
8888 . checked_mul ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
8989 . ok_or ( PriceError :: Overflow ) ?,
9090 ExponentFactor :: Div ( coef) => value
9191 . checked_div ( Decimal :: from_i64 ( coef) . ok_or ( PriceError :: Overflow ) ?)
9292 . ok_or ( PriceError :: Overflow ) ?,
9393 } ;
94- let value : i64 = value . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
94+ let mantissa : i64 = mantissa . try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
9595 Ok ( Self (
96- NonZeroI64 :: new ( value ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?,
96+ NonZeroI64 :: new ( mantissa ) . ok_or ( PriceError :: ZeroPriceUnsupported ) ?,
9797 ) )
9898 }
9999
100- pub fn add_with_same_mantissa ( self , other : Price ) -> Result < Self , PriceError > {
101- let value = self
100+ pub fn add_with_same_exponent ( self , other : Price ) -> Result < Self , PriceError > {
101+ let mantissa = self
102102 . 0
103103 . get ( )
104104 . checked_add ( other. 0 . get ( ) )
105105 . ok_or ( PriceError :: Overflow ) ?;
106- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
106+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
107107 }
108108
109- pub fn sub_with_same_mantissa ( self , other : Price ) -> Result < Self , PriceError > {
110- let value = self
109+ pub fn sub_with_same_exponent ( self , other : Price ) -> Result < Self , PriceError > {
110+ let mantissa = self
111111 . 0
112112 . get ( )
113113 . checked_sub ( other. 0 . get ( ) )
114114 . ok_or ( PriceError :: Overflow ) ?;
115- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
115+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
116116 }
117117
118118 pub fn mul_integer ( self , factor : i64 ) -> Result < Self , PriceError > {
119- let value = self
119+ let mantissa = self
120120 . 0
121121 . get ( )
122122 . checked_mul ( factor)
123123 . ok_or ( PriceError :: Overflow ) ?;
124- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
124+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
125125 }
126126
127127 pub fn div_integer ( self , factor : i64 ) -> Result < Self , PriceError > {
128- let value = self
128+ let mantissa = self
129129 . 0
130130 . get ( )
131131 . checked_div ( factor)
132132 . ok_or ( PriceError :: Overflow ) ?;
133- Self :: from_mantissa ( value ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
133+ Self :: from_mantissa ( mantissa ) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
134134 }
135135
136- pub fn mul_decimal ( self , mantissa : i64 , rhs_exponent : i16 ) -> Result < Self , PriceError > {
137- let left_value = i128:: from ( self . 0 . get ( ) ) ;
138- let right_value = i128:: from ( mantissa) ;
136+ pub fn mul_decimal ( self , mantissa : i64 , exponent : i16 ) -> Result < Self , PriceError > {
137+ let left_mantissa = i128:: from ( self . 0 . get ( ) ) ;
138+ let right_mantissa = i128:: from ( mantissa) ;
139139
140- let value = left_value
141- . checked_mul ( right_value)
140+ // multiplied_mantissas = left_mantissa * right_mantissa
141+ let multiplied_mantissas = left_mantissa
142+ . checked_mul ( right_mantissa)
142143 . ok_or ( PriceError :: Overflow ) ?;
143144
144- let value = match ExponentFactor :: get ( rhs_exponent) . ok_or ( PriceError :: Overflow ) ? {
145- ExponentFactor :: Mul ( coef) => {
146- value. checked_div ( coef. into ( ) ) . ok_or ( PriceError :: Overflow ) ?
147- }
148- ExponentFactor :: Div ( coef) => {
149- value. checked_mul ( coef. into ( ) ) . ok_or ( PriceError :: Overflow ) ?
150- }
145+ // result_mantissa = left_mantissa * right_mantissa * 10^exponent
146+ // Mul/div is reversed for multiplying 10^exponent
147+ let result_mantissa = match ExponentFactor :: get ( exponent) . ok_or ( PriceError :: Overflow ) ? {
148+ ExponentFactor :: Mul ( coef) => multiplied_mantissas
149+ . checked_div ( coef. into ( ) )
150+ . ok_or ( PriceError :: Overflow ) ?,
151+ ExponentFactor :: Div ( coef) => multiplied_mantissas
152+ . checked_mul ( coef. into ( ) )
153+ . ok_or ( PriceError :: Overflow ) ?,
151154 } ;
152- let value: i64 = value. try_into ( ) . map_err ( |_| PriceError :: Overflow ) ?;
153- Self :: from_mantissa ( value) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
155+ let result_mantissa: i64 = result_mantissa
156+ . try_into ( )
157+ . map_err ( |_| PriceError :: Overflow ) ?;
158+ Self :: from_mantissa ( result_mantissa) . map_err ( |_| PriceError :: ZeroPriceUnsupported )
154159 }
155160}
0 commit comments