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 DecimalConstants: 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// Backwards-compat alias for the trait's original name. The 0.3.2
275// shipped name was `DecimalConsts`; renamed to `DecimalConstants`
276// for consistency with the spelled-out `Decimal*` style elsewhere
277// in the crate. The alias keeps existing imports compiling without
278// a SemVer-breaking bump.
279//
280// TODO(0.4): remove this alias.
281#[deprecated(
282 since = "0.3.3",
283 note = "renamed to `DecimalConstants`; the `DecimalConsts` alias will be removed in 0.4"
284)]
285pub use self::DecimalConstants as DecimalConsts;
286
287// Public-to-crate helpers that return each constant's rescaled bits at
288// the caller's target SCALE. Used by the `decl_decimal_consts!` macro
289// to provide DecimalConsts for narrower widths (D9, D18) without
290// duplicating the rescale logic.
291
292pub(crate) fn pi_at_target<const TARGET: u32>() -> i128 {
293 rescale_75_to_target::<TARGET>(PI_RAW, "pi")
294}
295pub(crate) fn tau_at_target<const TARGET: u32>() -> i128 {
296 rescale_75_to_target::<TARGET>(TAU_RAW, "tau")
297}
298pub(crate) fn half_pi_at_target<const TARGET: u32>() -> i128 {
299 rescale_75_to_target::<TARGET>(HALF_PI_RAW, "half_pi")
300}
301pub(crate) fn quarter_pi_at_target<const TARGET: u32>() -> i128 {
302 rescale_75_to_target::<TARGET>(QUARTER_PI_RAW, "quarter_pi")
303}
304pub(crate) fn golden_at_target<const TARGET: u32>() -> i128 {
305 rescale_75_to_target::<TARGET>(GOLDEN_RAW, "golden")
306}
307pub(crate) fn e_at_target<const TARGET: u32>() -> i128 {
308 rescale_75_to_target::<TARGET>(E_RAW, "e")
309}
310
311// Mode-aware variants — used by the `*_with(mode)` constant methods.
312
313pub(crate) fn pi_at_target_with<const TARGET: u32>(
314 mode: crate::rounding::RoundingMode,
315) -> i128 {
316 rescale_75_to_target_with::<TARGET>(PI_RAW, "pi", mode)
317}
318pub(crate) fn tau_at_target_with<const TARGET: u32>(
319 mode: crate::rounding::RoundingMode,
320) -> i128 {
321 rescale_75_to_target_with::<TARGET>(TAU_RAW, "tau", mode)
322}
323pub(crate) fn half_pi_at_target_with<const TARGET: u32>(
324 mode: crate::rounding::RoundingMode,
325) -> i128 {
326 rescale_75_to_target_with::<TARGET>(HALF_PI_RAW, "half_pi", mode)
327}
328pub(crate) fn quarter_pi_at_target_with<const TARGET: u32>(
329 mode: crate::rounding::RoundingMode,
330) -> i128 {
331 rescale_75_to_target_with::<TARGET>(QUARTER_PI_RAW, "quarter_pi", mode)
332}
333pub(crate) fn golden_at_target_with<const TARGET: u32>(
334 mode: crate::rounding::RoundingMode,
335) -> i128 {
336 rescale_75_to_target_with::<TARGET>(GOLDEN_RAW, "golden", mode)
337}
338pub(crate) fn e_at_target_with<const TARGET: u32>(
339 mode: crate::rounding::RoundingMode,
340) -> i128 {
341 rescale_75_to_target_with::<TARGET>(E_RAW, "e", mode)
342}
343
344// The `DecimalConsts` impl for `D38<SCALE>` is emitted by the
345// `decl_decimal_consts!` macro — the same macro D9 / D18 / D76+ use.
346// It expands to `Self(pi_at_target::<SCALE>())` etc.; each
347// `*_at_target` helper above rescales the 75-digit Int256 reference
348// down to the caller's `SCALE` via half-to-even and narrows to i128
349// (or panics with a clear message if the constant's magnitude
350// exceeds the storage range at that scale).
351crate::macros::consts::decl_decimal_consts!(D38, i128);
352
353// EPSILON / MIN_POSITIVE for every width are now emitted by
354// `decl_decimal_basics!`. The D38-specific inherent impl that used
355// to live here has been removed in favour of the macro-emitted ones
356// so D9 / D18 / D38 / D56 / D76 / D114 / D153 / D230 / D307 / D461 /
357// D615 / D923 / D1231 all share the same EPSILON / MIN_POSITIVE
358// surface.
359
360#[cfg(test)]
361mod tests {
362 use super::*;
363 use crate::core_type::D38s12;
364
365 // Bit-exact assertions at SCALE = 12.
366 //
367 // At SCALE = 12 each constant is the 37-digit raw integer divided by
368 // 10^23, rounded half-to-even.
369
370 /// pi at SCALE=12: raw / 10^23.
371 /// Truncated 13 digits: 3_141_592_653_589.
372 /// 14th digit is 7 (from position 14 of the raw) -> round up.
373 /// Expected: 3_141_592_653_590.
374 #[test]
375 fn pi_is_bit_exact_at_scale_12() {
376 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
377 assert_eq!(D38s12::pi().to_bits(), 3_141_592_653_590_i128);
378 }
379
380 /// tau at SCALE=12: raw / 10^23.
381 /// Truncated 13 digits: 6_283_185_307_179.
382 /// 14th digit is 5 -> round up. Expected: 6_283_185_307_180.
383 #[test]
384 fn tau_is_bit_exact_at_scale_12() {
385 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
386 assert_eq!(D38s12::tau().to_bits(), 6_283_185_307_180_i128);
387 }
388
389 /// half_pi at SCALE=12: raw / 10^23.
390 /// Truncated 13 digits: 1_570_796_326_794.
391 /// 14th digit is 8 -> round up. Expected: 1_570_796_326_795.
392 #[test]
393 fn half_pi_is_bit_exact_at_scale_12() {
394 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
395 assert_eq!(D38s12::half_pi().to_bits(), 1_570_796_326_795_i128);
396 }
397
398 /// quarter_pi at SCALE=12: raw / 10^23.
399 /// Truncated 12 digits: 785_398_163_397.
400 /// 13th digit is 4 -> no round-up. Expected: 785_398_163_397.
401 #[test]
402 fn quarter_pi_is_bit_exact_at_scale_12() {
403 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
404 assert_eq!(D38s12::quarter_pi().to_bits(), 785_398_163_397_i128);
405 }
406
407 /// e at SCALE=12: raw / 10^23.
408 /// Truncated 13 digits: 2_718_281_828_459.
409 /// 14th digit is 0 -> no round-up. Expected: 2_718_281_828_459.
410 #[test]
411 fn e_is_bit_exact_at_scale_12() {
412 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
413 assert_eq!(D38s12::e().to_bits(), 2_718_281_828_459_i128);
414 }
415
416 /// golden at SCALE=12: raw / 10^23.
417 /// Truncated 13 digits: 1_618_033_988_749.
418 /// 14th digit is 8 -> round up. Expected: 1_618_033_988_750.
419 #[test]
420 fn golden_is_bit_exact_at_scale_12() {
421 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
422 assert_eq!(D38s12::golden().to_bits(), 1_618_033_988_750_i128);
423 }
424
425 // Closeness checks against core::f64::consts.
426 // These verify that the correct reference digits were selected; the
427 // bit-exact tests above are the primary acceptance criteria.
428
429 /// pi() converted to f64 is within 1e-11 of `core::f64::consts::PI`.
430 /// At SCALE=12, 1 LSB = 1e-12, so 1e-11 covers rescale rounding plus
431 /// the f64 conversion step.
432 #[test]
433 fn pi_close_to_f64_pi() {
434 let diff = (D38s12::pi().to_f64() - core::f64::consts::PI).abs();
435 assert!(diff < 1e-11, "pi diverges from f64 PI by {diff}");
436 }
437
438 #[test]
439 fn tau_close_to_f64_tau() {
440 let diff = (D38s12::tau().to_f64() - core::f64::consts::TAU).abs();
441 assert!(diff < 1e-11, "tau diverges from f64 TAU by {diff}");
442 }
443
444 #[test]
445 fn half_pi_close_to_f64_frac_pi_2() {
446 let diff =
447 (D38s12::half_pi().to_f64() - core::f64::consts::FRAC_PI_2).abs();
448 assert!(diff < 1e-11, "half_pi diverges from f64 FRAC_PI_2 by {diff}");
449 }
450
451 #[test]
452 fn quarter_pi_close_to_f64_frac_pi_4() {
453 let diff =
454 (D38s12::quarter_pi().to_f64() - core::f64::consts::FRAC_PI_4).abs();
455 assert!(
456 diff < 1e-11,
457 "quarter_pi diverges from f64 FRAC_PI_4 by {diff}"
458 );
459 }
460
461 #[test]
462 fn e_close_to_f64_e() {
463 let diff = (D38s12::e().to_f64() - core::f64::consts::E).abs();
464 assert!(diff < 1e-11, "e diverges from f64 E by {diff}");
465 }
466
467 /// golden() converted to f64 is within 1e-11 of the closed form
468 /// `(1 + sqrt(5)) / 2`. Requires std for `f64::sqrt`.
469 #[cfg(feature = "std")]
470 #[test]
471 fn golden_close_to_closed_form() {
472 let expected = (1.0_f64 + 5.0_f64.sqrt()) / 2.0;
473 let diff = (D38s12::golden().to_f64() - expected).abs();
474 assert!(diff < 1e-11, "golden diverges from closed-form by {diff}");
475 }
476
477 // EPSILON / MIN_POSITIVE
478
479 #[test]
480 fn epsilon_is_one_ulp() {
481 assert_eq!(D38s12::EPSILON.to_bits(), 1_i128);
482 assert!(D38s12::EPSILON > D38s12::ZERO);
483 }
484
485 #[test]
486 fn min_positive_is_one_ulp() {
487 assert_eq!(D38s12::MIN_POSITIVE.to_bits(), 1_i128);
488 assert_eq!(D38s12::MIN_POSITIVE, D38s12::EPSILON);
489 }
490
491 /// At SCALE = 6 the LSB is 10^-6; EPSILON is still raw 1.
492 #[test]
493 fn epsilon_at_scale_6_is_one_ulp() {
494 type D6 = D38<6>;
495 assert_eq!(D6::EPSILON.to_bits(), 1_i128);
496 assert_eq!(D6::MIN_POSITIVE.to_bits(), 1_i128);
497 }
498
499 // Cross-scale exercises
500
501 /// At SCALE = 6, pi() should equal 3.141593 (rounded half-to-even from
502 /// 3.1415926535...). Expected raw bits: 3_141_593.
503 #[test]
504 fn pi_at_scale_6_is_bit_exact() {
505 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
506 type D6 = D38<6>;
507 assert_eq!(D6::pi().to_bits(), 3_141_593_i128);
508 }
509
510 /// At SCALE = 0, pi() rounds to 3 (first fractional digit is 1, no
511 /// round-up).
512 #[test]
513 fn pi_at_scale_0_is_three() {
514 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
515 type D0 = D38<0>;
516 assert_eq!(D0::pi().to_bits(), 3_i128);
517 }
518
519 /// `D38<37>::pi()` is the canonical pi rounded half-to-even to 37
520 /// fractional digits. The 75-digit Int256 reference is rescaled
521 /// down to 37 digits; the result is bit-identical to the
522 /// hand-tabulated constant.
523 #[test]
524 fn pi_at_scale_37_matches_canonical_37_digit_rounding() {
525 type D37 = D38<37>;
526 // pi to 38 digits: 3.14159265358979323846264338327950288420
527 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
528 // keep 37 frac digits; the 38th digit is 0
529 // so half-to-even rounds down — no bump.
530 let expected: i128 = 31_415_926_535_897_932_384_626_433_832_795_028_842;
531 assert_eq!(D37::pi().to_bits(), expected);
532 }
533
534 // `D38<38>` storage range is approximately ±1.70141 (i128::MAX /
535 // 10^38). The three constants whose magnitude exceeds that bound
536 // must panic with a clear "out of storage range" message:
537 //
538 // - pi ≈ 3.14159 > 1.70141 → must panic
539 // - tau ≈ 6.28318 > 1.70141 → must panic
540 // - e ≈ 2.71828 > 1.70141 → must panic
541 //
542 // The three that DO fit must be correctly rounded to 0.5 ULP:
543 //
544 // - half_pi ≈ 1.57079 < 1.70141 → must round to 0.5 ULP
545 // - quarter_pi ≈ 0.78540 < 1.70141 → must round to 0.5 ULP
546 // - golden ≈ 1.61803 < 1.70141 → must round to 0.5 ULP
547
548 #[test]
549 #[should_panic(expected = "out of storage range")]
550 fn pi_at_scale_38_panics_storage_range() {
551 let _ = D38::<38>::pi();
552 }
553
554 #[test]
555 #[should_panic(expected = "out of storage range")]
556 fn tau_at_scale_38_panics_storage_range() {
557 let _ = D38::<38>::tau();
558 }
559
560 #[test]
561 #[should_panic(expected = "out of storage range")]
562 fn e_at_scale_38_panics_storage_range() {
563 let _ = D38::<38>::e();
564 }
565
566 /// `half_pi` / `quarter_pi` / `golden` at `D38<38>` must not panic
567 /// (their magnitudes are inside the type's ±1.7 storage range) and
568 /// each must be correctly rounded to 0.5 ULP (= 1 LSB).
569 #[test]
570 fn fitting_constants_at_scale_38_are_correctly_rounded() {
571 // half_pi to 38 digits: 1.57079632679489661923132169163975144210
572 let expected_half_pi: i128 = 157_079_632_679_489_661_923_132_169_163_975_144_210;
573 let got = D38::<38>::half_pi().to_bits();
574 let diff = (got - expected_half_pi).abs();
575 assert!(diff <= 1, "half_pi: got {got}, expected {expected_half_pi}, diff {diff} > 1 LSB");
576
577 // quarter_pi to 38 digits: 0.78539816339744830961566084581987572105
578 let expected_quarter_pi: i128 = 78_539_816_339_744_830_961_566_084_581_987_572_105;
579 let got = D38::<38>::quarter_pi().to_bits();
580 let diff = (got - expected_quarter_pi).abs();
581 assert!(diff <= 1, "quarter_pi: got {got}, expected {expected_quarter_pi}, diff {diff} > 1 LSB");
582
583 // golden to 38 digits: 1.61803398874989484820458683436563811772
584 let expected_golden: i128 = 161_803_398_874_989_484_820_458_683_436_563_811_772;
585 let got = D38::<38>::golden().to_bits();
586 let diff = (got - expected_golden).abs();
587 assert!(diff <= 1, "golden: got {got}, expected {expected_golden}, diff {diff} > 1 LSB");
588 }
589
590 /// Negative-side rounding: negating pi gives the expected raw bits.
591 #[test]
592 fn neg_pi_round_trip() {
593 if !crate::rounding::DEFAULT_IS_HALF_TO_EVEN { return; }
594 let pi = D38s12::pi();
595 let neg_pi = -pi;
596 assert_eq!(neg_pi.to_bits(), -3_141_592_653_590_i128);
597 }
598
599 // (`rescale_from_ref` boundary tests removed: the rounding logic now
600 // lives in `D38::rescale` / `src/rounding.rs::apply_rounding` and is
601 // covered by the tests in those modules.)
602}