12
12
use num_traits:: Zero ;
13
13
14
14
use crate :: extendedbigdecimal:: ExtendedBigDecimal ;
15
- use crate :: extendedbigint:: ExtendedBigInt ;
16
-
17
- /// An integral or floating point number.
18
- #[ derive( Debug , PartialEq ) ]
19
- pub enum Number {
20
- Int ( ExtendedBigInt ) ,
21
- Float ( ExtendedBigDecimal ) ,
22
- }
23
-
24
- impl Number {
25
- /// Decide whether this number is zero (either positive or negative).
26
- pub fn is_zero ( & self ) -> bool {
27
- // We would like to implement `num_traits::Zero`, but it
28
- // requires an addition implementation, and we don't want to
29
- // implement that here.
30
- match self {
31
- Self :: Int ( n) => n. is_zero ( ) ,
32
- Self :: Float ( x) => x. is_zero ( ) ,
33
- }
34
- }
35
-
36
- /// Convert this number into an `ExtendedBigDecimal`.
37
- pub fn into_extended_big_decimal ( self ) -> ExtendedBigDecimal {
38
- match self {
39
- Self :: Int ( n) => ExtendedBigDecimal :: from ( n) ,
40
- Self :: Float ( x) => x,
41
- }
42
- }
43
-
44
- /// The integer number one.
45
- pub fn one ( ) -> Self {
46
- // We would like to implement `num_traits::One`, but it requires
47
- // a multiplication implementation, and we don't want to
48
- // implement that here.
49
- Self :: Int ( ExtendedBigInt :: one ( ) )
50
- }
51
-
52
- /// Round this number towards the given other number.
53
- ///
54
- /// If `other` is greater, then round up. If `other` is smaller,
55
- /// then round down.
56
- pub fn round_towards ( self , other : & ExtendedBigInt ) -> ExtendedBigInt {
57
- match self {
58
- // If this number is already an integer, it is already
59
- // rounded to the nearest integer in the direction of
60
- // `other`.
61
- Self :: Int ( num) => num,
62
- // Otherwise, if this number is a float, we need to decide
63
- // whether `other` is larger or smaller than it, and thus
64
- // whether to round up or round down, respectively.
65
- Self :: Float ( num) => {
66
- let other: ExtendedBigDecimal = From :: from ( other. clone ( ) ) ;
67
- if other > num {
68
- num. ceil ( )
69
- } else {
70
- // If they are equal, then `self` is already an
71
- // integer, so calling `floor()` does no harm and
72
- // will just return that integer anyway.
73
- num. floor ( )
74
- }
75
- }
76
- }
77
- }
78
- }
79
15
80
16
/// A number with a specified number of integer and fractional digits.
81
17
///
@@ -87,13 +23,13 @@ impl Number {
87
23
/// You can get an instance of this struct by calling [`str::parse`].
88
24
#[ derive( Debug ) ]
89
25
pub struct PreciseNumber {
90
- pub number : Number ,
26
+ pub number : ExtendedBigDecimal ,
91
27
pub num_integral_digits : usize ,
92
28
pub num_fractional_digits : usize ,
93
29
}
94
30
95
31
impl PreciseNumber {
96
- pub fn new ( number : Number , num_integral_digits : usize , num_fractional_digits : usize ) -> Self {
32
+ pub fn new ( number : ExtendedBigDecimal , num_integral_digits : usize , num_fractional_digits : usize ) -> Self {
97
33
Self {
98
34
number,
99
35
num_integral_digits,
@@ -106,7 +42,7 @@ impl PreciseNumber {
106
42
// We would like to implement `num_traits::One`, but it requires
107
43
// a multiplication implementation, and we don't want to
108
44
// implement that here.
109
- Self :: new ( Number :: one ( ) , 1 , 0 )
45
+ Self :: new ( ExtendedBigDecimal :: one ( ) , 1 , 0 )
110
46
}
111
47
112
48
/// Decide whether this number is zero (either positive or negative).
0 commit comments