Skip to content

Commit e0dae49

Browse files
authored
Merge pull request #119 from flyingrobots/echo/scalar-trait-115
feat(math): add Scalar trait (ops supertraits + sin/cos)
2 parents 4a319f5 + 699cfdb commit e0dae49

File tree

5 files changed

+105
-0
lines changed

5 files changed

+105
-0
lines changed

crates/rmg-core/src/math/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::f32::consts::TAU;
88
mod mat4;
99
mod prng;
1010
mod quat;
11+
mod scalar;
1112
mod vec3;
1213

1314
#[doc(inline)]
@@ -17,6 +18,8 @@ pub use prng::Prng;
1718
#[doc(inline)]
1819
pub use quat::Quat;
1920
#[doc(inline)]
21+
pub use scalar::Scalar;
22+
#[doc(inline)]
2023
pub use vec3::Vec3;
2124

2225
/// Degeneracy threshold used by math routines to detect near-zero magnitudes.

crates/rmg-core/src/math/scalar.rs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
//! Deterministic scalar arithmetic abstraction for Echo math.
2+
//!
3+
//! This trait provides a minimal, platform-stable surface for numeric code in
4+
//! Echo to depend on without committing to a single concrete representation.
5+
//! Implementations must uphold determinism across supported platforms and are
6+
//! expected to encapsulate representation-specific policies (e.g., float32
7+
//! canonicalization or fixed-point scaling).
8+
//!
9+
//! Scope (Issue #115):
10+
//! - Core arithmetic: add, sub, mul, div, neg.
11+
//! - Core transcendentals: sin, cos (angles in radians).
12+
//!
13+
//! Out of scope for this commit:
14+
//! - Canonicalization of `-0.0` to `+0.0` and subnormal flushing (to be handled
15+
//! by concrete float wrappers in a follow-up task).
16+
//! - Lookup-table or polynomial-backed trig implementations (tracked separately;
17+
//! this trait only declares the API).
18+
//! - Concrete backends: `F32Scalar` and `DFix64` will implement this trait in
19+
//! subsequent changes.
20+
//!
21+
//! Determinism contract:
22+
//! - Operations must be pure and total for all valid inputs of the
23+
//! implementation’s domain.
24+
//! - For floating-point backends, implementations are responsible for any
25+
//! canonicalization/flush semantics required by Echo’s determinism policy.
26+
//! - Trigonometric functions interpret arguments as radians and must be
27+
//! consistent across platforms for identical inputs (e.g., via LUT/polynomial
28+
//! in later work).
29+
30+
use core::ops::{Add, Div, Mul, Neg, Sub};
31+
32+
/// Deterministic scalar arithmetic and basic transcendentals.
33+
///
34+
/// This trait abstracts the numeric core used by Echo so that engine code can
35+
/// be written generically and later bound to either a deterministic float32
36+
/// wrapper (`F32Scalar`) or a fixed-point implementation (`DFix64`). Arithmetic
37+
/// operators are required via the standard operator traits for ergonomic use of
38+
/// `+`, `-`, `*`, `/`, and unary `-` in generic code.
39+
pub trait Scalar:
40+
Copy
41+
+ core::fmt::Debug
42+
+ PartialEq
43+
+ Send
44+
+ Sync
45+
+ 'static
46+
+ Add<Output = Self>
47+
+ Sub<Output = Self>
48+
+ Mul<Output = Self>
49+
+ Div<Output = Self>
50+
+ Neg<Output = Self>
51+
{
52+
/// Returns the additive identity (zero).
53+
fn zero() -> Self;
54+
55+
/// Returns the multiplicative identity (one).
56+
fn one() -> Self;
57+
58+
/// Returns the sine of `self` (radians) under deterministic semantics.
59+
fn sin(self) -> Self;
60+
61+
/// Returns the cosine of `self` (radians) under deterministic semantics.
62+
fn cos(self) -> Self;
63+
64+
/// Returns both sine and cosine of `self` (radians).
65+
///
66+
/// Default implementation computes `sin` and `cos` separately; concrete
67+
/// implementations may override for efficiency or shared range reduction.
68+
fn sin_cos(self) -> (Self, Self) {
69+
(self.sin(), self.cos())
70+
}
71+
72+
/// Converts from `f32` into this scalar type.
73+
///
74+
/// This is intended for boundary crossings (e.g., deserializing payloads)
75+
/// and test scaffolding. Implementations must apply any necessary
76+
/// canonicalization required by Echo’s determinism policy.
77+
fn from_f32(value: f32) -> Self;
78+
79+
/// Converts this scalar value to `f32` for interop and diagnostics.
80+
///
81+
/// Implementations should define rounding policy precisely (e.g., ties to
82+
/// even) and ensure platform-stable results.
83+
fn to_f32(self) -> f32;
84+
}

