decimal_scaled/consts.rs
1//! Mathematical constants and float-compatibility constants for every
2//! decimal width.
3//!
4//! # Constants provided
5//!
6//! The [`DecimalConsts`] trait exposes `pi`, `tau`, `half_pi`,
7//! `quarter_pi`, `golden`, and `e` as methods on every width. The
8//! native-tier (`D38` and narrower) impls live here; the wide tier
9//! (`D76` / `D153` / `D307`) impls live in `consts_wide.rs`.
10//!
11//! Two inherent associated constants, `EPSILON` and `MIN_POSITIVE`, are
12//! provided as analogues to `f64::EPSILON` and `f64::MIN_POSITIVE` so
13//! that generic code parameterised over numeric types continues to
14//! compile when `T` is any of the decimal widths.
15//!
16//! # Precision strategy
17//!
18//! Constants are derived from raw integer references — no `f64`
19//! anywhere. Each tier has its own reference at the tier's maximum
20//! storage precision; the rescale to the caller's `SCALE` is always
21//! **downward**, never upward, so half-to-even rounding always lands
22//! on the **correctly-rounded** value at the target scale:
23//!
24//! | Tier | Reference storage | `SCALE_REF` (= reference digits) | Source file |
25//! |----------------|-------------------|----------------------------------|-------------------|
26//! | D9 / D18 / D38 | `Int256` | 75 | this file |
27//! | D76 | `Int256` | 75 | `consts_wide.rs` |
28//! | D153 | `Int512` | 153 | `consts_wide.rs` |
29//! | D307 | `Int1024` | 307 | `consts_wide.rs` |
30//!
31//! The rescale from `SCALE_REF` to the caller's `SCALE` uses integer
32//! division with the crate-default [`RoundingMode`] (half-to-even by
33//! default; overridable via the `rounding-*` Cargo features). Going
34//! through `f64` would cap precision at ~15–17 decimal digits; the
35//! raw-integer path preserves the full per-tier reference width.
36//!
37//! **0.5 ULP at every supported scale**, on every width, with no
38//! exceptions in the precision contract. The only constraint is the
39//! width's *storage range*: a value that mathematically exceeds the
40//! type's `Storage::MAX / 10^SCALE` cannot be represented at all. At
41//! `D38<38>` the storage range is approximately ±1.70141, so the three
42//! larger-magnitude constants — `pi ≈ 3.14159`, `tau ≈ 6.28318`,
43//! `e ≈ 2.71828` — overflow `i128` and the corresponding methods panic
44//! with a clear "constant out of storage range" message;
45//! `half_pi ≈ 1.57080`, `quarter_pi ≈ 0.78540`, and `golden ≈ 1.61803`
46//! all fit inside ±1.70141 and remain correctly-rounded to 0.5 ULP.
47//!
48//! [`RoundingMode`]: crate::rounding::RoundingMode
49//!
50//! # Sources
51//!
52//! Each raw constant is the half-to-even rounding of the canonical
53//! decimal expansion to the tier's `SCALE_REF` fractional digits. ISO
54//! 80000-2 (pi, tau, pi/2, pi/4), OEIS A001113 (e), OEIS A001622
55//! (golden ratio).
56
57use crate::core_type::D38;
58use crate::d_w128_kernels::Fixed;
59use crate::wide_int::Int256;
60
61/// Reference scale for every constant in this file: the 75-digit
62/// representation that fits an `Int256` (`2 · 128` bits). Every D38
63/// scale (0..=38) is at most 38 digits, so we always rescale **down**
64/// from 75 → SCALE, never up. The half-to-even rescale-down step is
65/// performed by [`Fixed::round_to_i128`] (`Fixed` is the same 256-bit
66/// guard-digit type the strict transcendentals use), giving 0.5 ULP at
67/// the caller's `SCALE` for every value that fits `i128` at that
68/// scale.
69///
70/// # Precision
71///
72/// N/A: constant value, no arithmetic performed.
73const SCALE_REF: u32 = 75;
74
75// Raw decimal strings at 75 fractional digits, materialised at build
76// time by `build.rs` (the same hand-rolled multi-precision generator
77// that emits the wide-tier constants). Sources: ISO 80000-2 (pi, tau,
78// pi/2, pi/4), OEIS A001113 (e), OEIS A001622 (golden ratio).
79//
80// The build-time string -> Int256 parse is `const fn` (via
81// `Int256::from_str_radix`, base 10 only). The 75-digit reference is
82// the largest decimal expansion that always fits Int256 for the
83// biggest of these constants (tau ≈ 6.28×10⁷⁵ < Int256::MAX ≈
84// 5.78×10⁷⁶); a single shared SCALE_REF keeps the rescale helpers
85// uniform across all six methods on the trait.
86
87include!(concat!(env!("OUT_DIR"), "/wide_consts.rs"));
88
89pub(crate) const PI_RAW: Int256 = match Int256::from_str_radix(PI_D76_S75, 10) {
90 Ok(v) => v,
91 Err(_) => panic!("consts: PI_D76_S75 not parseable"),
92};
93const TAU_RAW: Int256 = match Int256::from_str_radix(TAU_D76_S75, 10) {
94 Ok(v) => v,
95 Err(_) => panic!("consts: TAU_D76_S75 not parseable"),
96};
97const HALF_PI_RAW: Int256 = match Int256::from_str_radix(HALF_PI_D76_S75, 10) {
98 Ok(v) => v,
99 Err(_) => panic!("consts: HALF_PI_D76_S75 not parseable"),
100};
101const QUARTER_PI_RAW: Int256 = match Int256::from_str_radix(QUARTER_PI_D76_S75, 10) {
102 Ok(v) => v,
103 Err(_) => panic!("consts: QUARTER_PI_D76_S75 not parseable"),
104};
105const E_RAW: Int256 = match Int256::from_str_radix(E_D76_S75, 10) {
106 Ok(v) => v,
107 Err(_) => panic!("consts: E_D76_S75 not parseable"),
108};
109const GOLDEN_RAW: Int256 = match Int256::from_str_radix(GOLDEN_D76_S75, 10) {
110 Ok(v) => v,
111 Err(_) => panic!("consts: GOLDEN_D76_S75 not parseable"),
112};
113
114/// Rescale a 75-digit `Int256` reference down to the caller's `TARGET`
115/// scale as an `i128`, half-to-even. Panics if the value at `TARGET`
116/// does not fit `i128` (the type's storage range at that scale just
117/// doesn't include this constant — e.g. `pi ≈ 3.14` at `D38<38>` would
118/// need `3.14 × 10^38 ≈ 3.14e38`, which exceeds `i128::MAX ≈ 1.7e38`).
119fn rescale_75_to_target<const TARGET: u32>(raw: Int256, name: &'static str) -> i128 {
120 // Int256 storage is [u64; 4]; the D38 Fixed kernel uses [u128; 2].
121 // Pack pairs of u64 limbs into u128 little-endian halves.
122 let words = raw.0;
123 let mag: [u128; 2] = [
124 (words[0] as u128) | ((words[1] as u128) << 64),
125 (words[2] as u128) | ((words[3] as u128) << 64),
126 ];
127 let f = Fixed { negative: false, mag };
128 match f.round_to_i128(SCALE_REF, TARGET) {
129 Some(v) => v,
130 None => panic!(
131 "D38 constant out of storage range: {name} cannot fit i128 at SCALE = {TARGET} \
132 (storage range is ±i128::MAX / 10^SCALE)",
133 name = name,
134 TARGET = TARGET,
135 ),
136 }
137}
138
139/// Well-known mathematical constants available on every decimal width
140/// (`D9` / `D18` / `D38` / `D76` / `D153` / `D307`).
141///
142/// Import this trait to call `D38s12::pi()`, `D76::<35>::e()`, etc.
143///
144/// All returned values are computed from a raw integer reference at
145/// the tier's maximum storage precision (75 digits for D9/D18/D38 and
146/// D76; 153 for D153; 307 for D307) without passing through `f64`,
147/// then rescaled down to the caller's `SCALE` with half-to-even
148/// rounding. The result is **within 0.5 ULP** of the canonical
149/// decimal expansion at every supported scale on every width.
150///
151/// The one situation where a method does not return a value is when
152/// the constant's magnitude exceeds the type's storage range at the
153/// caller's `SCALE` — e.g. `D38<38>::pi()` would need `3.14 × 10³⁸`,
154/// which exceeds `i128::MAX ≈ 1.7×10³⁸`. The method panics with a
155/// clear "constant out of storage range" message in that case.
156pub trait DecimalConsts: Sized {
157 /// Pi (~3.14159265...). One half-turn in radians.
158 ///
159 /// Source: ISO 80000-2 / OEIS A000796. Rescaled per-tier (see the
160 /// module-level table) to the caller's `SCALE` via the crate-default
161 /// rounding mode.
162 ///
163 /// # Precision
164 ///
165 /// N/A: constant value, no arithmetic performed.
166 fn pi() -> Self;
167
168 /// Tau (~6.28318530...). One full turn in radians.
169 ///
170 /// Defined as `2 * pi`. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
171 ///
172 /// # Precision
173 ///
174 /// N/A: constant value, no arithmetic performed.
175 fn tau() -> Self;
176
177 /// Half-pi (~1.57079632...). One quarter-turn in radians.
178 ///
179 /// Defined as `pi / 2`. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
180 ///
181 /// # Precision
182 ///
183 /// N/A: constant value, no arithmetic performed.
184 fn half_pi() -> Self;
185
186 /// Quarter-pi (~0.78539816...). One eighth-turn in radians.
187 ///
188 /// Defined as `pi / 4`. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
189 ///
190 /// # Precision
191 ///
192 /// N/A: constant value, no arithmetic performed.
193 fn quarter_pi() -> Self;
194
195 /// The golden ratio (~1.61803398...). Dimensionless.
196 ///
197 /// Defined as `(1 + sqrt(5)) / 2`. Source: OEIS A001622. Rescaled
198 /// per-tier (see the module-level table) to the caller's `SCALE`
199 /// via the crate-default rounding mode.
200 ///
201 /// # Precision
202 ///
203 /// N/A: constant value, no arithmetic performed.
204 fn golden() -> Self;
205
206 /// Euler's number (~2.71828182...). Dimensionless.
207 ///
208 /// Source: OEIS A001113. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
209 ///
210 /// # Precision
211 ///
212 /// N/A: constant value, no arithmetic performed.
213 fn e() -> Self;
214}
215
216// Public-to-crate helpers that return each constant's rescaled bits at
217// the caller's target SCALE. Used by the `decl_decimal_consts!` macro
218// to provide DecimalConsts for narrower widths (D9, D18) without
219// duplicating the rescale logic.
220
221pub(crate) fn pi_at_target<const TARGET: u32>() -> i128 {
222 rescale_75_to_target::<TARGET>(PI_RAW, "pi")
223}
224pub(crate) fn tau_at_target<const TARGET: u32>() -> i128 {
225 rescale_75_to_target::<TARGET>(TAU_RAW, "tau")
226}
227pub(crate) fn half_pi_at_target<const TARGET: u32>() -> i128 {
228 rescale_75_to_target::<TARGET>(HALF_PI_RAW, "half_pi")
229}
230pub(crate) fn quarter_pi_at_target<const TARGET: u32>() -> i128 {
231 rescale_75_to_target::<TARGET>(QUARTER_PI_RAW, "quarter_pi")
232}
233pub(crate) fn golden_at_target<const TARGET: u32>() -> i128 {
234 rescale_75_to_target::<TARGET>(GOLDEN_RAW, "golden")
235}
236pub(crate) fn e_at_target<const TARGET: u32>() -> i128 {
237 rescale_75_to_target::<TARGET>(E_RAW, "e")
238}
239
240// The `DecimalConsts` impl for `D38<SCALE>` is emitted by the
241// `decl_decimal_consts!` macro — the same macro D9 / D18 / D76+ use.
242// It expands to `Self(pi_at_target::<SCALE>())` etc.; each
243// `*_at_target` helper above rescales the 75-digit Int256 reference
244// down to the caller's `SCALE` via half-to-even and narrows to i128
245// (or panics with a clear message if the constant's magnitude
246// exceeds the storage range at that scale).
247crate::macros::consts::decl_decimal_consts!(D38, i128);
248
249// Inherent associated constants: EPSILON / MIN_POSITIVE.
250//
251// These mirror `f64::EPSILON` and `f64::MIN_POSITIVE` so that generic
252// numeric code that calls `T::EPSILON` or `T::MIN_POSITIVE` compiles
253// when `T = D38<SCALE>`. For D38 both equal `D38(1)` -- the smallest
254// representable positive value (1 LSB = 10^-SCALE). There are no subnormals.
255
256impl<const SCALE: u32> D38<SCALE> {
257 /// Smallest representable positive value: 1 LSB = `10^-SCALE`.
258 ///
259 /// Provided as an analogue to `f64::EPSILON` for generic numeric code.
260 /// Note that this differs from the f64 definition ("difference between
261 /// 1.0 and the next-larger f64"): for `D38` the LSB is uniform across
262 /// the entire representable range.
263 ///
264 /// # Precision
265 ///
266 /// N/A: constant value, no arithmetic performed.
267 pub const EPSILON: Self = Self(1);
268
269 /// Smallest positive value (equal to [`Self::EPSILON`]).
270 ///
271 /// Provided as an analogue to `f64::MIN_POSITIVE` for generic numeric
272 /// code. Unlike `f64`, `D38` has no subnormals, so `MIN_POSITIVE`
273 /// and `EPSILON` are the same value.
274 ///
275 /// # Precision
276 ///
277 /// N/A: constant value, no arithmetic performed.
278 pub const MIN_POSITIVE: Self = Self(1);
279}
280
281#[cfg(test)]
282mod tests {
283 use super::*;
284 use crate::core_type::D38s12;
285
286 // Bit-exact assertions at SCALE = 12.
287 //
288 // At SCALE = 12 each constant is the 37-digit raw integer divided by
289 // 10^23, rounded half-to-even.
290
291 /// pi at SCALE=12: raw / 10^23.
292 /// Truncated 13 digits: 3_141_592_653_589.
293 /// 14th digit is 7 (from position 14 of the raw) -> round up.
294 /// Expected: 3_141_592_653_590.
295 #[test]
296 fn pi_is_bit_exact_at_scale_12() {
297 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
298 assert_eq!(D38s12::pi().to_bits(), 3_141_592_653_590_i128);
299 }
300
301 /// tau at SCALE=12: raw / 10^23.
302 /// Truncated 13 digits: 6_283_185_307_179.
303 /// 14th digit is 5 -> round up. Expected: 6_283_185_307_180.
304 #[test]
305 fn tau_is_bit_exact_at_scale_12() {
306 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
307 assert_eq!(D38s12::tau().to_bits(), 6_283_185_307_180_i128);
308 }
309
310 /// half_pi at SCALE=12: raw / 10^23.
311 /// Truncated 13 digits: 1_570_796_326_794.
312 /// 14th digit is 8 -> round up. Expected: 1_570_796_326_795.
313 #[test]
314 fn half_pi_is_bit_exact_at_scale_12() {
315 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
316 assert_eq!(D38s12::half_pi().to_bits(), 1_570_796_326_795_i128);
317 }
318
319 /// quarter_pi at SCALE=12: raw / 10^23.
320 /// Truncated 12 digits: 785_398_163_397.
321 /// 13th digit is 4 -> no round-up. Expected: 785_398_163_397.
322 #[test]
323 fn quarter_pi_is_bit_exact_at_scale_12() {
324 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
325 assert_eq!(D38s12::quarter_pi().to_bits(), 785_398_163_397_i128);
326 }
327
328 /// e at SCALE=12: raw / 10^23.
329 /// Truncated 13 digits: 2_718_281_828_459.
330 /// 14th digit is 0 -> no round-up. Expected: 2_718_281_828_459.
331 #[test]
332 fn e_is_bit_exact_at_scale_12() {
333 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
334 assert_eq!(D38s12::e().to_bits(), 2_718_281_828_459_i128);
335 }
336
337 /// golden at SCALE=12: raw / 10^23.
338 /// Truncated 13 digits: 1_618_033_988_749.
339 /// 14th digit is 8 -> round up. Expected: 1_618_033_988_750.
340 #[test]
341 fn golden_is_bit_exact_at_scale_12() {
342 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
343 assert_eq!(D38s12::golden().to_bits(), 1_618_033_988_750_i128);
344 }
345
346 // Closeness checks against core::f64::consts.
347 // These verify that the correct reference digits were selected; the
348 // bit-exact tests above are the primary acceptance criteria.
349
350 /// pi() converted to f64 is within 1e-11 of `core::f64::consts::PI`.
351 /// At SCALE=12, 1 LSB = 1e-12, so 1e-11 covers rescale rounding plus
352 /// the f64 conversion step.
353 #[test]
354 fn pi_close_to_f64_pi() {
355 let diff = (D38s12::pi().to_f64() - core::f64::consts::PI).abs();
356 assert!(diff < 1e-11, "pi diverges from f64 PI by {diff}");
357 }
358
359 #[test]
360 fn tau_close_to_f64_tau() {
361 let diff = (D38s12::tau().to_f64() - core::f64::consts::TAU).abs();
362 assert!(diff < 1e-11, "tau diverges from f64 TAU by {diff}");
363 }
364
365 #[test]
366 fn half_pi_close_to_f64_frac_pi_2() {
367 let diff =
368 (D38s12::half_pi().to_f64() - core::f64::consts::FRAC_PI_2).abs();
369 assert!(diff < 1e-11, "half_pi diverges from f64 FRAC_PI_2 by {diff}");
370 }
371
372 #[test]
373 fn quarter_pi_close_to_f64_frac_pi_4() {
374 let diff =
375 (D38s12::quarter_pi().to_f64() - core::f64::consts::FRAC_PI_4).abs();
376 assert!(
377 diff < 1e-11,
378 "quarter_pi diverges from f64 FRAC_PI_4 by {diff}"
379 );
380 }
381
382 #[test]
383 fn e_close_to_f64_e() {
384 let diff = (D38s12::e().to_f64() - core::f64::consts::E).abs();
385 assert!(diff < 1e-11, "e diverges from f64 E by {diff}");
386 }
387
388 /// golden() converted to f64 is within 1e-11 of the closed form
389 /// `(1 + sqrt(5)) / 2`. Requires std for `f64::sqrt`.
390 #[cfg(feature = "std")]
391 #[test]
392 fn golden_close_to_closed_form() {
393 let expected = (1.0_f64 + 5.0_f64.sqrt()) / 2.0;
394 let diff = (D38s12::golden().to_f64() - expected).abs();
395 assert!(diff < 1e-11, "golden diverges from closed-form by {diff}");
396 }
397
398 // EPSILON / MIN_POSITIVE
399
400 #[test]
401 fn epsilon_is_one_ulp() {
402 assert_eq!(D38s12::EPSILON.to_bits(), 1_i128);
403 assert!(D38s12::EPSILON > D38s12::ZERO);
404 }
405
406 #[test]
407 fn min_positive_is_one_ulp() {
408 assert_eq!(D38s12::MIN_POSITIVE.to_bits(), 1_i128);
409 assert_eq!(D38s12::MIN_POSITIVE, D38s12::EPSILON);
410 }
411
412 /// At SCALE = 6 the LSB is 10^-6; EPSILON is still raw 1.
413 #[test]
414 fn epsilon_at_scale_6_is_one_ulp() {
415 type D6 = D38<6>;
416 assert_eq!(D6::EPSILON.to_bits(), 1_i128);
417 assert_eq!(D6::MIN_POSITIVE.to_bits(), 1_i128);
418 }
419
420 // Cross-scale exercises
421
422 /// At SCALE = 6, pi() should equal 3.141593 (rounded half-to-even from
423 /// 3.1415926535...). Expected raw bits: 3_141_593.
424 #[test]
425 fn pi_at_scale_6_is_bit_exact() {
426 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
427 type D6 = D38<6>;
428 assert_eq!(D6::pi().to_bits(), 3_141_593_i128);
429 }
430
431 /// At SCALE = 0, pi() rounds to 3 (first fractional digit is 1, no
432 /// round-up).
433 #[test]
434 fn pi_at_scale_0_is_three() {
435 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
436 type D0 = D38<0>;
437 assert_eq!(D0::pi().to_bits(), 3_i128);
438 }
439
440 /// `D38<37>::pi()` is the canonical pi rounded half-to-even to 37
441 /// fractional digits. The 75-digit Int256 reference is rescaled
442 /// down to 37 digits; the result is bit-identical to the
443 /// hand-tabulated constant.
444 #[test]
445 fn pi_at_scale_37_matches_canonical_37_digit_rounding() {
446 type D37 = D38<37>;
447 // pi to 38 digits: 3.14159265358979323846264338327950288420
448 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
449 // keep 37 frac digits; the 38th digit is 0
450 // so half-to-even rounds down — no bump.
451 let expected: i128 = 31_415_926_535_897_932_384_626_433_832_795_028_842;
452 assert_eq!(D37::pi().to_bits(), expected);
453 }
454
455 // `D38<38>` storage range is approximately ±1.70141 (i128::MAX /
456 // 10^38). The three constants whose magnitude exceeds that bound
457 // must panic with a clear "out of storage range" message:
458 //
459 // - pi ≈ 3.14159 > 1.70141 → must panic
460 // - tau ≈ 6.28318 > 1.70141 → must panic
461 // - e ≈ 2.71828 > 1.70141 → must panic
462 //
463 // The three that DO fit must be correctly rounded to 0.5 ULP:
464 //
465 // - half_pi ≈ 1.57079 < 1.70141 → must round to 0.5 ULP
466 // - quarter_pi ≈ 0.78540 < 1.70141 → must round to 0.5 ULP
467 // - golden ≈ 1.61803 < 1.70141 → must round to 0.5 ULP
468
469 #[test]
470 #[should_panic(expected = "out of storage range")]
471 fn pi_at_scale_38_panics_storage_range() {
472 let _ = D38::<38>::pi();
473 }
474
475 #[test]
476 #[should_panic(expected = "out of storage range")]
477 fn tau_at_scale_38_panics_storage_range() {
478 let _ = D38::<38>::tau();
479 }
480
481 #[test]
482 #[should_panic(expected = "out of storage range")]
483 fn e_at_scale_38_panics_storage_range() {
484 let _ = D38::<38>::e();
485 }
486
487 /// `half_pi` / `quarter_pi` / `golden` at `D38<38>` must not panic
488 /// (their magnitudes are inside the type's ±1.7 storage range) and
489 /// each must be correctly rounded to 0.5 ULP (= 1 LSB).
490 #[test]
491 fn fitting_constants_at_scale_38_are_correctly_rounded() {
492 // half_pi to 38 digits: 1.57079632679489661923132169163975144210
493 let expected_half_pi: i128 = 157_079_632_679_489_661_923_132_169_163_975_144_210;
494 let got = D38::<38>::half_pi().to_bits();
495 let diff = (got - expected_half_pi).abs();
496 assert!(diff <= 1, "half_pi: got {got}, expected {expected_half_pi}, diff {diff} > 1 LSB");
497
498 // quarter_pi to 38 digits: 0.78539816339744830961566084581987572105
499 let expected_quarter_pi: i128 = 78_539_816_339_744_830_961_566_084_581_987_572_105;
500 let got = D38::<38>::quarter_pi().to_bits();
501 let diff = (got - expected_quarter_pi).abs();
502 assert!(diff <= 1, "quarter_pi: got {got}, expected {expected_quarter_pi}, diff {diff} > 1 LSB");
503
504 // golden to 38 digits: 1.61803398874989484820458683436563811772
505 let expected_golden: i128 = 161_803_398_874_989_484_820_458_683_436_563_811_772;
506 let got = D38::<38>::golden().to_bits();
507 let diff = (got - expected_golden).abs();
508 assert!(diff <= 1, "golden: got {got}, expected {expected_golden}, diff {diff} > 1 LSB");
509 }
510
511 /// Negative-side rounding: negating pi gives the expected raw bits.
512 #[test]
513 fn neg_pi_round_trip() {
514 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
515 let pi = D38s12::pi();
516 let neg_pi = -pi;
517 assert_eq!(neg_pi.to_bits(), -3_141_592_653_590_i128);
518 }
519
520 // (`rescale_from_ref` boundary tests removed: the rounding logic now
521 // lives in `D38::rescale` / `src/rounding.rs::apply_rounding` and is
522 // covered by the tests in those modules.)
523}