Change trig functions to return number with Default units (#7425)

* Change trig functions to return number with Default units

* Update docs

* Update output
This commit is contained in:
Jonathan Tran
2025-06-10 20:24:48 -04:00
committed by GitHub
parent ff15c7b9db
commit 851ea28bd3
21 changed files with 192 additions and 115 deletions

View File

@ -8,7 +8,7 @@ layout: manual
Compute the cosine of a number.
```kcl
cos(@num: number(Angle)): number(_)
cos(@num: number(Angle)): number
```
@ -21,7 +21,7 @@ cos(@num: number(Angle)): number(_)
### Returns
[`number(_)`](/docs/kcl-std/types/std-types-number) - A number.
[`number`](/docs/kcl-std/types/std-types-number) - A number.
### Examples

View File

@ -8,7 +8,7 @@ layout: manual
Compute the sine of a number.
```kcl
sin(@num: number(Angle)): number(_)
sin(@num: number(Angle)): number
```
@ -21,7 +21,7 @@ sin(@num: number(Angle)): number(_)
### Returns
[`number(_)`](/docs/kcl-std/types/std-types-number) - A number.
[`number`](/docs/kcl-std/types/std-types-number) - A number.
### Examples

View File

@ -8,7 +8,7 @@ layout: manual
Compute the tangent of a number.
```kcl
tan(@num: number(Angle)): number(_)
tan(@num: number(Angle)): number
```
@ -21,7 +21,7 @@ tan(@num: number(Angle)): number(_)
### Returns
[`number(_)`](/docs/kcl-std/types/std-types-number) - A number.
[`number`](/docs/kcl-std/types/std-types-number) - A number.
### Examples

View File

@ -2342,10 +2342,10 @@ d = cos(30)
let result = parse_execute(program).await.unwrap();
assert!(result.exec_state.errors().is_empty());
assert_value_and_type("a", &result, 1.0, NumericType::count());
assert_value_and_type("a", &result, 1.0, NumericType::default());
assert_value_and_type("b", &result, 3.0, NumericType::default());
assert_value_and_type("c", &result, 1.0, NumericType::count());
assert_value_and_type("d", &result, 1.0, NumericType::count());
assert_value_and_type("c", &result, 1.0, NumericType::default());
assert_value_and_type("d", &result, 1.0, NumericType::default());
}
#[tokio::test(flavor = "multi_thread")]

View File

@ -402,8 +402,8 @@ mod any_type {
super::execute(TEST_NAME, false).await
}
}
mod error_with_point_shows_numeric_units {
const TEST_NAME: &str = "error_with_point_shows_numeric_units";
mod coerce_from_trig_to_point {
const TEST_NAME: &str = "coerce_from_trig_to_point";
/// Test parsing KCL.
#[test]

View File

@ -34,21 +34,21 @@ pub async fn rem(exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kcl
pub async fn cos(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let num: TyF64 = args.get_unlabeled_kw_arg("input", &RuntimeType::angle(), exec_state)?;
let num = num.to_radians();
Ok(args.make_user_val_from_f64_with_type(TyF64::count(num.cos())))
Ok(args.make_user_val_from_f64_with_type(TyF64::new(num.cos(), exec_state.current_default_units())))
}
/// Compute the sine of a number (in radians).
pub async fn sin(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let num: TyF64 = args.get_unlabeled_kw_arg("input", &RuntimeType::angle(), exec_state)?;
let num = num.to_radians();
Ok(args.make_user_val_from_f64_with_type(TyF64::count(num.sin())))
Ok(args.make_user_val_from_f64_with_type(TyF64::new(num.sin(), exec_state.current_default_units())))
}
/// Compute the tangent of a number (in radians).
pub async fn tan(exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let num: TyF64 = args.get_unlabeled_kw_arg("input", &RuntimeType::angle(), exec_state)?;
let num = num.to_radians();
Ok(args.make_user_val_from_f64_with_type(TyF64::count(num.tan())))
Ok(args.make_user_val_from_f64_with_type(TyF64::new(num.tan(), exec_state.current_default_units())))
}
/// Compute the square root of a number.

View File

@ -72,7 +72,7 @@ export TAU = 6.28318530717958647692528676655900577_
/// example = extrude(exampleSketch, length = 5)
/// ```
@(impl = std_rust)
export fn cos(@num: number(Angle)): number(Count) {}
export fn cos(@num: number(Angle)): number {}
/// Compute the sine of a number.
///
@ -89,7 +89,7 @@ export fn cos(@num: number(Angle)): number(Count) {}
/// example = extrude(exampleSketch, length = 5)
/// ```
@(impl = std_rust)
export fn sin(@num: number(Angle)): number(Count) {}
export fn sin(@num: number(Angle)): number {}
/// Compute the tangent of a number.
///
@ -106,7 +106,7 @@ export fn sin(@num: number(Angle)): number(Count) {}
/// example = extrude(exampleSketch, length = 5)
/// ```
@(impl = std_rust)
export fn tan(@num: number(Angle)): number(Count) {}
export fn tan(@num: number(Angle)): number {}
/// Compute the arccosine of a number.
///

View File

@ -0,0 +1,133 @@
---
source: kcl-lib/src/simulation_tests.rs
description: Artifact commands coerce_from_trig_to_point.kcl
---
[
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "edge_lines_visible",
"hidden": false
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "object_visible",
"object_id": "[uuid]",
"hidden": true
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "object_visible",
"object_id": "[uuid]",
"hidden": true
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "make_plane",
"origin": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"x_axis": {
"x": 1.0,
"y": 0.0,
"z": 0.0
},
"y_axis": {
"x": 0.0,
"y": 0.0,
"z": 1.0
},
"size": 60.0,
"clobber": false,
"hide": true
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "close_path",
"path_id": "[uuid]"
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "enable_sketch_mode",
"entity_id": "[uuid]",
"ortho": false,
"animated": false,
"adjust_camera": false,
"planar_normal": {
"x": 0.0,
"y": -1.0,
"z": 0.0
}
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "extend_path",
"path": "[uuid]",
"segment": {
"type": "arc",
"center": {
"x": 0.918,
"y": 0.397
},
"radius": 1.0,
"start": {
"unit": "degrees",
"value": 0.0
},
"end": {
"unit": "degrees",
"value": 360.0
},
"relative": false
}
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "move_path_pen",
"path": "[uuid]",
"to": {
"x": 1.918,
"y": 0.397,
"z": 0.0
}
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "sketch_mode_disable"
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "start_path"
}
}
]

View File

@ -1,6 +1,6 @@
---
source: kcl-lib/src/simulation_tests.rs
description: Artifact graph flowchart error_with_point_shows_numeric_units.kcl
description: Artifact graph flowchart coerce_from_trig_to_point.kcl
extension: md
snapshot_kind: binary
---

View File

@ -0,0 +1,15 @@
```mermaid
flowchart LR
subgraph path2 [Path]
2["Path<br>[23, 92, 0]"]
%% [ProgramBodyItem { index: 0 }, ExpressionStatementExpr, PipeBodyItem { index: 1 }]
3["Segment<br>[23, 92, 0]"]
%% [ProgramBodyItem { index: 0 }, ExpressionStatementExpr, PipeBodyItem { index: 1 }]
4[Solid2d]
end
1["Plane<br>[0, 17, 0]"]
%% [ProgramBodyItem { index: 0 }, ExpressionStatementExpr, PipeBodyItem { index: 0 }]
1 --- 2
2 --- 3
2 --- 4
```

View File

@ -0,0 +1,5 @@
---
source: kcl-lib/src/simulation_tests.rs
description: Variables in memory after executing coerce_from_trig_to_point.kcl
---
{}

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

View File

@ -134,8 +134,13 @@ description: Variables in memory after executing computed_var.kcl
"type": "Number",
"value": 1.0,
"ty": {
"type": "Known",
"type": "Count"
"type": "Default",
"len": {
"type": "Mm"
},
"angle": {
"type": "Degrees"
}
}
}
}