docs/decision-log.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
| Date | Context | Decision | Rationale | Consequence |
66
| ---- | ------- | -------- | --------- | ----------- |
7+
| 2025-11-03 | Scalar foundation | Add `rmg-core::math::Scalar` trait (operator supertraits + sin/cos) | Arithmetic via `Add/Sub/Mul/Div/Neg` supertraits for ergonomic `+ - * /`; `sin/cos` methods declared; canonicalization/LUTs deferred | Unblocks F32Scalar and DFix64 implementations; math code can target a stable trait |
78
| 2025-10-23 | Repo reset | Adopt pnpm + TS skeleton | Monorepo scaffolding for Echo | Phase 0 tasks established |
89
| 2025-10-24 | Branch tree spec | Integrate roaring bitmaps and chunk epochs | Deterministic merges & diffs | Snapshot policy updated |
910
| 2025-10-24 | Codex’s Baby spec | Event envelopes, temporal bridge integration | Align with causality layer | Security envelopes + inspector updates |

docs/echo-total.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,14 @@ This is Codex’s working map for building Echo. Update it relentlessly—each s
260260

261261
## Today’s Intent
262262

263+
> 2025-11-03 — Issue #115: Scalar trait scaffold
264+
265+
- Added `rmg-core::math::scalar::Scalar` trait declaring deterministic scalar operations.
266+
- Arithmetic is required via operator supertraits: `Add/Sub/Mul/Div/Neg` with `Output = Self` for ergonomic `+ - * / -` use in generics.
267+
- Explicit APIs included: `zero`, `one`, `sin`, `cos`, `sin_cos` (default), `from_f32`, `to_f32`.
268+
- No implementations yet (F32Scalar/DFix64 follow); no canonicalization or LUTs in this change.
269+
- Exported via `rmg-core::math::Scalar` for consumers.
270+
263271
> 2025-11-02 — PR-12: benches updates (CI docs guard)
264272
265273
- Dependency policy: pin `blake3` in `rmg-benches` to `1.8.2` (no wildcard).
@@ -598,6 +606,7 @@ Remember: every entry here shrinks temporal drift between Codices. Leave breadcr
598606

599607
| Date | Context | Decision | Rationale | Consequence |
600608
| ---- | ------- | -------- | --------- | ----------- |
609+
| 2025-11-03 | Scalar foundation | Add `rmg-core::math::Scalar` trait (operator supertraits + sin/cos) | Arithmetic via `Add/Sub/Mul/Div/Neg` supertraits for ergonomic `+ - * /`; `sin/cos` methods declared; canonicalization/LUTs deferred | Unblocks F32Scalar and DFix64 implementations; math code can target a stable trait |
601610
| 2025-10-23 | Repo reset | Adopt pnpm + TS skeleton | Monorepo scaffolding for Echo | Phase 0 tasks established |
602611
| 2025-10-24 | Branch tree spec | Integrate roaring bitmaps and chunk epochs | Deterministic merges & diffs | Snapshot policy updated |
603612
| 2025-10-24 | Codex’s Baby spec | Event envelopes, temporal bridge integration | Align with causality layer | Security envelopes + inspector updates |

docs/execution-plan.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ This is Codex’s working map for building Echo. Update it relentlessly—each s
3333

3434
## Today’s Intent
3535

36+
> 2025-11-03 — Issue #115: Scalar trait scaffold
37+
38+
- Added `rmg-core::math::scalar::Scalar` trait declaring deterministic scalar operations.
39+
- Arithmetic is required via operator supertraits: `Add/Sub/Mul/Div/Neg` with `Output = Self` for ergonomic `+ - * / -` use in generics.
40+
- Explicit APIs included: `zero`, `one`, `sin`, `cos`, `sin_cos` (default), `from_f32`, `to_f32`.
41+
- No implementations yet (F32Scalar/DFix64 follow); no canonicalization or LUTs in this change.
42+
- Exported via `rmg-core::math::Scalar` for consumers.
43+
3644
> 2025-11-02 — PR-12: benches updates (CI docs guard)
3745
3846
- Dependency policy: pin `blake3` in `rmg-benches` to `1.8.2` (no wildcard).

0 commit comments

Comments
 (0)