Add math functions back to the prelude (#6595)

* Add math functions back to the prelude

* Update output

* Update docs
This commit is contained in:
Jonathan Tran
2025-04-30 11:07:05 -04:00
committed by GitHub
parent f1fdf48834
commit ccd5b0272d
111 changed files with 4002 additions and 4335 deletions

View File

@ -1307,7 +1307,7 @@ part001 = startSketchOn(XY)
|> startProfile(at = [0, 0])
|> line(end = [3, 4], tag = $seg01)
|> line(end = [
math::min([segLen(seg01), myVar]),
min([segLen(seg01), myVar]),
-legLen(hypotenuse = segLen(seg01), leg = myVar)
])
"#;
@ -1322,7 +1322,7 @@ part001 = startSketchOn(XY)
|> startProfile(at = [0, 0])
|> line(end = [3, 4], tag = $seg01)
|> line(end = [
math::min([segLen(seg01), myVar]),
min([segLen(seg01), myVar]),
legLen(hypotenuse = segLen(seg01), leg = myVar)
])
"#;
@ -1662,7 +1662,7 @@ shape = layer() |> patternTransform(instances = 10, transform = transform)
#[tokio::test(flavor = "multi_thread")]
async fn test_math_execute_with_functions() {
let ast = r#"myVar = 2 + math::min([100, -1 + legLen(hypotenuse = 5, leg = 3)])"#;
let ast = r#"myVar = 2 + min([100, -1 + legLen(hypotenuse = 5, leg = 3)])"#;
let result = parse_execute(ast).await.unwrap();
assert_eq!(
5.0,

View File

@ -2081,10 +2081,10 @@ o = 3mm / 3
p = 3_ / 4
q = 4inch / 2_
r = math::min([0, 3, 42])
s = math::min([0, 3mm, -42])
t = math::min([100, 3in, 142mm])
u = math::min([3rad, 4in])
r = min([0, 3, 42])
s = min([0, 3mm, -42])
t = min([100, 3in, 142mm])
u = min([3rad, 4in])
"#;
let result = parse_execute(program).await.unwrap();
@ -2137,10 +2137,10 @@ b = 180 / PI * a + 360
#[tokio::test(flavor = "multi_thread")]
async fn cos_coercions() {
let program = r#"
a = math::cos(units::toRadians(30))
a = cos(units::toRadians(30))
b = 3 / a
c = math::cos(30deg)
d = math::cos(30)
c = cos(30deg)
d = cos(30)
"#;
let result = parse_execute(program).await.unwrap();

View File

@ -144,15 +144,15 @@ pub async fn reduce(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
///
/// // Start the decagon sketch at this point.
/// startOfDecagonSketch = startSketchOn(XY)
/// |> startProfile(at = [(math::cos(0)*radius), (math::sin(0) * radius)])
/// |> startProfile(at = [(cos(0)*radius), (sin(0) * radius)])
///
/// // Use a `reduce` to draw the remaining decagon sides.
/// // For each number in the array 1..10, run the given function,
/// // which takes a partially-sketched decagon and adds one more edge to it.
/// fullDecagon = reduce([1..10], initial = startOfDecagonSketch, f = fn(i, partialDecagon) {
/// // Draw one edge of the decagon.
/// x = math::cos(stepAngle * i) * radius
/// y = math::sin(stepAngle * i) * radius
/// x = cos(stepAngle * i) * radius
/// y = sin(stepAngle * i) * radius
/// return line(partialDecagon, end = [x, y])
/// })
///
@ -165,13 +165,13 @@ pub async fn reduce(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
/// fn decagon(radius):
/// stepAngle = ((1/10) * TAU): number(rad)
/// plane = startSketchOn(XY)
/// startOfDecagonSketch = startProfile(plane, at = [(math::cos(0)*radius), (math::sin(0) * radius)])
/// startOfDecagonSketch = startProfile(plane, at = [(cos(0)*radius), (sin(0) * radius)])
///
/// // Here's the reduce part.
/// partialDecagon = startOfDecagonSketch
/// for i in [1..10]:
/// x = math::cos(stepAngle * i) * radius
/// y = math::sin(stepAngle * i) * radius
/// x = cos(stepAngle * i) * radius
/// y = sin(stepAngle * i) * radius
/// partialDecagon = line(partialDecagon, end = [x, y])
/// fullDecagon = partialDecagon // it's now full
/// return fullDecagon

View File

@ -149,84 +149,84 @@ pub(crate) fn std_fn(path: &str, fn_name: &str) -> (crate::std::StdFn, StdFnProp
match (path, fn_name) {
("math", "cos") => (
|e, a| Box::pin(crate::std::math::cos(e, a)),
StdFnProps::default("std::math::cos"),
StdFnProps::default("std::cos"),
),
("math", "sin") => (
|e, a| Box::pin(crate::std::math::sin(e, a)),
StdFnProps::default("std::math::sin"),
StdFnProps::default("std::sin"),
),
("math", "tan") => (
|e, a| Box::pin(crate::std::math::tan(e, a)),
StdFnProps::default("std::math::tan"),
StdFnProps::default("std::tan"),
),
("math", "acos") => (
|e, a| Box::pin(crate::std::math::acos(e, a)),
StdFnProps::default("std::math::acos"),
StdFnProps::default("std::acos"),
),
("math", "asin") => (
|e, a| Box::pin(crate::std::math::asin(e, a)),
StdFnProps::default("std::math::asin"),
StdFnProps::default("std::asin"),
),
("math", "atan") => (
|e, a| Box::pin(crate::std::math::atan(e, a)),
StdFnProps::default("std::math::atan"),
StdFnProps::default("std::atan"),
),
("math", "atan2") => (
|e, a| Box::pin(crate::std::math::atan2(e, a)),
StdFnProps::default("std::math::atan2"),
StdFnProps::default("std::atan2"),
),
("math", "sqrt") => (
|e, a| Box::pin(crate::std::math::sqrt(e, a)),
StdFnProps::default("std::math::sqrt"),
StdFnProps::default("std::sqrt"),
),
("math", "abs") => (
|e, a| Box::pin(crate::std::math::abs(e, a)),
StdFnProps::default("std::math::abs"),
StdFnProps::default("std::abs"),
),
("math", "rem") => (
|e, a| Box::pin(crate::std::math::rem(e, a)),
StdFnProps::default("std::math::rem"),
StdFnProps::default("std::rem"),
),
("math", "round") => (
|e, a| Box::pin(crate::std::math::round(e, a)),
StdFnProps::default("std::math::round"),
StdFnProps::default("std::round"),
),
("math", "floor") => (
|e, a| Box::pin(crate::std::math::floor(e, a)),
StdFnProps::default("std::math::floor"),
StdFnProps::default("std::floor"),
),
("math", "ceil") => (
|e, a| Box::pin(crate::std::math::ceil(e, a)),
StdFnProps::default("std::math::ceil"),
StdFnProps::default("std::ceil"),
),
("math", "min") => (
|e, a| Box::pin(crate::std::math::min(e, a)),
StdFnProps::default("std::math::min"),
StdFnProps::default("std::min"),
),
("math", "max") => (
|e, a| Box::pin(crate::std::math::max(e, a)),
StdFnProps::default("std::math::max"),
StdFnProps::default("std::max"),
),
("math", "pow") => (
|e, a| Box::pin(crate::std::math::pow(e, a)),
StdFnProps::default("std::math::pow"),
StdFnProps::default("std::pow"),
),
("math", "log") => (
|e, a| Box::pin(crate::std::math::log(e, a)),
StdFnProps::default("std::math::log"),
StdFnProps::default("std::log"),
),
("math", "log2") => (
|e, a| Box::pin(crate::std::math::log2(e, a)),
StdFnProps::default("std::math::log2"),
StdFnProps::default("std::log2"),
),
("math", "log10") => (
|e, a| Box::pin(crate::std::math::log10(e, a)),
StdFnProps::default("std::math::log10"),
StdFnProps::default("std::log10"),
),
("math", "ln") => (
|e, a| Box::pin(crate::std::math::ln(e, a)),
StdFnProps::default("std::math::ln"),
StdFnProps::default("std::ln"),
),
("sketch", "circle") => (
|e, a| Box::pin(crate::std::shapes::circle(e, a)),

View File

@ -202,7 +202,7 @@ pub async fn pattern_transform_2d(exec_state: &mut ExecState, args: Args) -> Res
/// // Defines how to modify each layer of the vase.
/// // Each replica is shifted up the Z axis, and has a smoothly-varying radius
/// fn transform(replicaId) {
/// scale = r * math::abs(1 - (t * replicaId)) * (5 + math::cos((replicaId / 8): number(rad)))
/// scale = r * abs(1 - (t * replicaId)) * (5 + cos((replicaId / 8): number(rad)))
/// return {
/// translate = [0, 0, replicaId * 10],
/// scale = [scale, scale, 0],