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
use crate::{
error::assert_finite,
fbig::FBig,
repr::{Context, Repr},
round::{mode, Round, Rounded},
utils::{shr_digits, split_digits, split_digits_ref},
};
use dashu_base::{Approximation::*, Sign};
use dashu_int::{IBig, Word};
impl<R: Round, const B: Word> FBig<R, B> {
/// Get the integral part of the float
///
/// See [FBig::round] for how the output precision is determined.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("1.234")?;
/// assert_eq!(a.trunc(), DBig::from_str("1")?);
/// // the actual precision of the integral part is 1 digit
/// assert_eq!(a.trunc().precision(), 1);
/// # Ok::<(), ParseError>(())
/// ```
///
/// # Panics
///
/// Panics if the number is infinte
#[inline]
pub fn trunc(&self) -> Self {
assert_finite(&self.repr);
if self.repr.exponent >= 0 {
return self.clone();
} else if self.repr.smaller_than_one() {
return Self::ZERO;
}
let shift = (-self.repr.exponent) as usize;
let signif = shr_digits::<B>(&self.repr.significand, shift);
let context = Context::new(self.context.precision.saturating_sub(shift));
FBig::new(Repr::new(signif, 0), context)
}
// Split the float number at the radix point, assuming it exists (the number is not a integer).
// The method returns (integral part, fractional part, fractional scale).
//
// Different from the public `split_at_point()` API, this method doesn't take the ownership of
// this number.
pub(crate) fn split_at_point_internal(&self) -> (IBig, IBig, usize) {
debug_assert!(self.repr.exponent < 0);
let shift = (-self.repr.exponent) as usize;
if self.repr.smaller_than_one() {
// For numbers smaller than 1, the integral part is zero and the stored
// significand is the whole fractional payload.
//
// The third return value is the fractional scale, i.e. the number of
// radix digits after the point. It must be -exponent, because callers
// such as round_fract use it as the denominator exponent B^scale.
//
// This is intentionally not self.context.precision: context precision is
// the significant-digit precision of the float, while this value describes
// the positional scale of the fractional part.
return (IBig::ZERO, self.repr.significand.clone(), shift);
}
let (hi, lo) = split_digits_ref::<B>(&self.repr.significand, shift);
(hi, lo, shift)
}
/// Split the rational number into integral and fractional parts (split at the radix point)
///
/// It's equivalent to `(self.trunc(), self.fract())`
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("1.234")?;
/// let (trunc, fract) = a.split_at_point();
/// assert_eq!(trunc, DBig::from_str("1.0")?);
/// assert_eq!(fract, DBig::from_str("0.234")?);
/// // the actual precision of the fractional part is 3 digits
/// assert_eq!(trunc.precision(), 1);
/// assert_eq!(fract.precision(), 3);
/// # Ok::<(), ParseError>(())
/// ```
pub fn split_at_point(self) -> (Self, Self) {
// trivial case when the exponent is positive
if self.repr.exponent >= 0 {
return (self, Self::ZERO);
} else if self.repr.smaller_than_one() {
return (Self::ZERO, self);
}
let shift = (-self.repr.exponent) as usize;
let (hi, lo) = split_digits::<B>(self.repr.significand, shift);
let hi_ctxt = Context::new(self.context.precision.saturating_sub(shift));
let lo_ctxt = Context::new(shift);
(
FBig::new(Repr::new(hi, 0), hi_ctxt),
FBig::new(Repr::new(lo, self.repr.exponent), lo_ctxt),
)
}
/// Get the fractional part of the float
///
/// **Note**: this function will adjust the precision accordingly!
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("1.234")?;
/// assert_eq!(a.fract(), DBig::from_str("0.234")?);
/// // the actual precision of the fractional part is 3 digits
/// assert_eq!(a.fract().precision(), 3);
/// # Ok::<(), ParseError>(())
/// ```
///
/// # Panics
///
/// Panics if the number is infinte
#[inline]
pub fn fract(&self) -> Self {
assert_finite(&self.repr);
if self.repr.exponent >= 0 {
return Self::ZERO;
} else if self.repr.smaller_than_one() {
return self.clone();
}
let (_, lo, precision) = self.split_at_point_internal();
let context = Context::new(precision);
FBig::new(Repr::new(lo, self.repr.exponent), context)
}
/// Returns the smallest integer greater than or equal to self.
///
/// See [FBig::round] for how the output precision is determined.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("1.234")?;
/// assert_eq!(a.ceil(), DBig::from_str("2")?);
///
/// // works for very large exponent
/// let b = DBig::from_str("1.234e10000")?;
/// assert_eq!(b.ceil(), b);
/// # Ok::<(), ParseError>(())
/// ```
///
/// # Panics
///
/// Panics if the number is infinte
#[inline]
pub fn ceil(&self) -> Self {
assert_finite(&self.repr);
if self.repr.is_zero() || self.repr.exponent >= 0 {
return self.clone();
} else if self.repr.smaller_than_one() {
return match self.repr.sign() {
Sign::Positive => Self::ONE,
Sign::Negative => Self::ZERO,
};
}
let (hi, lo, precision) = self.split_at_point_internal();
let rounding = mode::Up::round_fract::<B>(&hi, lo, precision);
let context = Context::new(self.context.precision.saturating_sub(precision));
FBig::new(Repr::new(hi + rounding, 0), context)
}
/// Returns the largest integer less than or equal to self.
///
/// See [FBig::round] for how the output precision is determined.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("1.234")?;
/// assert_eq!(a.floor(), DBig::from_str("1")?);
///
/// // works for very large exponent
/// let b = DBig::from_str("1.234e10000")?;
/// assert_eq!(b.floor(), b);
/// # Ok::<(), ParseError>(())
/// ```
///
/// # Panics
///
/// Panics if the number is infinte
#[inline]
pub fn floor(&self) -> Self {
assert_finite(&self.repr);
if self.repr.exponent >= 0 {
return self.clone();
} else if self.repr.smaller_than_one() {
return match self.repr.sign() {
Sign::Positive => Self::ZERO,
Sign::Negative => Self::NEG_ONE,
};
}
let (hi, lo, precision) = self.split_at_point_internal();
let rounding = mode::Down::round_fract::<B>(&hi, lo, precision);
let context = Context::new(self.context.precision.saturating_sub(precision));
FBig::new(Repr::new(hi + rounding, 0), context)
}
/// Returns the integer nearest to self.
///
/// If there are two integers equally close, then the one farther from zero is chosen.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// let a = DBig::from_str("1.234")?;
/// assert_eq!(a.round(), DBig::from_str("1")?);
///
/// // works for very large exponent
/// let b = DBig::from_str("1.234e10000")?;
/// assert_eq!(b.round(), b);
/// # Ok::<(), ParseError>(())
/// ```
///
/// # Precision
///
/// If `self` is an integer, the result will have the same precision as `self`.
/// If `self` has fractional part, then the precision will be subtracted by the digits
/// in the fractional part. Examples:
/// * `1.00e100` (precision = 3) rounds to `1.00e100` (precision = 3)
/// * `1.234` (precision = 4) rounds to `1.` (precision = 1)
/// * `1.234e-10` (precision = 4) rounds to `0.` (precision = 0, i.e arbitrary precision)
///
/// # Panics
///
/// Panics if the number is infinte
pub fn round(&self) -> Self {
assert_finite(&self.repr);
if self.repr.exponent >= 0 {
return self.clone();
} else if self.repr.exponent + (self.repr.digits_ub() as isize) < -2 {
// to determine if the number rounds to zero, we need to make sure |self| < 0.5
// which is stricter than `self.repr.smaller_than_one()`
return Self::ZERO;
}
let (hi, lo, precision) = self.split_at_point_internal();
let rounding = mode::HalfAway::round_fract::<B>(&hi, lo, precision);
let context = Context::new(self.context.precision.saturating_sub(precision));
FBig::new(Repr::new(hi + rounding, 0), context)
}
/// Round the number to the nearest multiple of `BASE^exp`.
///
/// This is the dashu analog of Python's `Decimal.quantize()`. The result's
/// value is an exact multiple of `BASE^exp`, and its precision is set so that
/// [`ulp()`][FBig::ulp] equals `BASE^exp`. Because dashu floats are
/// normalized, trailing zeros are not preserved in storage (the stored
/// exponent may be coarser than `exp`), but the value and ULP are exact. The
/// result keeps `self`'s rounding mode.
///
/// # Examples
///
/// ```
/// # use core::str::FromStr;
/// # use dashu_base::ParseError;
/// # use dashu_float::DBig;
/// use dashu_base::Approximation::*;
/// use dashu_float::round::Rounding::*;
///
/// let a = DBig::from_str("1.234")?; // precision 4
///
/// // round to 2 fractional digits (exp = -2): 3 significant figures remain
/// assert_eq!(a.quantize(-2), Inexact(DBig::from_str("1.23")?, NoOp));
/// assert_eq!(a.quantize(-2).value().precision(), 3);
///
/// // a finer quantum is exact (no rounding) and *increases* the precision
/// assert_eq!(a.quantize(-10), Exact(DBig::from_str("1.234")?));
/// assert_eq!(a.quantize(-10).value().precision(), 11);
///
/// // round to integer (exp = 0), or to the nearest 1000 (exp = 3)
/// assert_eq!(a.quantize(0), Inexact(DBig::from_str("1")?, NoOp));
/// assert_eq!(DBig::from_str("999")?.quantize(3), Inexact(DBig::from_str("1000")?, AddOne));
/// # Ok::<(), ParseError>(())
/// ```
///
/// # Panics
///
/// Panics if the number is infinte
pub fn quantize(&self, exp: isize) -> Rounded<Self> {
assert_finite(&self.repr);
if self.repr.is_zero() {
return Exact(self.clone());
}
let self_exp = self.repr.exponent;
if exp <= self_exp {
// finer-or-equal quantum: self is already an exact multiple of BASE^exp,
// so only the precision changes (set so that ulp == BASE^exp).
let precision = (self_exp + self.repr.digits() as isize - exp) as usize;
return Exact(FBig::new(self.repr.clone(), Context::new(precision)));
}
// coarser quantum: round off (exp - self_exp) low-order digits.
// Because a normalized significand is never divisible by BASE, this branch
// is always inexact (its low part is never all-zero).
let shift = (exp - self_exp) as usize;
let (hi, lo) = split_digits_ref::<B>(&self.repr.significand, shift);
let adjust = R::round_fract::<B>(&hi, lo, shift);
let repr = Repr::new(hi + adjust, exp);
// precision is set so that ulp == BASE^exp; a result that rounds to zero
// has no meaningful ulp, so it gets unlimited precision (like `round()`).
let precision = if repr.is_zero() {
0
} else {
(repr.exponent + repr.digits() as isize - exp) as usize
};
Inexact(FBig::new(repr, Context::new(precision)), adjust)
}
}