feat: add abs function

This commit is contained in:
electria 2026-11-04 14:10:34 -08:00
commit 4eb6f55f9a
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs
4 changed files with 12 additions and 0 deletions

View file

@ -153,6 +153,8 @@ impl Function {
Self::ArcTangent => Float::atan_mut(n), Self::ArcTangent => Float::atan_mut(n),
Self::Secant => Float::sec_mut(n), Self::Secant => Float::sec_mut(n),
Self::CoSecant => Float::csc_mut(n), Self::CoSecant => Float::csc_mut(n),
Self::Abs => Float::abs_mut(n),
} }
} }
} }

View file

@ -86,4 +86,6 @@ pub enum Function {
ArcTangent, ArcTangent,
Secant, Secant,
CoSecant, CoSecant,
Abs,
} }

View file

@ -50,6 +50,9 @@ impl FromStr for Function {
"atan" | "arctan" => Ok(Self::ArcTangent), "atan" | "arctan" => Ok(Self::ArcTangent),
"sec" => Ok(Self::Secant), "sec" => Ok(Self::Secant),
"csc" => Ok(Self::CoSecant), "csc" => Ok(Self::CoSecant),
"abs" => Ok(Self::Abs),
_ => Err(s.to_owned()), _ => Err(s.to_owned()),
} }
} }

View file

@ -63,3 +63,8 @@ fn operator_after_parenthesis() {
assert_eq!(eval("(0.2)20^2"), 80.); assert_eq!(eval("(0.2)20^2"), 80.);
assert_eq!(eval("(0.2)20^2/2 - (0.2)10^2/2"), 30.); assert_eq!(eval("(0.2)20^2/2 - (0.2)10^2/2"), 30.);
} }
#[test]
fn abs() {
assert_eq!(eval("abs(-1)"), 1.);
}