Skip to content

Commit

Permalink
Deprecate the math constants functions
Browse files Browse the repository at this point in the history
Signed-off-by: Nick Cameron <[email protected]>
  • Loading branch information
nrc committed Jan 24, 2025
1 parent bb7ced5 commit 3e7c648
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 16 deletions.
3 changes: 0 additions & 3 deletions docs/kcl/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ layout: manual
* [`close`](kcl/close)
* [`cm`](kcl/cm)
* [`cos`](kcl/cos)
* [`e`](kcl/e)
* [`extrude`](kcl/extrude)
* [`fillet`](kcl/fillet)
* [`floor`](kcl/floor)
Expand Down Expand Up @@ -84,7 +83,6 @@ layout: manual
* [`patternLinear3d`](kcl/patternLinear3d)
* [`patternTransform`](kcl/patternTransform)
* [`patternTransform2d`](kcl/patternTransform2d)
* [`pi`](kcl/pi)
* [`polar`](kcl/polar)
* [`polygon`](kcl/polygon)
* [`pop`](kcl/pop)
Expand Down Expand Up @@ -116,7 +114,6 @@ layout: manual
* [`tangentialArc`](kcl/tangentialArc)
* [`tangentialArcTo`](kcl/tangentialArcTo)
* [`tangentialArcToRelative`](kcl/tangentialArcToRelative)
* [`tau`](kcl/tau)
* [`toDegrees`](kcl/toDegrees)
* [`toRadians`](kcl/toRadians)
* [`xLine`](kcl/xLine)
Expand Down
4 changes: 2 additions & 2 deletions docs/kcl/reduce.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ assertEqual(sum, 6, 0.00001, "1 + 2 + 3 summed is 6")
// Declare a function that sketches a decagon.
fn decagon(radius) {
// Each side of the decagon is turned this many degrees from the previous angle.
stepAngle = 1 / 10 * tau()
stepAngle = 1 / 10 * TAU

// Start the decagon sketch at this point.
startOfDecagonSketch = startSketchOn('XY')
Expand All @@ -97,7 +97,7 @@ fn decagon(radius) {

/* The `decagon` above is basically like this pseudo-code:
fn decagon(radius):
stepAngle = (1/10) * tau()
stepAngle = (1/10) * TAU
plane = startSketchOn('XY')
startOfDecagonSketch = startProfileAt([(cos(0)*radius), (sin(0) * radius)], plane)
Expand Down
14 changes: 7 additions & 7 deletions docs/kcl/std.json
Original file line number Diff line number Diff line change
Expand Up @@ -69705,7 +69705,7 @@
{
"name": "e",
"summary": "Return the value of Euler’s number `e`.",
"description": "",
"description": "**DEPRECATED** use the constant E",
"tags": [
"math"
],
Expand All @@ -69724,7 +69724,7 @@
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"deprecated": true,
"examples": [
"exampleSketch = startSketchOn(\"XZ\")\n |> startProfileAt([0, 0], %)\n |> angledLine({ angle = 30, length = 2 * e() ^ 2 }, %)\n |> yLineTo(0, %)\n |> close(%)\n\nexample = extrude(10, exampleSketch)"
]
Expand Down Expand Up @@ -143089,7 +143089,7 @@
{
"name": "pi",
"summary": "Return the value of `pi`. Archimedes’ constant (π).",
"description": "",
"description": "**DEPRECATED** use the constant PI",
"tags": [
"math"
],
Expand All @@ -143108,7 +143108,7 @@
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"deprecated": true,
"examples": [
"circumference = 70\n\nexampleSketch = startSketchOn(\"XZ\")\n |> circle({\n center = [0, 0],\n radius = circumference / (2 * pi())\n }, %)\n\nexample = extrude(5, exampleSketch)"
]
Expand Down Expand Up @@ -174308,7 +174308,7 @@
"examples": [
"// This function adds two numbers.\nfn add(a, b) {\n return a + b\n}\n\n// This function adds an array of numbers.\n// It uses the `reduce` function, to call the `add` function on every\n// element of the `arr` parameter. The starting value is 0.\nfn sum(arr) {\n return reduce(arr, 0, add)\n}\n\n/* The above is basically like this pseudo-code:\nfn sum(arr):\n sumSoFar = 0\n for i in arr:\n sumSoFar = add(sumSoFar, i)\n return sumSoFar */\n\n\n// We use `assertEqual` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassertEqual(sum([1, 2, 3]), 6, 0.00001, \"1 + 2 + 3 summed is 6\")",
"// This example works just like the previous example above, but it uses\n// an anonymous `add` function as its parameter, instead of declaring a\n// named function outside.\narr = [1, 2, 3]\nsum = reduce(arr, 0, fn(i, result_so_far) {\n return i + result_so_far\n})\n\n// We use `assertEqual` to check that our `sum` function gives the\n// expected result. It's good to check your work!\nassertEqual(sum, 6, 0.00001, \"1 + 2 + 3 summed is 6\")",
"// Declare a function that sketches a decagon.\nfn decagon(radius) {\n // Each side of the decagon is turned this many degrees from the previous angle.\n stepAngle = 1 / 10 * tau()\n\n // Start the decagon sketch at this point.\n startOfDecagonSketch = startSketchOn('XY')\n |> startProfileAt([cos(0) * radius, sin(0) * radius], %)\n\n // Use a `reduce` to draw the remaining decagon sides.\n // For each number in the array 1..10, run the given function,\n // which takes a partially-sketched decagon and adds one more edge to it.\n fullDecagon = reduce([1..10], startOfDecagonSketch, fn(i, partialDecagon) {\n // Draw one edge of the decagon.\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n return lineTo([x, y], partialDecagon)\n })\n\n return fullDecagon\n}\n\n/* The `decagon` above is basically like this pseudo-code:\nfn decagon(radius):\n stepAngle = (1/10) * tau()\n plane = startSketchOn('XY')\n startOfDecagonSketch = startProfileAt([(cos(0)*radius), (sin(0) * radius)], plane)\n\n // Here's the reduce part.\n partialDecagon = startOfDecagonSketch\n for i in [1..10]:\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n partialDecagon = lineTo([x, y], partialDecagon)\n fullDecagon = partialDecagon // it's now full\n return fullDecagon */\n\n\n// Use the `decagon` function declared above, to sketch a decagon with radius 5.\ndecagon(5.0)\n |> close(%)"
"// Declare a function that sketches a decagon.\nfn decagon(radius) {\n // Each side of the decagon is turned this many degrees from the previous angle.\n stepAngle = 1 / 10 * TAU\n\n // Start the decagon sketch at this point.\n startOfDecagonSketch = startSketchOn('XY')\n |> startProfileAt([cos(0) * radius, sin(0) * radius], %)\n\n // Use a `reduce` to draw the remaining decagon sides.\n // For each number in the array 1..10, run the given function,\n // which takes a partially-sketched decagon and adds one more edge to it.\n fullDecagon = reduce([1..10], startOfDecagonSketch, fn(i, partialDecagon) {\n // Draw one edge of the decagon.\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n return lineTo([x, y], partialDecagon)\n })\n\n return fullDecagon\n}\n\n/* The `decagon` above is basically like this pseudo-code:\nfn decagon(radius):\n stepAngle = (1/10) * TAU\n plane = startSketchOn('XY')\n startOfDecagonSketch = startProfileAt([(cos(0)*radius), (sin(0) * radius)], plane)\n\n // Here's the reduce part.\n partialDecagon = startOfDecagonSketch\n for i in [1..10]:\n x = cos(stepAngle * i) * radius\n y = sin(stepAngle * i) * radius\n partialDecagon = lineTo([x, y], partialDecagon)\n fullDecagon = partialDecagon // it's now full\n return fullDecagon */\n\n\n// Use the `decagon` function declared above, to sketch a decagon with radius 5.\ndecagon(5.0)\n |> close(%)"
]
},
{
Expand Down Expand Up @@ -219519,7 +219519,7 @@
{
"name": "tau",
"summary": "Return the value of `tau`. The full circle constant (τ). Equal to 2π.",
"description": "",
"description": "**DEPRECATED** use the constant TAU",
"tags": [
"math"
],
Expand All @@ -219538,7 +219538,7 @@
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"deprecated": true,
"examples": [
"exampleSketch = startSketchOn(\"XZ\")\n |> startProfileAt([0, 0], %)\n |> angledLine({ angle = 50, length = 10 * tau() }, %)\n |> yLineTo(0, %)\n |> close(%)\n\nexample = extrude(5, exampleSketch)"
]
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/execution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3475,7 +3475,7 @@ let shape = layer() |> patternTransform(10, transform, %)

#[tokio::test(flavor = "multi_thread")]
async fn test_math_execute_with_pi() {
let ast = r#"const myVar = pi() * 2"#;
let ast = r#"const myVar = PI * 2"#;
let (_, _, exec_state) = parse_execute(ast).await.unwrap();
assert_eq!(
std::f64::consts::TAU,
Expand Down
2 changes: 1 addition & 1 deletion src/wasm-lib/kcl/src/parsing/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1680,7 +1680,7 @@ fn validate_path_string(path_string: String, var_name: bool, path_range: SourceR
for s in &segments {
if s.chars().any(|c| !c.is_ascii_alphanumeric() && c != '_') || s.starts_with('_') {
return Err(ErrMode::Cut(
CompilationError::fatal(path_range, "invalid path in import statment.").into(),
CompilationError::fatal(path_range, "invalid path in import statement.").into(),
));
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/wasm-lib/kcl/src/std/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub async fn reduce(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
/// // Declare a function that sketches a decagon.
/// fn decagon(radius) {
/// // Each side of the decagon is turned this many degrees from the previous angle.
/// stepAngle = (1/10) * tau()
/// stepAngle = (1/10) * TAU
///
/// // Start the decagon sketch at this point.
/// startOfDecagonSketch = startSketchOn('XY')
Expand All @@ -164,7 +164,7 @@ pub async fn reduce(exec_state: &mut ExecState, args: Args) -> Result<KclValue,
/// /*
/// The `decagon` above is basically like this pseudo-code:
/// fn decagon(radius):
/// stepAngle = (1/10) * tau()
/// stepAngle = (1/10) * TAU
/// plane = startSketchOn('XY')
/// startOfDecagonSketch = startProfileAt([(cos(0)*radius), (sin(0) * radius)], plane)
///
Expand Down
9 changes: 9 additions & 0 deletions src/wasm-lib/kcl/src/std/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ pub async fn pi(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl

/// Return the value of `pi`. Archimedes’ constant (π).
///
/// **DEPRECATED** use the constant PI
///
/// ```no_run
/// circumference = 70
///
Expand All @@ -132,6 +134,7 @@ pub async fn pi(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
#[stdlib {
name = "pi",
tags = ["math"],
deprecated = true,
}]
fn inner_pi() -> Result<f64, KclError> {
Ok(std::f64::consts::PI)
Expand Down Expand Up @@ -669,6 +672,8 @@ pub async fn e(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclE

/// Return the value of Euler’s number `e`.
///
/// **DEPRECATED** use the constant E
///
/// ```no_run
/// exampleSketch = startSketchOn("XZ")
/// |> startProfileAt([0, 0], %)
Expand All @@ -684,6 +689,7 @@ pub async fn e(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclE
#[stdlib {
name = "e",
tags = ["math"],
deprecated = true,
}]
fn inner_e() -> Result<f64, KclError> {
Ok(std::f64::consts::E)
Expand All @@ -698,6 +704,8 @@ pub async fn tau(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc

/// Return the value of `tau`. The full circle constant (τ). Equal to 2π.
///
/// **DEPRECATED** use the constant TAU
///
/// ```no_run
/// exampleSketch = startSketchOn("XZ")
/// |> startProfileAt([0, 0], %)
Expand All @@ -713,6 +721,7 @@ pub async fn tau(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
#[stdlib {
name = "tau",
tags = ["math"],
deprecated = true,
}]
fn inner_tau() -> Result<f64, KclError> {
Ok(std::f64::consts::TAU)
Expand Down

0 comments on commit 3e7c648

Please sign in to comment.