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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! Exact dyadic rationals — numbers of the form `n · 2^-k`, the rationals whose
//! denominator is a power of two.
//!
//! [`Dyadic`] is exact and closed under addition, subtraction, multiplication,
//! and scaling by powers of two (division in general is not — `1/3` is not
//! dyadic). Every dyadic value has a *terminating* decimal expansion, which
//! [`Dyadic`]'s [`Display`](core::fmt::Display) prints exactly.
//!
//! A value is stored as `numerator · 2^-scale` in canonical form: the numerator
//! is odd (so the representation is unique), or the value is zero
//! (`numerator == 0`, `scale == 0`). The `scale` is the number of fractional
//! binary digits; it is signed, so an even integer such as `8 = 1 · 2^-(-3)` has
//! a negative scale after normalization.
use core::cmp::Ordering;
use core::fmt;
use alloc::string::ToString;
use crate::int::{Int, Sign};
use crate::nat::Nat;
/// An exact dyadic rational, `numerator · 2^-scale`.
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct Dyadic {
numerator: Int,
scale: i64,
}
impl Dyadic {
/// The value zero.
pub fn zero() -> Dyadic {
Dyadic {
numerator: Int::ZERO,
scale: 0,
}
}
/// The value one.
pub fn one() -> Dyadic {
Dyadic {
numerator: Int::ONE,
scale: 0,
}
}
/// Builds `n · 2^-k`, canonicalizing to an odd numerator (so a positive `k`
/// scales the value *down*, matching the `n·2⁻ᵏ` convention).
pub fn new(n: Int, k: i64) -> Dyadic {
if n.is_zero() {
return Dyadic::zero();
}
let t = n.trailing_zeros();
if t > 0 {
// n = m·2^t (m odd): n·2^-k = m·2^-(k - t).
Dyadic {
numerator: n.div_2k_trunc(t),
scale: k - t as i64,
}
} else {
Dyadic {
numerator: n,
scale: k,
}
}
}
/// Builds the dyadic value of an integer.
#[inline]
pub fn from_int(n: Int) -> Dyadic {
Dyadic::new(n, 0)
}
/// Returns the (odd, or zero) numerator.
#[inline]
pub fn numerator(&self) -> &Int {
&self.numerator
}
/// Returns the scale `k`, so that the value equals `numerator · 2^-k`. A
/// positive scale is the count of fractional binary digits.
#[inline]
pub fn scale(&self) -> i64 {
self.scale
}
/// Returns `true` if this value is zero.
#[inline]
pub fn is_zero(&self) -> bool {
self.numerator.is_zero()
}
/// Returns `true` if this value is an integer (`scale <= 0`).
#[inline]
pub fn is_integer(&self) -> bool {
self.is_zero() || self.scale <= 0
}
/// Returns the sign of this value.
#[inline]
pub fn sign(&self) -> Sign {
self.numerator.sign()
}
/// Returns `-self`.
pub fn neg(&self) -> Dyadic {
Dyadic {
numerator: self.numerator.neg(),
scale: self.scale,
}
}
/// Returns `|self|`.
pub fn abs(&self) -> Dyadic {
Dyadic {
numerator: self.numerator.abs(),
scale: self.scale,
}
}
/// Returns `self · 2^k` (exact; `k` may be negative).
pub fn mul_2k(&self, k: i64) -> Dyadic {
if self.is_zero() {
return Dyadic::zero();
}
// value · 2^k = numerator · 2^-(scale - k).
Dyadic {
numerator: self.numerator.clone(),
scale: self.scale - k,
}
}
/// Aligns two values to a common (finer) scale, returning their numerators
/// at that scale and the scale.
fn aligned(&self, other: &Dyadic) -> (Int, Int, i64) {
let smax = self.scale.max(other.scale);
let a = self.numerator.mul_2k((smax - self.scale) as u32);
let b = other.numerator.mul_2k((smax - other.scale) as u32);
(a, b, smax)
}
/// Returns `self + rhs` (exact).
pub fn add(&self, rhs: &Dyadic) -> Dyadic {
if self.is_zero() {
return rhs.clone();
}
if rhs.is_zero() {
return self.clone();
}
let (a, b, smax) = self.aligned(rhs);
Dyadic::new(a.add(&b), smax)
}
/// Returns `self - rhs` (exact).
pub fn sub(&self, rhs: &Dyadic) -> Dyadic {
self.add(&rhs.neg())
}
/// Returns `self · rhs` (exact).
pub fn mul(&self, rhs: &Dyadic) -> Dyadic {
Dyadic::new(self.numerator.mul(&rhs.numerator), self.scale + rhs.scale)
}
/// Returns `self` raised to `exp` (exact).
pub fn pow(&self, exp: u32) -> Dyadic {
if exp == 0 {
return Dyadic::one();
}
Dyadic::new(self.numerator.pow(exp), self.scale * exp as i64)
}
/// Returns the greatest integer `<= self`.
pub fn floor(&self) -> Int {
if self.scale <= 0 {
self.numerator.mul_2k((-self.scale) as u32)
} else {
self.numerator
.div_floor(&Int::ONE.mul_2k(self.scale as u32))
}
}
/// Returns `self` truncated toward zero as an integer.
pub fn trunc(&self) -> Int {
if self.scale <= 0 {
self.numerator.mul_2k((-self.scale) as u32)
} else {
self.numerator.div_2k_trunc(self.scale as u32)
}
}
}
impl PartialOrd for Dyadic {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Dyadic {
fn cmp(&self, other: &Self) -> Ordering {
let (a, b, _) = self.aligned(other);
a.cmp(&b)
}
}
impl Default for Dyadic {
#[inline]
fn default() -> Dyadic {
Dyadic::zero()
}
}
impl From<Int> for Dyadic {
#[inline]
fn from(n: Int) -> Dyadic {
Dyadic::from_int(n)
}
}
impl From<i64> for Dyadic {
#[inline]
fn from(v: i64) -> Dyadic {
Dyadic::from_int(Int::from_i64(v))
}
}
impl fmt::Display for Dyadic {
/// Prints the exact (terminating) decimal expansion.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if self.is_zero() {
return f.write_str("0");
}
if self.numerator.is_negative() {
f.write_str("-")?;
}
let mag = self.numerator.magnitude();
if self.scale <= 0 {
// Integer: |numerator| << (-scale).
return fmt::Display::fmt(&mag.shl((-self.scale) as u64), f);
}
// value = |numerator| / 2^k = |numerator|·5^k / 10^k; place the point k
// digits from the right.
let k = self.scale as u32;
let scaled = mag.mul(&Nat::from_u64(5).pow(k));
let digits = scaled.to_string();
let k = k as usize;
if digits.len() <= k {
f.write_str("0.")?;
for _ in 0..k - digits.len() {
f.write_str("0")?;
}
f.write_str(&digits)
} else {
let point = digits.len() - k;
f.write_str(&digits[..point])?;
f.write_str(".")?;
f.write_str(&digits[point..])
}
}
}
impl fmt::Debug for Dyadic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Dyadic({} · 2^-{})", self.numerator, self.scale)
}
}
macro_rules! dyadic_binop {
($tr:ident, $m:ident, $atr:ident, $am:ident) => {
impl core::ops::$tr for Dyadic {
type Output = Dyadic;
#[inline]
fn $m(self, rhs: Dyadic) -> Dyadic {
Dyadic::$m(&self, &rhs)
}
}
impl core::ops::$tr<&Dyadic> for &Dyadic {
type Output = Dyadic;
#[inline]
fn $m(self, rhs: &Dyadic) -> Dyadic {
Dyadic::$m(self, rhs)
}
}
impl core::ops::$atr for Dyadic {
#[inline]
fn $am(&mut self, rhs: Dyadic) {
*self = Dyadic::$m(self, &rhs);
}
}
};
}
dyadic_binop!(Add, add, AddAssign, add_assign);
dyadic_binop!(Sub, sub, SubAssign, sub_assign);
dyadic_binop!(Mul, mul, MulAssign, mul_assign);
impl core::ops::Neg for Dyadic {
type Output = Dyadic;
#[inline]
fn neg(self) -> Dyadic {
Dyadic::neg(&self)
}
}
// --- conversions to the other numeric types ---
#[cfg(feature = "rational")]
impl Dyadic {
/// Returns the exact value as a [`Rational`](crate::rational::Rational).
pub fn to_rational(&self) -> crate::rational::Rational {
use crate::rational::Rational;
if self.scale <= 0 {
Rational::from_integer(self.numerator.mul_2k((-self.scale) as u32))
} else {
Rational::new(self.numerator.clone(), Int::ONE.mul_2k(self.scale as u32))
}
}
/// Converts a [`Rational`](crate::rational::Rational) to a [`Dyadic`], or
/// `None` if its denominator is not a power of two.
pub fn try_from_rational(r: &crate::rational::Rational) -> Option<Dyadic> {
let k = r.denominator().is_power_of_two()?;
Some(Dyadic::new(r.numerator().clone(), k as i64))
}
}
#[cfg(feature = "float")]
impl Dyadic {
/// Rounds this value to a [`Float`](crate::float::Float) at `precision` bits.
pub fn to_float(
&self,
precision: u64,
mode: crate::float::RoundingMode,
) -> crate::float::Float {
// Exact rational → correctly rounded float.
crate::float::Float::from_rational(&self.to_rational(), precision, mode)
}
/// Converts a finite [`Float`](crate::float::Float) to an exact [`Dyadic`],
/// or `None` for NaN / ±∞ (finite floats are always exactly dyadic).
pub fn from_float(f: &crate::float::Float) -> Option<Dyadic> {
if f.is_zero() {
return Some(Dyadic::zero());
}
let sig = f.significand()?; // None for NaN/±∞
let exp = f.exponent()?; // value = ±sig · 2^exp = ±sig · 2^-(-exp)
let numerator = Int::from_sign_magnitude(f.sign(), sig.clone());
Some(Dyadic::new(numerator, -exp))
}
}
impl core::str::FromStr for Dyadic {
type Err = crate::error::Error;
/// Parses a decimal (`"3"`, `"-1.5"`, `"0.25"`); returns
/// [`Error::Parse`](crate::error::Error::Parse) if the value is not dyadic
/// (its reduced denominator is not a power of two).
#[cfg(feature = "rational")]
fn from_str(s: &str) -> crate::error::Result<Dyadic> {
let r: crate::rational::Rational = s.parse()?;
Dyadic::try_from_rational(&r).ok_or(crate::error::Error::Parse)
}
/// Without the `rational` feature, only plain integers parse.
#[cfg(not(feature = "rational"))]
fn from_str(s: &str) -> crate::error::Result<Dyadic> {
Ok(Dyadic::from_int(s.parse()?))
}
}