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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// numera::number::integer::q::ops
//
//!
//
use crate::number::{rational::*, traits::Ident};
use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
use devela::paste;
// impl ops (which panic on overflow)
macro_rules! impl_rational_ops {
// impl all ops for multiple integer types
(
$( $t:ident + $b:literal, cast: $bcast:literal);+
) => {
$(
impl_rational_ops![Add: $t + $b, cast: $bcast];
impl_rational_ops![Sub: $t + $b, cast: $bcast];
impl_rational_ops![Mul: $t + $b, cast: $bcast];
impl_rational_ops![Div: $t + $b, cast: $bcast];
impl_rational_ops![Rem: $t + $b, cast: $bcast];
impl_rational_ops![Neg: $t + $b, cast: $bcast];
)+
};
// impl Add for a single rational
//
// $t: rational type. e.g. Rational8
(Add: $t:ident + $b:literal, cast: $bcast:literal) => { paste! {
impl Add for [<$t$b>] {
type Output = Self;
/// The addition operator `+`.
///
/// The operands are upcasted for this operation to the next larger
/// bit-size (except for 128-bit), and the result is reduced before
/// trying to downcast it to the original bit-size.
///
/// # Panics
/// If the result can't fit the current bit-size.
fn add(self, other: Self::Output) -> Self::Output {
let cself = [<$t$bcast>]::from(self);
let cother = [<$t$bcast>]::from(other);
let num = cself.num * cother.den.into() + cother.num * cself.den.into();
let den = cself.den * cother.den;
let creduced = [<$t$bcast>] { num, den }.reduced();
#[cfg(feature = "try_from")]
return creduced.try_into().unwrap();
#[cfg(not(feature = "try_from"))]
return [<$t$b>]::new(
creduced.num.0.try_into().unwrap(),
creduced.den.0.get().try_into().unwrap()
).unwrap();
}
}
}};
(Sub: $t:ident + $b:literal, cast: $bcast:literal) => { paste! {
impl Sub for [<$t$b>] {
type Output = Self;
/// The subtraction operator `-`.
///
/// The operands are upcasted for this operation to the next larger
/// bit-size (except for 128-bit), and the result is reduced before
/// trying to downcast it to the original bit-size.
///
/// # Panics
/// If the result can't fit the current bit-size.
fn sub(self, other: Self) -> Self::Output {
let cself = [<$t$bcast>]::from(self);
let cother = [<$t$bcast>]::from(other);
let num = cself.num * cother.den.into() - cother.num * cself.den.into();
let den = cself.den * cother.den;
let creduced = [<$t$bcast>] { num, den }.reduced();
#[cfg(feature = "try_from")]
return creduced.try_into().unwrap();
#[cfg(not(feature = "try_from"))]
return [<$t$b>]::new(
creduced.num.0.try_into().unwrap(),
creduced.den.0.get().try_into().unwrap()
).unwrap();
}
}
}};
(Mul: $t:ident + $b:literal, cast: $bcast:literal) => { paste! {
impl Mul for [<$t$b>] {
type Output = Self;
/// The multiplication operator `*`.
///
/// The operands are upcasted for this operation to the next larger
/// bit-size (except for 128-bit), and the result is reduced before
/// trying to downcast it to the original bit-size.
///
/// # Panics
/// If the result can't fit the current bit-size.
fn mul(self, other: Self) -> Self::Output {
let cself = [<$t$bcast>]::from(self);
let cother = [<$t$bcast>]::from(other);
let num = cself.num * cother.num;
let den = cself.den * cother.den;
let creduced = [<$t$bcast>] { num, den }.reduced();
#[cfg(feature = "try_from")]
return creduced.try_into().unwrap();
#[cfg(not(feature = "try_from"))]
return [<$t$b>]::new(
creduced.num.0.try_into().unwrap(),
creduced.den.0.get().try_into().unwrap()
).unwrap();
}
}
}};
(Div: $t:ident + $b:literal, cast: $bcast:literal) => { paste! {
impl Div for [<$t$b>] {
type Output = Self;
/// The division operator `/`.
///
/// The operands are upcasted for this operation to the next larger
/// bit-size (except for 128-bit), and the result is reduced before
/// trying to downcast it to the original bit-size.
///
/// # Panics
/// If the result can't fit the current bit-size.
fn div(self, other: Self) -> Self::Output {
let cself = [<$t$bcast>]::from(self);
let cother = [<$t$bcast>]::from(other);
let num = cself.num * cother.den.into();
let den = cother.num * cself.den.into();
if den.is_zero() {
unreachable![] // IMPROVE: use hint
} else {
#[cfg(feature = "try_from")]
let den = den.try_into().unwrap();
#[cfg(not(feature = "try_from"))]
let den = core::num::[<NonZeroI$bcast>]::new(den.0).unwrap().into();
let creduced = [<$t$bcast>] { num, den }.reduced();
#[cfg(feature = "try_from")]
return creduced.try_into().unwrap();
#[cfg(not(feature = "try_from"))]
return [<$t$b>]::new(
creduced.num.0.try_into().unwrap(),
creduced.den.0.get().try_into().unwrap()
).unwrap();
}
}
}
}};
(Rem: $t:ident + $b:literal, cast: $bcast:literal) => { paste! {
impl Rem for [<$t$b>] {
type Output = Self;
/// The remainder operator `%` (using truncated division)
///
/// The operands are upcasted for this operation to the next larger
/// bit-size (except for 128-bit), and the result is reduced before
/// trying to downcast it to the original bit-size.
///
/// # Panics
/// If the result can't fit the current bit-size.
fn rem(self, other: Self) -> Self::Output {
let cself = [<$t$bcast>]::from(self);
let cother = [<$t$bcast>]::from(other);
let lhs_num = cself.num * cother.den.into();
let rhs_num = cother.num * cself.den.into();
let num = (lhs_num % rhs_num);
let den = cself.den * cother.den;
#[cfg(feature = "try_from")]
return [<$t$bcast>] { num, den }.reduced().try_into().unwrap();
#[cfg(not(feature = "try_from"))]
return [<$t$b>]::new(
num.0.try_into().unwrap(),
den.0.get().try_into().unwrap()
).unwrap().reduced();
}
}
}};
(Neg: $t:ident + $b:literal, cast: $bcast:literal) => { paste! {
impl Neg for [<$t$b>] {
type Output = Self;
/// The negation operator `-`.
///
/// The result is reduced.
fn neg(self) -> Self::Output {
Self {
num: self.num.neg(),
den: self.den,
}
.reduced()
}
}
}};
}
impl_rational_ops![
Rational+8, cast: 16;
Rational+16, cast: 32;
Rational+32, cast: 64;
Rational+64, cast: 128;
Rational+128, cast: 128
];
#[cfg(test)]
mod tests {
use crate::all::*;
#[test]
fn q_ops() -> NumeraResult<()> {
let _5 = Q8::new(5, 1)?;
let _7 = Q8::new(7, 1)?;
// Neg
assert_eq![-Q8::new(5, 1)?, Q8::new(-5, 1)?];
assert_eq![-Q8::new(-5, 1)?, Q8::new(5, 1)?];
assert_eq![-Q8::new(5, -1)?, Q8::new(5, 1)?];
// Add
assert_eq![Q8::new(5, 1)? + Q8::new(7, 1)?, Q8::new(12, 1)?];
assert_eq![Q8::new(1, 5)? + Q8::new(1, 7)?, Q8::new(12, 35)?];
assert_eq![Q8::new(2, 7)? + Q8::new(3, 8)?, Q8::new(37, 56)?];
assert_eq![Q8::new(15, 32)? + Q8::new(27, 9)?, Q8::new(111, 32)?];
// Sub
assert_eq![Q8::new(12, 1)? - Q8::new(7, 1)?, Q8::new(5, 1)?];
assert_eq![Q16::new(12, 35)? - Q16::new(1, 7)?, Q16::new(1, 5)?];
assert_eq![Q16::new(37, 56)? - Q16::new(3, 8)?, Q16::new(2, 7)?];
assert_eq![Q16::new(111, 32)? - Q16::new(27, 9)?, Q16::new(15, 32)?];
// Mul
assert_eq![Q8::new(12, 1)? * Q8::new(7, 1)?, Q8::new(84, 1)?];
assert_eq![Q16::new(2, 7)? * Q16::new(3, 8)?, Q16::new(3, 28)?];
assert_eq![Q16::new(11, 5)? * Q16::new(4, 9)?, Q16::new(44, 45)?];
// Div
assert_eq![Q8::new(84, 1)? / Q8::new(7, 1)?, Q8::new(12, 1)?];
assert_eq![Q16::new(3, 28)? / Q16::new(3, 8)?, Q16::new(2, 7)?];
assert_eq![Q16::new(44, 45)? / Q16::new(4, 9)?, Q16::new(11, 5)?];
// Rem
assert_eq![Q8::new(12, 1)? % Q8::new(7, 1)?, Q8::new(5, 1)?];
assert_eq![Q16::new(12, 35)? % Q16::new(1, 7)?, Q16::new(2, 35)?];
assert_eq![Q16::new(44, 45)? % Q16::new(4, 9)?, Q16::new(4, 45)?];
// #[cfg(feature = "std")]
// {
// use std::panic::catch_unwind;
// let a = Q8::new(125, 13).unwrap();
// let b = Q8::new(2, 26).unwrap();
//
// // overflow
// assert![catch_unwind(|| a + b).is_err()];
// }
Ok(())
}
}