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 rescale_75_to_target_with::<TARGET>(raw, name, crate::rounding::DEFAULT_ROUNDING_MODE)
121}
122
123/// Mode-aware variant of [`rescale_75_to_target`].
124///
125/// `Floor` gives the largest representable value ≤ true constant —
126/// useful when downstream code uses the value as an upper bound that
127/// must not be exceeded. `Ceiling` gives the smallest value ≥ true
128/// constant — useful for conservative bucket counts /
129/// over-approximation. The three half-modes coincide for irrational
130/// constants (no integer mantissa hits the exact half-way point at
131/// the 75-digit reference scale).
132fn rescale_75_to_target_with<const TARGET: u32>(
133 raw: Int256,
134 name: &'static str,
135 mode: crate::rounding::RoundingMode,
136) -> i128 {
137 let words = raw.0;
138 let mag: [u128; 2] = [
139 (words[0] as u128) | ((words[1] as u128) << 64),
140 (words[2] as u128) | ((words[3] as u128) << 64),
141 ];
142 let f = Fixed { negative: false, mag };
143 match f.round_to_i128_with(SCALE_REF, TARGET, mode) {
144 Some(v) => v,
145 None => panic!(
146 "D38 constant out of storage range: {name} cannot fit i128 at SCALE = {TARGET} \
147 (storage range is ±i128::MAX / 10^SCALE)",
148 name = name,
149 TARGET = TARGET,
150 ),
151 }
152}
153
154/// Well-known mathematical constants available on every decimal width
155/// (`D9` / `D18` / `D38` / `D76` / `D153` / `D307`).
156///
157/// Import this trait to call `D38s12::pi()`, `D76::<35>::e()`, etc.
158///
159/// All returned values are computed from a raw integer reference at
160/// the tier's maximum storage precision (75 digits for D9/D18/D38 and
161/// D76; 153 for D153; 307 for D307) without passing through `f64`,
162/// then rescaled down to the caller's `SCALE` with half-to-even
163/// rounding. The result is **within 0.5 ULP** of the canonical
164/// decimal expansion at every supported scale on every width.
165///
166/// The one situation where a method does not return a value is when
167/// the constant's magnitude exceeds the type's storage range at the
168/// caller's `SCALE` — e.g. `D38<38>::pi()` would need `3.14 × 10³⁸`,
169/// which exceeds `i128::MAX ≈ 1.7×10³⁸`. The method panics with a
170/// clear "constant out of storage range" message in that case.
171///
172/// # Crossing into f64
173///
174/// `to_f64()` is itself correctly rounded, but it can only round to
175/// the *decimal value the type holds* — not to the underlying ideal
176/// constant. `f64` carries ~15.95 decimal digits of mantissa, so any
177/// constant produced at `SCALE < 15` is intrinsically coarser than
178/// the `f64` grid: `D38<12>::pi().to_f64()` lands ~466 ULPs from
179/// [`std::f64::consts::PI`], because the 12-digit decimal rounds
180/// differently than the closest-`f64` to true π. At `SCALE ≥ 15` the
181/// round-trip is bit-exact for these constants (the decimal value
182/// has enough digits to disambiguate the `f64` grid).
183///
184/// **Practical rule for downstream code that crosses into `f64`** —
185/// CAD bulge-arc tessellation, OpenGL/GLSL, hardware drivers — and
186/// uses the `f64` value to count, bucket, or seed a fixed-iteration
187/// loop: source mathematical constants from [`std::f64::consts`]
188/// directly at the boundary rather than going through
189/// `Decimal::pi().to_f64()`. Otherwise pick a `SCALE` of 15 or more
190/// so the decimal value can round-trip to the canonical `f64`.
191pub trait DecimalConsts: Sized {
192 /// Pi (~3.14159265...). One half-turn in radians.
193 ///
194 /// Source: ISO 80000-2 / OEIS A000796. Rescaled per-tier (see the
195 /// module-level table) to the caller's `SCALE` via the crate-default
196 /// rounding mode.
197 ///
198 /// # Precision
199 ///
200 /// N/A: constant value, no arithmetic performed.
201 fn pi() -> Self;
202
203 /// Tau (~6.28318530...). One full turn in radians.
204 ///
205 /// Defined as `2 * pi`. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
206 ///
207 /// # Precision
208 ///
209 /// N/A: constant value, no arithmetic performed.
210 fn tau() -> Self;
211
212 /// Half-pi (~1.57079632...). One quarter-turn in radians.
213 ///
214 /// Defined as `pi / 2`. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
215 ///
216 /// # Precision
217 ///
218 /// N/A: constant value, no arithmetic performed.
219 fn half_pi() -> Self;
220
221 /// Quarter-pi (~0.78539816...). One eighth-turn in radians.
222 ///
223 /// Defined as `pi / 4`. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
224 ///
225 /// # Precision
226 ///
227 /// N/A: constant value, no arithmetic performed.
228 fn quarter_pi() -> Self;
229
230 /// The golden ratio (~1.61803398...). Dimensionless.
231 ///
232 /// Defined as `(1 + sqrt(5)) / 2`. Source: OEIS A001622. Rescaled
233 /// per-tier (see the module-level table) to the caller's `SCALE`
234 /// via the crate-default rounding mode.
235 ///
236 /// # Precision
237 ///
238 /// N/A: constant value, no arithmetic performed.
239 fn golden() -> Self;
240
241 /// Euler's number (~2.71828182...). Dimensionless.
242 ///
243 /// Source: OEIS A001113. Rescaled per-tier (see the module-level table) to the caller's `SCALE` via the crate-default rounding mode.
244 ///
245 /// # Precision
246 ///
247 /// N/A: constant value, no arithmetic performed.
248 fn e() -> Self;
249
250 // ─── *_with(mode) siblings ───────────────────────────────────
251 //
252 // Each `<const>_with(mode)` rescales the 75-digit reference under
253 // the caller-supplied `RoundingMode`. Useful when the default
254 // mode (half-to-even, or whatever a `rounding-*` Cargo feature
255 // selects) is the wrong direction for the use case — e.g. a CAD
256 // tessellation that needs `pi_with(Floor)` so the down-stream
257 // f64 conversion stays ≤ true π and segment counts can't
258 // over-flow their fixed-size buffers.
259
260 /// `pi()` under the supplied rounding mode.
261 fn pi_with(mode: crate::rounding::RoundingMode) -> Self;
262 /// `tau()` under the supplied rounding mode.
263 fn tau_with(mode: crate::rounding::RoundingMode) -> Self;
264 /// `half_pi()` under the supplied rounding mode.
265 fn half_pi_with(mode: crate::rounding::RoundingMode) -> Self;
266 /// `quarter_pi()` under the supplied rounding mode.
267 fn quarter_pi_with(mode: crate::rounding::RoundingMode) -> Self;
268 /// `golden()` under the supplied rounding mode.
269 fn golden_with(mode: crate::rounding::RoundingMode) -> Self;
270 /// `e()` under the supplied rounding mode.
271 fn e_with(mode: crate::rounding::RoundingMode) -> Self;
272}
273
274// Public-to-crate helpers that return each constant's rescaled bits at
275// the caller's target SCALE. Used by the `decl_decimal_consts!` macro
276// to provide DecimalConsts for narrower widths (D9, D18) without
277// duplicating the rescale logic.
278
279pub(crate) fn pi_at_target<const TARGET: u32>() -> i128 {
280 rescale_75_to_target::<TARGET>(PI_RAW, "pi")
281}
282pub(crate) fn tau_at_target<const TARGET: u32>() -> i128 {
283 rescale_75_to_target::<TARGET>(TAU_RAW, "tau")
284}
285pub(crate) fn half_pi_at_target<const TARGET: u32>() -> i128 {
286 rescale_75_to_target::<TARGET>(HALF_PI_RAW, "half_pi")
287}
288pub(crate) fn quarter_pi_at_target<const TARGET: u32>() -> i128 {
289 rescale_75_to_target::<TARGET>(QUARTER_PI_RAW, "quarter_pi")
290}
291pub(crate) fn golden_at_target<const TARGET: u32>() -> i128 {
292 rescale_75_to_target::<TARGET>(GOLDEN_RAW, "golden")
293}
294pub(crate) fn e_at_target<const TARGET: u32>() -> i128 {
295 rescale_75_to_target::<TARGET>(E_RAW, "e")
296}
297
298// Mode-aware variants — used by the `*_with(mode)` constant methods.
299
300pub(crate) fn pi_at_target_with<const TARGET: u32>(
301 mode: crate::rounding::RoundingMode,
302) -> i128 {
303 rescale_75_to_target_with::<TARGET>(PI_RAW, "pi", mode)
304}
305pub(crate) fn tau_at_target_with<const TARGET: u32>(
306 mode: crate::rounding::RoundingMode,
307) -> i128 {
308 rescale_75_to_target_with::<TARGET>(TAU_RAW, "tau", mode)
309}
310pub(crate) fn half_pi_at_target_with<const TARGET: u32>(
311 mode: crate::rounding::RoundingMode,
312) -> i128 {
313 rescale_75_to_target_with::<TARGET>(HALF_PI_RAW, "half_pi", mode)
314}
315pub(crate) fn quarter_pi_at_target_with<const TARGET: u32>(
316 mode: crate::rounding::RoundingMode,
317) -> i128 {
318 rescale_75_to_target_with::<TARGET>(QUARTER_PI_RAW, "quarter_pi", mode)
319}
320pub(crate) fn golden_at_target_with<const TARGET: u32>(
321 mode: crate::rounding::RoundingMode,
322) -> i128 {
323 rescale_75_to_target_with::<TARGET>(GOLDEN_RAW, "golden", mode)
324}
325pub(crate) fn e_at_target_with<const TARGET: u32>(
326 mode: crate::rounding::RoundingMode,
327) -> i128 {
328 rescale_75_to_target_with::<TARGET>(E_RAW, "e", mode)
329}
330
331// The `DecimalConsts` impl for `D38<SCALE>` is emitted by the
332// `decl_decimal_consts!` macro — the same macro D9 / D18 / D76+ use.
333// It expands to `Self(pi_at_target::<SCALE>())` etc.; each
334// `*_at_target` helper above rescales the 75-digit Int256 reference
335// down to the caller's `SCALE` via half-to-even and narrows to i128
336// (or panics with a clear message if the constant's magnitude
337// exceeds the storage range at that scale).
338crate::macros::consts::decl_decimal_consts!(D38, i128);
339
340// EPSILON / MIN_POSITIVE for every width are now emitted by
341// `decl_decimal_basics!`. The D38-specific inherent impl that used
342// to live here has been removed in favour of the macro-emitted ones
343// so D9 / D18 / D38 / D56 / D76 / D114 / D153 / D230 / D307 / D461 /
344// D615 / D923 / D1231 all share the same EPSILON / MIN_POSITIVE
345// surface.
346
347#[cfg(test)]
348mod tests {
349 use super::*;
350 use crate::core_type::D38s12;
351
352 // Bit-exact assertions at SCALE = 12.
353 //
354 // At SCALE = 12 each constant is the 37-digit raw integer divided by
355 // 10^23, rounded half-to-even.
356
357 /// pi at SCALE=12: raw / 10^23.
358 /// Truncated 13 digits: 3_141_592_653_589.
359 /// 14th digit is 7 (from position 14 of the raw) -> round up.
360 /// Expected: 3_141_592_653_590.
361 #[test]
362 fn pi_is_bit_exact_at_scale_12() {
363 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
364 assert_eq!(D38s12::pi().to_bits(), 3_141_592_653_590_i128);
365 }
366
367 /// tau at SCALE=12: raw / 10^23.
368 /// Truncated 13 digits: 6_283_185_307_179.
369 /// 14th digit is 5 -> round up. Expected: 6_283_185_307_180.
370 #[test]
371 fn tau_is_bit_exact_at_scale_12() {
372 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
373 assert_eq!(D38s12::tau().to_bits(), 6_283_185_307_180_i128);
374 }
375
376 /// half_pi at SCALE=12: raw / 10^23.
377 /// Truncated 13 digits: 1_570_796_326_794.
378 /// 14th digit is 8 -> round up. Expected: 1_570_796_326_795.
379 #[test]
380 fn half_pi_is_bit_exact_at_scale_12() {
381 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
382 assert_eq!(D38s12::half_pi().to_bits(), 1_570_796_326_795_i128);
383 }
384
385 /// quarter_pi at SCALE=12: raw / 10^23.
386 /// Truncated 12 digits: 785_398_163_397.
387 /// 13th digit is 4 -> no round-up. Expected: 785_398_163_397.
388 #[test]
389 fn quarter_pi_is_bit_exact_at_scale_12() {
390 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
391 assert_eq!(D38s12::quarter_pi().to_bits(), 785_398_163_397_i128);
392 }
393
394 /// e at SCALE=12: raw / 10^23.
395 /// Truncated 13 digits: 2_718_281_828_459.
396 /// 14th digit is 0 -> no round-up. Expected: 2_718_281_828_459.
397 #[test]
398 fn e_is_bit_exact_at_scale_12() {
399 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
400 assert_eq!(D38s12::e().to_bits(), 2_718_281_828_459_i128);
401 }
402
403 /// golden at SCALE=12: raw / 10^23.
404 /// Truncated 13 digits: 1_618_033_988_749.
405 /// 14th digit is 8 -> round up. Expected: 1_618_033_988_750.
406 #[test]
407 fn golden_is_bit_exact_at_scale_12() {
408 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
409 assert_eq!(D38s12::golden().to_bits(), 1_618_033_988_750_i128);
410 }
411
412 // Closeness checks against core::f64::consts.
413 // These verify that the correct reference digits were selected; the
414 // bit-exact tests above are the primary acceptance criteria.
415
416 /// pi() converted to f64 is within 1e-11 of `core::f64::consts::PI`.
417 /// At SCALE=12, 1 LSB = 1e-12, so 1e-11 covers rescale rounding plus
418 /// the f64 conversion step.
419 #[test]
420 fn pi_close_to_f64_pi() {
421 let diff = (D38s12::pi().to_f64() - core::f64::consts::PI).abs();
422 assert!(diff < 1e-11, "pi diverges from f64 PI by {diff}");
423 }
424
425 #[test]
426 fn tau_close_to_f64_tau() {
427 let diff = (D38s12::tau().to_f64() - core::f64::consts::TAU).abs();
428 assert!(diff < 1e-11, "tau diverges from f64 TAU by {diff}");
429 }
430
431 #[test]
432 fn half_pi_close_to_f64_frac_pi_2() {
433 let diff =
434 (D38s12::half_pi().to_f64() - core::f64::consts::FRAC_PI_2).abs();
435 assert!(diff < 1e-11, "half_pi diverges from f64 FRAC_PI_2 by {diff}");
436 }
437
438 #[test]
439 fn quarter_pi_close_to_f64_frac_pi_4() {
440 let diff =
441 (D38s12::quarter_pi().to_f64() - core::f64::consts::FRAC_PI_4).abs();
442 assert!(
443 diff < 1e-11,
444 "quarter_pi diverges from f64 FRAC_PI_4 by {diff}"
445 );
446 }
447
448 #[test]
449 fn e_close_to_f64_e() {
450 let diff = (D38s12::e().to_f64() - core::f64::consts::E).abs();
451 assert!(diff < 1e-11, "e diverges from f64 E by {diff}");
452 }
453
454 /// golden() converted to f64 is within 1e-11 of the closed form
455 /// `(1 + sqrt(5)) / 2`. Requires std for `f64::sqrt`.
456 #[cfg(feature = "std")]
457 #[test]
458 fn golden_close_to_closed_form() {
459 let expected = (1.0_f64 + 5.0_f64.sqrt()) / 2.0;
460 let diff = (D38s12::golden().to_f64() - expected).abs();
461 assert!(diff < 1e-11, "golden diverges from closed-form by {diff}");
462 }
463
464 // EPSILON / MIN_POSITIVE
465
466 #[test]
467 fn epsilon_is_one_ulp() {
468 assert_eq!(D38s12::EPSILON.to_bits(), 1_i128);
469 assert!(D38s12::EPSILON > D38s12::ZERO);
470 }
471
472 #[test]
473 fn min_positive_is_one_ulp() {
474 assert_eq!(D38s12::MIN_POSITIVE.to_bits(), 1_i128);
475 assert_eq!(D38s12::MIN_POSITIVE, D38s12::EPSILON);
476 }
477
478 /// At SCALE = 6 the LSB is 10^-6; EPSILON is still raw 1.
479 #[test]
480 fn epsilon_at_scale_6_is_one_ulp() {
481 type D6 = D38<6>;
482 assert_eq!(D6::EPSILON.to_bits(), 1_i128);
483 assert_eq!(D6::MIN_POSITIVE.to_bits(), 1_i128);
484 }
485
486 // Cross-scale exercises
487
488 /// At SCALE = 6, pi() should equal 3.141593 (rounded half-to-even from
489 /// 3.1415926535...). Expected raw bits: 3_141_593.
490 #[test]
491 fn pi_at_scale_6_is_bit_exact() {
492 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
493 type D6 = D38<6>;
494 assert_eq!(D6::pi().to_bits(), 3_141_593_i128);
495 }
496
497 /// At SCALE = 0, pi() rounds to 3 (first fractional digit is 1, no
498 /// round-up).
499 #[test]
500 fn pi_at_scale_0_is_three() {
501 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
502 type D0 = D38<0>;
503 assert_eq!(D0::pi().to_bits(), 3_i128);
504 }
505
506 /// `D38<37>::pi()` is the canonical pi rounded half-to-even to 37
507 /// fractional digits. The 75-digit Int256 reference is rescaled
508 /// down to 37 digits; the result is bit-identical to the
509 /// hand-tabulated constant.
510 #[test]
511 fn pi_at_scale_37_matches_canonical_37_digit_rounding() {
512 type D37 = D38<37>;
513 // pi to 38 digits: 3.14159265358979323846264338327950288420
514 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
515 // keep 37 frac digits; the 38th digit is 0
516 // so half-to-even rounds down — no bump.
517 let expected: i128 = 31_415_926_535_897_932_384_626_433_832_795_028_842;
518 assert_eq!(D37::pi().to_bits(), expected);
519 }
520
521 // `D38<38>` storage range is approximately ±1.70141 (i128::MAX /
522 // 10^38). The three constants whose magnitude exceeds that bound
523 // must panic with a clear "out of storage range" message:
524 //
525 // - pi ≈ 3.14159 > 1.70141 → must panic
526 // - tau ≈ 6.28318 > 1.70141 → must panic
527 // - e ≈ 2.71828 > 1.70141 → must panic
528 //
529 // The three that DO fit must be correctly rounded to 0.5 ULP:
530 //
531 // - half_pi ≈ 1.57079 < 1.70141 → must round to 0.5 ULP
532 // - quarter_pi ≈ 0.78540 < 1.70141 → must round to 0.5 ULP
533 // - golden ≈ 1.61803 < 1.70141 → must round to 0.5 ULP
534
535 #[test]
536 #[should_panic(expected = "out of storage range")]
537 fn pi_at_scale_38_panics_storage_range() {
538 let _ = D38::<38>::pi();
539 }
540
541 #[test]
542 #[should_panic(expected = "out of storage range")]
543 fn tau_at_scale_38_panics_storage_range() {
544 let _ = D38::<38>::tau();
545 }
546
547 #[test]
548 #[should_panic(expected = "out of storage range")]
549 fn e_at_scale_38_panics_storage_range() {
550 let _ = D38::<38>::e();
551 }
552
553 /// `half_pi` / `quarter_pi` / `golden` at `D38<38>` must not panic
554 /// (their magnitudes are inside the type's ±1.7 storage range) and
555 /// each must be correctly rounded to 0.5 ULP (= 1 LSB).
556 #[test]
557 fn fitting_constants_at_scale_38_are_correctly_rounded() {
558 // half_pi to 38 digits: 1.57079632679489661923132169163975144210
559 let expected_half_pi: i128 = 157_079_632_679_489_661_923_132_169_163_975_144_210;
560 let got = D38::<38>::half_pi().to_bits();
561 let diff = (got - expected_half_pi).abs();
562 assert!(diff <= 1, "half_pi: got {got}, expected {expected_half_pi}, diff {diff} > 1 LSB");
563
564 // quarter_pi to 38 digits: 0.78539816339744830961566084581987572105
565 let expected_quarter_pi: i128 = 78_539_816_339_744_830_961_566_084_581_987_572_105;
566 let got = D38::<38>::quarter_pi().to_bits();
567 let diff = (got - expected_quarter_pi).abs();
568 assert!(diff <= 1, "quarter_pi: got {got}, expected {expected_quarter_pi}, diff {diff} > 1 LSB");
569
570 // golden to 38 digits: 1.61803398874989484820458683436563811772
571 let expected_golden: i128 = 161_803_398_874_989_484_820_458_683_436_563_811_772;
572 let got = D38::<38>::golden().to_bits();
573 let diff = (got - expected_golden).abs();
574 assert!(diff <= 1, "golden: got {got}, expected {expected_golden}, diff {diff} > 1 LSB");
575 }
576
577 /// Negative-side rounding: negating pi gives the expected raw bits.
578 #[test]
579 fn neg_pi_round_trip() {
580 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
581 let pi = D38s12::pi();
582 let neg_pi = -pi;
583 assert_eq!(neg_pi.to_bits(), -3_141_592_653_590_i128);
584 }
585
586 // (`rescale_from_ref` boundary tests removed: the rounding logic now
587 // lives in `D38::rescale` / `src/rounding.rs::apply_rounding` and is
588 // covered by the tests in those modules.)
589}