KCL: 'rem' fn in stdlib (#3999)
This commit is contained in:
@ -78,6 +78,7 @@ layout: manual
|
||||
* [`profileStart`](kcl/profileStart)
|
||||
* [`profileStartX`](kcl/profileStartX)
|
||||
* [`profileStartY`](kcl/profileStartY)
|
||||
* [`rem`](kcl/rem)
|
||||
* [`revolve`](kcl/revolve)
|
||||
* [`segAng`](kcl/segAng)
|
||||
* [`segEndX`](kcl/segEndX)
|
||||
|
39
docs/kcl/rem.md
Normal file
39
docs/kcl/rem.md
Normal file
File diff suppressed because one or more lines are too long
@ -221739,6 +221739,48 @@
|
||||
"const sketch001 = startSketchOn('XY')\n |> startProfileAt([5, 2], %)\n |> angledLine({ angle: -60, length: 14 }, %)\n |> angledLineToY({ angle: 30, to: profileStartY(%) }, %)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "rem",
|
||||
"summary": "Compute the remainder after dividing `num` by `div`.",
|
||||
"description": "If `num` is negative, the result will be too.",
|
||||
"tags": [
|
||||
"math"
|
||||
],
|
||||
"args": [
|
||||
{
|
||||
"name": "num",
|
||||
"type": "i64",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
{
|
||||
"name": "divisor",
|
||||
"type": "i64",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"required": true
|
||||
}
|
||||
],
|
||||
"returnValue": {
|
||||
"name": "",
|
||||
"type": "i64",
|
||||
"schema": {
|
||||
"type": "integer",
|
||||
"format": "int64"
|
||||
},
|
||||
"required": true
|
||||
},
|
||||
"unpublished": false,
|
||||
"deprecated": false,
|
||||
"examples": [
|
||||
"assertEqual(rem(int(7), int(4)), 3, 0.01, \"remainder is 3\")\nassertEqual(rem(int(-7), int(4)), -3, 0.01, \"remainder is 3\")\nassertEqual(rem(int(7), int(-4)), 3, 0.01, \"remainder is 3\")"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "revolve",
|
||||
"summary": "Rotate a sketch around some provided axis, creating a solid from its extent.",
|
||||
|
@ -581,6 +581,14 @@ impl<'a> FromKclValue<'a> for &'a str {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromKclValue<'a> for i64 {
|
||||
fn from_mem_item(arg: &'a KclValue) -> Option<Self> {
|
||||
arg.as_user_val()
|
||||
.and_then(|uv| uv.value.as_number())
|
||||
.and_then(|num| num.as_i64())
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FromKclValue<'a> for TagDeclarator {
|
||||
fn from_mem_item(arg: &'a KclValue) -> Option<Self> {
|
||||
arg.get_tag_declarator().ok()
|
||||
|
@ -10,6 +10,33 @@ use crate::{
|
||||
std::Args,
|
||||
};
|
||||
|
||||
use super::args::FromArgs;
|
||||
|
||||
/// Compute the remainder after dividing `num` by `div`.
|
||||
/// If `num` is negative, the result will be too.
|
||||
pub async fn rem(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let (n, d) = FromArgs::from_args(&args, 0)?;
|
||||
let result = inner_rem(n, d)?;
|
||||
|
||||
args.make_user_val_from_i64(result)
|
||||
}
|
||||
|
||||
/// Compute the remainder after dividing `num` by `div`.
|
||||
/// If `num` is negative, the result will be too.
|
||||
///
|
||||
/// ```no_run
|
||||
/// assertEqual(rem(int( 7), int(4)), 3, 0.01, "remainder is 3")
|
||||
/// assertEqual(rem(int(-7), int(4)), -3, 0.01, "remainder is 3")
|
||||
/// assertEqual(rem(int( 7), int(-4)), 3, 0.01, "remainder is 3")
|
||||
/// ```
|
||||
#[stdlib {
|
||||
name = "rem",
|
||||
tags = ["math"],
|
||||
}]
|
||||
fn inner_rem(num: i64, divisor: i64) -> Result<i64, KclError> {
|
||||
Ok(num % divisor)
|
||||
}
|
||||
|
||||
/// Compute the cosine of a number (in radians).
|
||||
pub async fn cos(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
|
||||
let num = args.get_number()?;
|
||||
|
@ -121,6 +121,7 @@ lazy_static! {
|
||||
Box::new(crate::std::math::Tau),
|
||||
Box::new(crate::std::math::Sqrt),
|
||||
Box::new(crate::std::math::Abs),
|
||||
Box::new(crate::std::math::Rem),
|
||||
Box::new(crate::std::math::Floor),
|
||||
Box::new(crate::std::math::Ceil),
|
||||
Box::new(crate::std::math::Min),
|
||||
|
BIN
src/wasm-lib/kcl/tests/outputs/serial_test_example_rem0.png
Normal file
BIN
src/wasm-lib/kcl/tests/outputs/serial_test_example_rem0.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 19 KiB |
Reference in New Issue
Block a user