1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use num_bigint::{BigInt, BigUint};
use num_traits::Zero;
use crate::result::{illegal_operation, IonError};
use crate::types::magnitude::Magnitude;
use std::convert::TryFrom;
use std::fmt::{Display, Formatter};
use std::ops::{MulAssign, Neg};
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum Sign {
Negative,
Positive,
}
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Coefficient {
pub(crate) sign: Sign,
pub(crate) magnitude: Magnitude,
}
impl Coefficient {
pub(crate) fn new<I: Into<Magnitude>>(sign: Sign, magnitude: I) -> Self {
let magnitude = magnitude.into();
Coefficient { sign, magnitude }
}
pub(crate) fn sign(&self) -> Sign {
self.sign
}
pub(crate) fn magnitude(&self) -> &Magnitude {
&self.magnitude
}
pub(crate) fn number_of_decimal_digits(&self) -> u64 {
self.magnitude.number_of_decimal_digits()
}
pub(crate) fn negative_zero() -> Self {
Coefficient {
sign: Sign::Negative,
magnitude: Magnitude::U64(0),
}
}
pub(crate) fn is_negative_zero(&self) -> bool {
match (self.sign, &self.magnitude) {
(Sign::Negative, Magnitude::U64(0)) => true,
(Sign::Negative, Magnitude::BigUInt(b)) if b.is_zero() => true,
_ => false,
}
}
pub(crate) fn is_positive_zero(&self) -> bool {
match (self.sign, &self.magnitude) {
(Sign::Positive, Magnitude::U64(0)) => true,
(Sign::Positive, Magnitude::BigUInt(b)) if b.is_zero() => true,
_ => false,
}
}
pub(crate) fn is_zero(&self) -> bool {
match (self.sign, &self.magnitude) {
(_, Magnitude::U64(0)) => true,
(_, Magnitude::BigUInt(b)) if b.is_zero() => true,
_ => false,
}
}
pub(crate) fn as_i64(&self) -> Option<i64> {
match self.magnitude {
Magnitude::U64(unsigned) => match i64::try_from(unsigned) {
Ok(signed) => match self.sign {
Sign::Negative => Some(signed.neg()),
Sign::Positive => Some(signed),
},
Err(_) => None,
},
Magnitude::BigUInt(_) => None,
}
}
}
macro_rules! impl_coefficient_from_unsigned_int_types {
($($t:ty),*) => ($(
impl From<$t> for Coefficient {
fn from(value: $t) -> Coefficient {
Coefficient::new(Sign::Positive, value)
}
}
)*)
}
impl_coefficient_from_unsigned_int_types!(u8, u16, u32, u64, u128, usize, BigUint);
macro_rules! impl_coefficient_from_signed_int_types {
($($t:ty),*) => ($(
impl From<$t> for Coefficient {
fn from(value: $t) -> Coefficient {
let sign = if value < <$t>::zero() { Sign::Negative } else { Sign::Positive };
Coefficient::new(sign, value)
}
}
)*)
}
impl_coefficient_from_signed_int_types!(i8, i16, i32, i64, i128, isize);
impl TryFrom<Coefficient> for BigInt {
type Error = IonError;
fn try_from(value: Coefficient) -> Result<Self, Self::Error> {
if value.is_negative_zero() {
illegal_operation("Cannot convert negative zero Decimal to BigDecimal")?;
}
let mut big_int: BigInt = match value.magnitude {
Magnitude::U64(m) => m.into(),
Magnitude::BigUInt(m) => m.into(),
};
if value.sign == Sign::Negative {
big_int.mul_assign(-1);
}
Ok(big_int)
}
}
impl Display for Coefficient {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.sign {
Sign::Positive => {}
Sign::Negative => write!(f, "-")?,
};
match &self.magnitude {
Magnitude::U64(m) => write!(f, "{}", *m),
Magnitude::BigUInt(m) => write!(f, "{}", m),
}
}
}
#[cfg(test)]
mod coefficient_tests {
use crate::ion_eq::IonEq;
use num_bigint::BigUint;
use crate::types::coefficient::Coefficient;
use crate::types::decimal::Decimal;
fn eq_test<I1, I2>(c1: I1, c2: I2)
where
I1: Into<Coefficient>,
I2: Into<Coefficient>,
{
let c1 = c1.into();
let c2 = c2.into();
assert_eq!(c1, c2);
}
#[test]
fn test_coefficient_eq() {
eq_test(0u64, 0u64);
eq_test(0u64, BigUint::from(0u64));
eq_test(BigUint::from(0u64), 0u64);
eq_test(BigUint::from(0u64), BigUint::from(0u64));
eq_test(u64::MAX, u64::MAX);
eq_test(u64::MAX, BigUint::from(u64::MAX));
eq_test(BigUint::from(u64::MAX), u64::MAX);
eq_test(BigUint::from(u64::MAX), BigUint::from(u64::MAX));
eq_test(BigUint::from(u128::MAX), BigUint::from(u128::MAX));
}
#[test]
fn test_negative_zero_eq() {
let neg_zero = Decimal::new(Coefficient::negative_zero(), 0);
let pos_zero = Decimal::new(0, 0);
assert_eq!(neg_zero, neg_zero);
assert!(neg_zero.ion_eq(&neg_zero));
assert_eq!(neg_zero, pos_zero);
assert!(!neg_zero.ion_eq(&pos_zero));
assert_eq!(pos_zero, pos_zero);
assert!(pos_zero.ion_eq(&pos_zero));
}
}