From 4eb6f55f9af6a2be762208979d76589706210148 Mon Sep 17 00:00:00 2001 From: electria Date: Wed, 4 Nov 2026 14:10:34 -0800 Subject: [PATCH] feat: add abs function --- src/nodes/eval.rs | 2 ++ src/nodes/mod.rs | 2 ++ src/nodes/parse.rs | 3 +++ src/nodes/tests.rs | 5 +++++ 4 files changed, 12 insertions(+) diff --git a/src/nodes/eval.rs b/src/nodes/eval.rs index 33c606b..b370b35 100644 --- a/src/nodes/eval.rs +++ b/src/nodes/eval.rs @@ -153,6 +153,8 @@ impl Function { Self::ArcTangent => Float::atan_mut(n), Self::Secant => Float::sec_mut(n), Self::CoSecant => Float::csc_mut(n), + + Self::Abs => Float::abs_mut(n), } } } diff --git a/src/nodes/mod.rs b/src/nodes/mod.rs index dbc5461..c165052 100644 --- a/src/nodes/mod.rs +++ b/src/nodes/mod.rs @@ -86,4 +86,6 @@ pub enum Function { ArcTangent, Secant, CoSecant, + + Abs, } diff --git a/src/nodes/parse.rs b/src/nodes/parse.rs index 662871a..e4fad1c 100644 --- a/src/nodes/parse.rs +++ b/src/nodes/parse.rs @@ -50,6 +50,9 @@ impl FromStr for Function { "atan" | "arctan" => Ok(Self::ArcTangent), "sec" => Ok(Self::Secant), "csc" => Ok(Self::CoSecant), + + "abs" => Ok(Self::Abs), + _ => Err(s.to_owned()), } } diff --git a/src/nodes/tests.rs b/src/nodes/tests.rs index a4de7d2..4a8b2db 100644 --- a/src/nodes/tests.rs +++ b/src/nodes/tests.rs @@ -63,3 +63,8 @@ fn operator_after_parenthesis() { assert_eq!(eval("(0.2)20^2"), 80.); assert_eq!(eval("(0.2)20^2/2 - (0.2)10^2/2"), 30.); } + +#[test] +fn abs() { + assert_eq!(eval("abs(-1)"), 1.); +}