Skip to content

Commit f1d32aa

Browse files
Jacob Zhongclaude
andcommitted
Add CachedCBig, the cache-backed variant of CBig
CachedCBig is the complex twin of CachedFBig: it wraps a CBig plus a shared Rc<RefCell<ConstCache>> handle and threads it through the complex transcendentals (ln/exp/powf/sin/cos/tan/sin_cos/asin/acos/atan/arg), reusing ConstCache unchanged — CBig's transcendentals are built entirely from real FBig ops, so there are no complex-specific constants to cache. It mirrors CBig's full always-on trait surface (formatting, ordering, conversions, the binary operators incl. cross-type ops against both CBig and FBig, Neg/Inverse, Sum/Product), is !Send + !Sync (so CBig stays Send + Sync and static_cbig! is unaffected), and ships under the dashu::FastComplex alias. The forward-compatible cache: Option<&mut ConstCache> hook (convenience layer passes None, cached layer passes Some) meant no Context signature change was needed. Two intentional divergences from CBig: into_parts returns (CachedFBig, CachedFBig) sharing the handle (vs CBig's (FBig, FBig)), and there are no ZERO/ONE/I constants (Rc isn't const-constructible). Third-party traits (serde/num-traits/num-order/num-complex/rand) are intentionally not mirrored — reach them via .as_cbig(). Operator surface is macro-driven, mirroring fbig_cached_ops.rs. Tests live as #[cfg(test)] mod tests blocks in the two source files (split by what each owns), matching the CachedFBig layout. Also updates AGENTS.md (cached-wrappers rule now covers CachedCBig + FastComplex), TODO-v05.md (pulled forward from §3.4 into the Phase 3 "implemented" status), the complex changelog, and the guide's cached page plus its SUMMARY title. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent a5f238e commit f1d32aa

9 files changed

Lines changed: 1162 additions & 11 deletions

File tree

AGENTS.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ Keep the `## Unreleased` section updated as you go.
8282

8383
## Cached wrappers (`CachedFBig`, `CachedCBig`)
8484

85-
**`CachedFBig` is a drop-in replacement for `FBig`, and `CachedCBig` for `CBig`.** Each cached wrapper must mirror the full public API and trait surface of its non-cached counterpart, delegating every impl to the inner value. **Whenever you add or change a trait impl on `FBig` or `CBig`, mirror it on `CachedFBig` / `CachedCBig` in the same change** — otherwise the `FastReal` / `FastDecimal` aliases regress: code that compiles with `FBig` must compile unchanged with `CachedFBig`. The only intentional divergences are that the cached type's transcendental ops thread the shared `ConstCache`, it is `!Send + !Sync`, construction takes a cache handle, and **third-party crate traits (serde, num-traits, num-order, rand, zeroize, postgres/diesel) are intentionally not mirrored** — reach them through `.as_fbig()` / `.as_cbig()`. (`CachedCBig` is planned but not yet implemented — this rule applies once it lands.)
85+
**`CachedFBig` is a drop-in replacement for `FBig`, and `CachedCBig` for `CBig`.** Each cached wrapper must mirror the full public API and trait surface of its non-cached counterpart, delegating every impl to the inner value. **Whenever you add or change a trait impl on `FBig` or `CBig`, mirror it on `CachedFBig` / `CachedCBig` in the same change** — otherwise the `FastReal` / `FastDecimal` / `FastComplex` aliases regress: code that compiles with `FBig`/`CBig` must compile unchanged with `CachedFBig`/`CachedCBig`. The only intentional divergences are that the cached type's transcendental ops thread the shared `ConstCache`, it is `!Send + !Sync`, construction takes a cache handle, `CachedCBig::into_parts` returns `(CachedFBig, CachedFBig)` sharing the handle (not `CBig`'s `(FBig, FBig)`), and **third-party crate traits (serde, num-traits, num-order, rand, zeroize, postgres/diesel) are intentionally not mirrored** — reach them through `.as_fbig()` / `.as_cbig()`.
8686

8787
## dashu-int internals
8888

TODO-v05.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,16 @@ the primary `Real`/`Decimal`.
207207
> `Context`). Verified with proptest identities + self-oracles, deterministic Annex-G vectors, and a
208208
> manual `rug::Complex`/MPC oracle in `fuzz/`. `NumHash` mirrors `num-complex`'s `Complex<f64>`
209209
> algebraic hash (verified against the `num-order` reference). See `complex/CHANGELOG.md` (0.5.0).
210+
>
211+
> **`CachedCBig`** — the cache-backed variant mirroring `CachedFBig` (originally listed in §3.4) has
212+
> been implemented and pulled into 0.5. It wraps a `CBig` plus a shared `Rc<RefCell<ConstCache>>`
213+
> handle, threads it through the complex transcendentals (`ln`/`exp`/`powf`/`sin`/`cos`/`tan`/`sin_cos`/
214+
> `asin`/`acos`/`atan`/`arg`) — reusing `ConstCache` unchanged, since `CBig`'s transcendentals are built
215+
> entirely from real `FBig` ops — mirrors `CBig`'s always-on trait surface (it is `!Send + !Sync`, so
216+
> `CBig` stays `Send + Sync` and `static_cbig!` is unaffected), and ships under the `dashu::FastComplex`
217+
> alias. The forward-compatible `cache: Option<&mut ConstCache>` hook (convenience layer passes `None`,
218+
> cached layer passes `Some`) meant no `Context` signature change was needed. See `complex/CHANGELOG.md`,
219+
> `complex/src/cbig_cached.rs`, and `guide/src/cached.md`.
210220
211221
**Goal:** a new crate `dashu-cmplx` (dir `complex/`) providing an arbitrary-precision complex type
212222
`CBig`, targeting GNU MPC parity for "common functionalities." It composes two parts (`re`, `im`)
@@ -281,14 +291,6 @@ removed). All additive — safe as point releases under 0.5.x.
281291
parts).
282292
- **A `ComplexFloat`-style trait** unifying `FBig` and `CBig` (sealed, for generic real/complex code).
283293
- **Ball arithmetic** (the `mpcb_t` analogue — interval/uncertainty complex).
284-
- **`CachedCBig`** — a cache-backed variant mirroring `CachedFBig`. Its structure is settled (so 0.5
285-
is forward-compatible): it wraps a `CBig` plus a shared `Rc<RefCell<dashu_float::ConstCache>>`
286-
handle, reusing `ConstCache` unchanged from `dashu-float` (there are no complex-specific constants
287-
to cache — `CBig`'s transcendentals are built entirely from real `FBig` ops). `CachedCBig` is
288-
`!Send + !Sync` while `CBig` stays `Send + Sync` (so `static_cbig!` produces `CBig`). **This is why
289-
0.5 already threads `cache: Option<&mut ConstCache>` through the transcendental `Context` ops:** the
290-
convenience layer passes `None`, `CachedCBig` will pass `Some(&mut cache)`, so adding the cached
291-
variant needs no signature change.
292294
- **Expose ownership-aware kernel functions from `dashu-float`**`dashu-float`'s `add.rs` already
293295
has `add_val_val` / `add_val_ref` / `add_ref_val` / `add_ref_ref` kernel functions that consume
294296
owned `FBig`/`Repr` when available (avoiding unnecessary clones at the convenience layer). These are
@@ -391,8 +393,9 @@ removed). All additive — safe as point releases under 0.5.x.
391393

392394
- `dashu-python` remains excluded and out of the release critical path (per `AGENTS.md`).
393395
- All `dashu-cmplx` follow-ups (complex hyperbolics, `fma`, `rootofunity`, `agm`, Ziv correct
394-
rounding, `CBig` serde/rkyv/zeroize, `num_complex` interop, `CachedCBig`, ball arithmetic,
396+
rounding, `CBig` serde/rkyv/zeroize, `num_complex` interop, ball arithmetic,
395397
`CRound` independent re/im rounding, vector ops) — see §3.4 for the full consolidated list.
398+
(`CachedCBig`, originally in this list, has been implemented — see the Phase 3 status note above.)
396399
- The full **C `<tgmath.h>` type-generic math surface** — the complete C standard math library for
397400
*both* real and complex (trig & inverse; hyperbolic & inverse; exp/log family including
398401
`exp2`/`exp10`/`expm1`/`log2`/`log10`/`log1p`; power/root `cbrt`/`hypot`/`pow`/`sqrt`; error & gamma

complex/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@
33
## Unreleased
44

55
### Add
6+
- `CachedCBig` — a cache-backed variant of `CBig` mirroring [`dashu-float`'s `CachedFBig`]. It wraps a
7+
`CBig` plus a shared `Rc<RefCell<ConstCache>>` handle, and threads that handle through the complex
8+
transcendentals (`ln`, `exp`, `sin`/`cos`/`tan`/`sin_cos`, `asin`/`acos`/`atan`, `powf`, `arg`) so the
9+
underlying real constants (π, ln2, ln10, …) are reused and progressively extended across a computation
10+
chain instead of recomputed from scratch. It reuses `ConstCache` unchanged (there are no
11+
complex-specific constants to cache), mirrors `CBig`'s full always-on trait surface
12+
(`Display`/`Debug`/`FromStr`, `Ord`/`PartialOrd`/`AbsOrd`, `From`/`TryFrom` conversions, the binary
13+
operators incl. cross-type ops against both `CBig` and `FBig`, `Neg`/`Inverse`, `Sum`/`Product`),
14+
and is `!Send + !Sync` (so `CBig` itself stays `Send + Sync` and `static_cbig!` is unaffected).
15+
Third-party traits (serde/num-traits/num-order/num-complex/rand) are intentionally not mirrored —
16+
reach them via `.as_cbig()`. `CachedCBig::into_parts` returns `(CachedFBig, CachedFBig)` sharing the
17+
handle (an intentional divergence from `CBig::into_parts`'s `(FBig, FBig)`). The meta-crate gains a
18+
`dashu::FastComplex` alias (the complex twin of `FastReal`/`FastDecimal`).
619
- `core::iter::Sum`/`Product` for `CBig` (and `Sum<&CBig>`/`Product<&CBig>`), folding with the
720
binary `+`/`*` operators. The impls are concrete (`Sum`/`Sum<&CBig>`, `Product`/`Product<&CBig>`),
821
matching the narrowed iter surface used for `FBig`; a correctly-rounded (exact-accumulating) `Sum`

0 commit comments

Comments
 (0)