View File

@ -1,57 +0,0 @@
---
source: kcl-lib/src/simulation_tests.rs
description: Artifact commands error_with_point_shows_numeric_units.kcl
---
[
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "edge_lines_visible",
"hidden": false
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "object_visible",
"object_id": "[uuid]",
"hidden": true
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "object_visible",
"object_id": "[uuid]",
"hidden": true
}
},
{
"cmdId": "[uuid]",
"range": [],
"command": {
"type": "make_plane",
"origin": {
"x": 0.0,
"y": 0.0,
"z": 0.0
},
"x_axis": {
"x": 1.0,
"y": 0.0,
"z": 0.0
},
"y_axis": {
"x": 0.0,
"y": 0.0,
"z": 1.0
},
"size": 60.0,
"clobber": false,
"hide": true
}
}
]

View File

@ -1,5 +0,0 @@
```mermaid
flowchart LR
1["Plane<br>[0, 17, 0]"]
%% [ProgramBodyItem { index: 0 }, ExpressionStatementExpr, PipeBodyItem { index: 0 }]
```

View File

@ -1,29 +0,0 @@
---
source: kcl-lib/src/simulation_tests.rs
description: Error from executing error_with_point_shows_numeric_units.kcl
---
KCL Semantic error
× semantic: center requires a value with type `Point2d`, but found array of
│ number(Count), number(Count) with 2 values
╭─[2:6]
1 │ startSketchOn(XZ)
2 │ ╭──▶ |> circle(center = [
3 │ ││ sin(66.6deg),
4 │ ││ cos(66.6deg)
5 │ ├──▶ ], radius = 1)
· ╰───── tests/error_with_point_shows_numeric_units/input.kcl
· ╰───── tests/error_with_point_shows_numeric_units/input.kcl
╰────
╰─▶ KCL Semantic error
× semantic: center requires a value with type `Point2d`, but found
│ array of number(Count), number(Count) with 2 values
╭─[2:22]
1 │ startSketchOn(XZ)
2 │ ╭─▶ |> circle(center = [
3 │ │ sin(66.6deg),
4 │ │ cos(66.6deg)
5 │ ├─▶ ], radius = 1)
· ╰──── tests/error_with_point_shows_numeric_units/input.kcl
╰────

View File

@ -173,8 +173,13 @@ description: Variables in memory after executing keyboard.kcl
"type": "Number",
"value": 0.122,
"ty": {
"type": "Known",
"type": "Count"
"type": "Default",
"len": {
"type": "Inches"
},
"angle": {
"type": "Degrees"
}
}
}
]
@ -349,8 +354,13 @@ description: Variables in memory after executing keyboard.kcl
"type": "Number",
"value": 0.122,
"ty": {
"type": "Known",
"type": "Count"
"type": "Default",
"len": {
"type": "Inches"
},
"angle": {
"type": "Degrees"
}
}
}
]