Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Expressions: hyperbolic functions sinh, cosh, tanh and their inverses (#9332) (#9334)

Rick Morgansbd1525dcfbfb
diff --git a/crates/doc/src/expr.rs b/crates/doc/src/expr.rs
index 935698e9f4..a60768b068 100644
--- a/crates/doc/src/expr.rs
+++ b/crates/doc/src/expr.rs
@@ -161,6 +161,12 @@ pub const FUNCTIONS: &[(&str, &str)] = &[
("ceil", "ceil(x)"),
("round", "round(x)"),
("exp", "exp(x)"),
+ ("sinh", "sinh(x)"),
+ ("cosh", "cosh(x)"),
+ ("tanh", "tanh(x)"),
+ ("asinh", "asinh(x)"),
+ ("acosh", "acosh(x), x ≥ 1"),
+ ("atanh", "atanh(x), −1 < x < 1"),
("ln", "ln(x)"),
("log", "log(x) (base 10)"),
("log10", "log10(x)"),
@@ -945,6 +951,24 @@ fn call(f: &str, args: &[Value], d: Defaults) -> Result<Value, DocError> {
unit_fn(r * if scale != 0.0 { scale } else { 1.0 }, a)
}
"exp" => Value::num(num(one()?)?.exp()),
+ "sinh" => Value::num(num(one()?)?.sinh()),
+ "cosh" => Value::num(num(one()?)?.cosh()),
+ "tanh" => Value::num(num(one()?)?.tanh()),
+ "asinh" => Value::num(num(one()?)?.asinh()),
+ "acosh" => {
+ let x = num(one()?)?;
+ if !(x >= 1.0) {
+ return Err(DocError::Expr("`acosh` needs a number of at least 1".into()));
+ }
+ Value::num(x.acosh())
+ }
+ "atanh" => {
+ let x = num(one()?)?;
+ if !(x > -1.0 && x < 1.0) {
+ return Err(DocError::Expr("`atanh` needs a number between -1 and 1, both excluded".into()));
+ }
+ Value::num(x.atanh())
+ }
"ln" | "log" | "log10" => {
let x = num(one()?)?;
if x <= 0.0 {
@@ -1330,6 +1354,15 @@ mod tests {
assert!((ev("atan2(1; 1)", Kind::Angle).unwrap() - std::f64::consts::FRAC_PI_4).abs() < 1e-12);
assert!((ev("exp(ln(5))", Kind::Unitless).unwrap() - 5.0).abs() < 1e-12);
assert!((ev("log(1000)", Kind::Unitless).unwrap() - 3.0).abs() < 1e-12);
+ // Hyperbolic functions (#9332): their definitions in exp, identities and inverses.
+ assert!((ev("cosh(1.5) - (exp(1.5) + exp(-1.5)) / 2", Kind::Unitless).unwrap()).abs() < 1e-12);
+ assert!((ev("sinh(1.5) - (exp(1.5) - exp(-1.5)) / 2", Kind::Unitless).unwrap()).abs() < 1e-12);
+ assert!((ev("tanh(0.7) - sinh(0.7) / cosh(0.7)", Kind::Unitless).unwrap()).abs() < 1e-12);
+ assert!((ev("cosh(2) ^ 2 - sinh(2) ^ 2", Kind::Unitless).unwrap() - 1.0).abs() < 1e-12);
+ assert!((ev("asinh(sinh(1.25))", Kind::Unitless).unwrap() - 1.25).abs() < 1e-12);
+ assert!((ev("acosh(cosh(1.25))", Kind::Unitless).unwrap() - 1.25).abs() < 1e-12);
+ assert!((ev("atanh(tanh(0.5))", Kind::Unitless).unwrap() - 0.5).abs() < 1e-12);
+ assert_eq!(ev("cosh(0) + sinh(0) + tanh(0) + asinh(0) + acosh(1) + atanh(0)", Kind::Unitless).unwrap(), 1.0);
assert_eq!(ev("floor(2.7) + ceil(0.2) + round(1.5) + abs(-1)", Kind::Unitless).unwrap(), 6.0);
assert!((ev("1 rad", Kind::Angle).unwrap() - 1.0).abs() < 1e-12);
assert!((ev("acos(0)", Kind::Angle).unwrap() - std::f64::consts::FRAC_PI_2).abs() < 1e-12);
@@ -1339,6 +1372,22 @@ mod tests {
assert_eq!(ev("width^2 / width", Kind::Length).unwrap(), 40.0);
}
+ /// The hyperbolic functions (#9332) take a unit-less number, and the inverses refuse what lies
+ /// outside their domain, as `asin` and `ln` do.
+ #[test]
+ fn hyperbolic_functions_refuse_units_and_numbers_outside_their_domain() {
+ for (e, says) in [
+ ("cosh(2 mm)", "`cosh` needs a unit-less number"),
+ ("tanh(30 deg)", "`tanh` needs a unit-less number"),
+ ("acosh(0.5)", "`acosh` needs a number of at least 1"),
+ ("atanh(1)", "`atanh` needs a number between -1 and 1"),
+ ("atanh(-1.5)", "`atanh` needs a number between -1 and 1"),
+ ] {
+ let err = ev(e, Kind::Unitless).map(|v| v.to_string()).unwrap_or_else(|e| e.to_string());
+ assert!(err.contains(says), "{e}: {err}");
+ }
+ }
+
/// A count from a length (`(L - 20) / 210`, the 210 read as unit-less) names the fix (#5901).
#[test]
fn a_length_where_a_count_is_expected_says_to_divide_by_a_length() {
diff --git a/crates/doc/src/tests.rs b/crates/doc/src/tests.rs
index e277576318..4a126f4831 100644
--- a/crates/doc/src/tests.rs
+++ b/crates/doc/src/tests.rs
@@ -486,6 +486,8 @@ fn hostile_expressions_never_panic() {
"deg",
"x",
"sqrt",
+ "cosh",
+ "atanh",
"pow",
"min",
"max",
diff --git a/docs/book/src/guide/equation-curves.md b/docs/book/src/guide/equation-curves.md
index f49cf8a161..ebcbf4d93c 100644
--- a/docs/book/src/guide/equation-curves.md
+++ b/docs/book/src/guide/equation-curves.md
@@ -67,6 +67,13 @@ middle (`2 * t`) grows while the clock runs, so the pen moves outwards as it goe
A number without a unit is in your design's unit (millimetres unless you changed it).
+## Other functions
+
+Besides `sin`, `cos` and `tan`, a rule can use `sqrt`, `exp`, `ln`, `pow`, `abs`, `min`, `max`
+and the hyperbolic functions `sinh`, `cosh` and `tanh` (and their inverses `asinh`, `acosh`,
+`atanh`), which take a plain number. A hanging chain is `20 * cosh(x / 20)` in **y(x)**, and the
+flare of a horn is often written with `cosh` and `sinh`.
+
## Using parameters: a gear tooth
Rules can use the design's [parameters](parameters.md). Here a parameter `r` (20 mm) is the size
diff --git a/docs/book/src/guide/parameters.md b/docs/book/src/guide/parameters.md
index 20f72e6702..955737c983 100644
--- a/docs/book/src/guide/parameters.md
+++ b/docs/book/src/guide/parameters.md
@@ -60,8 +60,9 @@ or **Alt/Option** for 0.1. The mouse wheel over the focused field does the same,
and the preview follows each step. An expression such as `width / 2` is left as typed.
Expressions are unit-aware: `2 * width + 5 mm`, `angle / 2`, `sqrt(area)`, `1 in + 3 mm`, using
-`+ - * / ^`, parentheses, `sin cos tan asin acos atan atan2 sqrt abs min max floor ceil round mod
-trunc` and unit conversion. A number and the unit after it are one value: `10 mm / 2 mm` is 5
+`+ - * / ^ %`, parentheses, `sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh sqrt
+abs sign min max floor ceil round trunc mod exp ln log log10 pow hypot` (`log` is base 10) and unit
+conversion. A number and the unit after it are one value: `10 mm / 2 mm` is 5
and `90 deg / 1 rad` is π/2, and a fraction of numbers before a unit is one value too (`1/2 in`
is half an inch; after a `/` only written without spaces: `1 in / 1/2 in` is 2, while
`width / 10 / 2 mm` divides by 10 and then by 2 mm). A decimal comma works too, outside parentheses: `2,5 mm` is
diff --git a/skills/solidcraft-checking/SKILL.md b/skills/solidcraft-checking/SKILL.md
index be46019096..18b5da9c9e 100644
--- a/skills/solidcraft-checking/SKILL.md
+++ b/skills/solidcraft-checking/SKILL.md
@@ -35,7 +35,7 @@ In `$SKILL/scripts/`:
3. **Circumscribed polygons.** `sketch.polygon.circumscribed` with a size dimension and one vertical side still has 1 dof: its corners can alternate in and out. `sketch.constraint.polygon` makes it regular; `scb.hexagon` adds it.
4. **Boolean retries.** A `Boolean retry: ...` warning means the result may be wrong (see "Boolean warning" in the `solidcraft-modeling` reference), so check the volume of that state. `temporary coincident-face separation` comes from faces that coincide. A tapered tool whose start or end lies on a face of a plain block, or a straight cylinder joined onto the floor of a filleted, shelled box, does not log it; a `tool displacement` warning shows up when a join and a cut share one location, as in a standoff built as a joined cylinder with a cut pilot hole, patterned together. That case reports the warning but its volume agrees with the analytic growth, and a single ring profile removes the warning. Starting cutters about 1 mm past the faces they pierce and sinking joins about 1 mm into the body remain good practice on a single feature. Check the volume of every state either way.
5. **Measurement accuracy.** The `solidcraft-modeling` reference gives how far measured volumes stray from computed ones. Centre of mass and bounding box come out up to about 5e-5 of the part size off. Set test tolerances as the template says.
-6. **Expressions.** They have `+ - * / ^ %`, `sin cos tan asin acos atan atan2 sqrt abs min max floor ceil round mod trunc exp ln log log10 pow sign hypot` (`log` is base 10), `pi` and units; no `if` and no comparisons. Build steps from `floor`, `mod`, `min`, `max` and `sign`, which gives 1 at 0. `asin acos atan atan2` return angles, so a unitless parameter cannot hold them.
+6. **Expressions.** They have `+ - * / ^ %`, `sin cos tan asin acos atan atan2 sinh cosh tanh asinh acosh atanh sqrt abs min max floor ceil round mod trunc exp ln log log10 pow sign hypot` (`log` is base 10; the hyperbolic ones take and give unit-less numbers), `pi` and units; no `if` and no comparisons. Build steps from `floor`, `mod`, `min`, `max` and `sign`, which gives 1 at 0. `asin acos atan atan2` return angles, so a unitless parameter cannot hold them.
7. **Snapping.** A point created exactly on an existing point or the origin becomes coincident with it (`connect` defaults to true).
8. **Repeated body names.** A repeated `body_name` is renamed (`Body1`) without an error, so `targets` reach only the first body.