Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for float numbers in rem() arguments #4858

Merged
merged 12 commits into from
Jan 2, 2025
11 changes: 7 additions & 4 deletions docs/kcl/rem.md

Large diffs are not rendered by default.

26 changes: 13 additions & 13 deletions docs/kcl/std.json
Original file line number Diff line number Diff line change
Expand Up @@ -153550,25 +153550,25 @@
"args": [
{
"name": "num",
"type": "i64",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "int64",
"type": "integer",
"format": "int64"
"title": "double",
jtran marked this conversation as resolved.
Show resolved Hide resolved
"type": "number",
jtran marked this conversation as resolved.
Show resolved Hide resolved
"format": "double"
},
"required": true,
"description": "The number which will be divided by `divisor`.",
"labelRequired": false
},
{
"name": "divisor",
"type": "i64",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "int64",
"type": "integer",
"format": "int64"
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"description": "The number which will divide `num`.",
Expand All @@ -153577,20 +153577,20 @@
],
"returnValue": {
"name": "",
"type": "i64",
"type": "number",
"schema": {
"$schema": "https://spec.openapis.org/oas/3.0/schema/2019-04-02#/definitions/Schema",
"title": "int64",
"type": "integer",
"format": "int64"
"title": "double",
"type": "number",
"format": "double"
},
"required": true,
"labelRequired": true
},
"unpublished": false,
"deprecated": false,
"examples": [
"assertEqual(rem(7, divisor = 4), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(-7, divisor = 4), -3, 0.01, \"remainder is -3\")\nassertEqual(rem(7, divisor = -4), 3, 0.01, \"remainder is 3\")"
"assertEqual(rem(7, divisor = 4), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(-7, divisor = 4), -3, 0.01, \"remainder is -3\")\nassertEqual(rem(7, divisor = -4), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(6, divisor = 2.5), 1, 0.01, \"remainder is 1\")\nassertEqual(rem(6.5, divisor = 2.5), 1.5, 0.01, \"remainder is 1.5\")\nassertEqual(rem(6.5, divisor = 2), 0.5, 0.01, \"remainder is 0.5\")"
]
},
{
Expand Down
5 changes: 5 additions & 0 deletions public/kcl-samples-manifest-fallback.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@
"title": "Hex nut",
"description": "A hex nut is a type of fastener with a threaded hole and a hexagonal outer shape, used in a wide variety of applications to secure parts together. The hexagonal shape allows for a greater torque to be applied with wrenches or tools, making it one of the most common nut types in hardware."
},
{
"file": "i-beam.kcl",
"title": "I-beam",
"description": "A structural metal beam with an I shaped cross section. Often used in construction"
},
{
"file": "kitt.kcl",
"title": "Kitt",
Expand Down
9 changes: 0 additions & 9 deletions src/wasm-lib/kcl/src/std/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,6 @@ impl Args {
)
}

pub(crate) fn make_user_val_from_i64(&self, n: i64) -> KclValue {
KclValue::Int {
value: n,
meta: vec![Metadata {
source_range: self.source_range,
}],
}
}

pub(crate) fn make_user_val_from_f64_array(&self, f: Vec<f64>) -> Result<KclValue, KclError> {
let array = f
.into_iter()
Expand Down
11 changes: 7 additions & 4 deletions src/wasm-lib/kcl/src/std/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ use super::args::FromArgs;
pub async fn rem(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let n = args.get_unlabeled_kw_arg("number to divide")?;
let d = args.get_kw_arg("divisor")?;
let result = inner_rem(n, d)?;
let remainder = inner_rem(n, d);

Ok(args.make_user_val_from_i64(result))
Ok(args.make_user_val_from_f64(remainder))
}

/// Compute the remainder after dividing `num` by `div`.
Expand All @@ -28,6 +28,9 @@ pub async fn rem(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
/// assertEqual(rem( 7, divisor = 4), 3, 0.01, "remainder is 3" )
/// assertEqual(rem(-7, divisor = 4), -3, 0.01, "remainder is -3")
/// assertEqual(rem( 7, divisor = -4), 3, 0.01, "remainder is 3" )
/// assertEqual(rem( 6, divisor = 2.5), 1, 0.01, "remainder is 1" )
/// assertEqual(rem( 6.5, divisor = 2.5), 1.5, 0.01, "remainder is 1.5" )
/// assertEqual(rem( 6.5, divisor = 2), 0.5, 0.01, "remainder is 0.5" )
/// ```
#[stdlib {
name = "rem",
Expand All @@ -39,8 +42,8 @@ pub async fn rem(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, Kc
divisor = "The number which will divide `num`.",
}
}]
fn inner_rem(num: i64, divisor: i64) -> Result<i64, KclError> {
Ok(num % divisor)
fn inner_rem(num: f64, divisor: f64) -> f64 {
num % divisor
}

/// Compute the cosine of a number (in radians).
Expand Down
Loading