KCL: 'rem' fn in stdlib (#3999)

This commit is contained in:
Adam Chalmers
2024-09-26 16:00:07 -05:00
committed by GitHub
parent 294040b618
commit fd21850e48
7 changed files with 118 additions and 0 deletions

View File

@ -78,6 +78,7 @@ layout: manual
* [`profileStart`](kcl/profileStart) * [`profileStart`](kcl/profileStart)
* [`profileStartX`](kcl/profileStartX) * [`profileStartX`](kcl/profileStartX)
* [`profileStartY`](kcl/profileStartY) * [`profileStartY`](kcl/profileStartY)
* [`rem`](kcl/rem)
* [`revolve`](kcl/revolve) * [`revolve`](kcl/revolve)
* [`segAng`](kcl/segAng) * [`segAng`](kcl/segAng)
* [`segEndX`](kcl/segEndX) * [`segEndX`](kcl/segEndX)

39
docs/kcl/rem.md Normal file

File diff suppressed because one or more lines are too long

View File

@ -221739,6 +221739,48 @@
"const sketch001 = startSketchOn('XY')\n |> startProfileAt([5, 2], %)\n |> angledLine({ angle: -60, length: 14 }, %)\n |> angledLineToY({ angle: 30, to: profileStartY(%) }, %)" "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", "name": "revolve",
"summary": "Rotate a sketch around some provided axis, creating a solid from its extent.", "summary": "Rotate a sketch around some provided axis, creating a solid from its extent.",

View File

@ -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 { impl<'a> FromKclValue<'a> for TagDeclarator {
fn from_mem_item(arg: &'a KclValue) -> Option<Self> { fn from_mem_item(arg: &'a KclValue) -> Option<Self> {
arg.get_tag_declarator().ok() arg.get_tag_declarator().ok()

View File

@ -10,6 +10,33 @@ use crate::{
std::Args, 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). /// Compute the cosine of a number (in radians).
pub async fn cos(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> { pub async fn cos(_exec_state: &mut ExecState, args: Args) -> Result<KclValue, KclError> {
let num = args.get_number()?; let num = args.get_number()?;

View File

@ -121,6 +121,7 @@ lazy_static! {
Box::new(crate::std::math::Tau), Box::new(crate::std::math::Tau),
Box::new(crate::std::math::Sqrt), Box::new(crate::std::math::Sqrt),
Box::new(crate::std::math::Abs), Box::new(crate::std::math::Abs),
Box::new(crate::std::math::Rem),
Box::new(crate::std::math::Floor), Box::new(crate::std::math::Floor),
Box::new(crate::std::math::Ceil), Box::new(crate::std::math::Ceil),
Box::new(crate::std::math::Min), Box::new(crate::std::math::Min),

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB