garage44.eu/jeroen/frits

logclone: git clone https://garage44.eu/jeroen/frits.git

cartopolis-agent · 2026-11-26T13:41:25Z

feat(frits): math.pow, exp, ln, log10, hypot and clamp

A theremin turns hand travel into pitch exponentially, so equal movements
are equal intervals; Cartopolis's theremin needs `pow`, which the
prelude lacked. The rest are the ones a port from JavaScript's Math
reaches for next.
crates/frits/src/prelude.rs | 30 ++++++++++++++++++++++++++++++
 crates/frits/src/tests.rs   | 11 +++++++++++
 2 files changed, 41 insertions(+)

diff --git a/crates/frits/src/prelude.rs b/crates/frits/src/prelude.rs
index dd51f90..71fa06e 100644
--- a/crates/frits/src/prelude.rs
+++ b/crates/frits/src/prelude.rs
@@ -40,6 +40,17 @@ module math {
     fn round(x: Float) -> Float
     fn min(a: Float, b: Float) -> Float
     fn max(a: Float, b: Float) -> Float
+    // `x` to the power `y`.
+    fn pow(x: Float, y: Float) -> Float
+    // e to the power `x`.
+    fn exp(x: Float) -> Float
+    // The natural logarithm.
+    fn ln(x: Float) -> Float
+    fn log10(x: Float) -> Float
+    // The length of `(x, y)`: `sqrt(x * x + y * y)`, without overflow.
+    fn hypot(x: Float, y: Float) -> Float
+    // `x` held between `lo` and `hi`.
+    fn clamp(x: Float, lo: Float, hi: Float) -> Float
     fn deg(radians: Float) -> Float
     fn rad(degrees: Float) -> Float
     let pi: Float
@@ -68,6 +79,25 @@ pub fn bind<S>(p: &mut Platform<S>) {
         .bind("math.max", |_, a| {
             Ok(Value::Float(a[0].float().max(a[1].float())))
         })
+        .bind("math.pow", |_, a| {
+            Ok(Value::Float(a[0].float().powf(a[1].float())))
+        })
+        .bind("math.exp", |_, a| f1(a, f64::exp))
+        .bind("math.ln", |_, a| f1(a, f64::ln))
+        .bind("math.log10", |_, a| f1(a, f64::log10))
+        .bind("math.hypot", |_, a| {
+            Ok(Value::Float(a[0].float().hypot(a[1].float())))
+        })
+        .bind("math.clamp", |_, a| {
+            let (x, lo, hi) = (a[0].float(), a[1].float(), a[2].float());
+            Ok(Value::Float(if x < lo {
+                lo
+            } else if x > hi {
+                hi
+            } else {
+                x
+            }))
+        })
         .bind("math.deg", |_, a| f1(a, f64::to_degrees))
         .bind("math.rad", |_, a| f1(a, f64::to_radians))
         .bind("Random.float", |cx, _| {
diff --git a/crates/frits/src/tests.rs b/crates/frits/src/tests.rs
index ac3f314..a91f28f 100644
--- a/crates/frits/src/tests.rs
+++ b/crates/frits/src/tests.rs
@@ -1001,3 +1001,14 @@ fn untrusted_text_is_shown_but_must_be_checked_before_it_acts() {
         assert!(e.iter().any(|e| e.contains(want)), "{src}: {e:?}");
     }
 }
+
+#[test]
+fn more_maths() {
+    let out = said(
+        "script / Out
+        on go() {
+            Out.say(\"{math.pow(2, 10)} {math.exp(0)} {math.ln(math.exp(2)):.3} {math.log10(1000)} {math.hypot(3, 4)} {math.clamp(7, 0, 5)}\")
+        }",
+    );
+    assert_eq!(out, ["1024 1 2.000 3 5 5"]);
+}