From 64a12a353ccca74eb4493aedec8d8667166636ac Mon Sep 17 00:00:00 2001 From: wagesj45 Date: Tue, 19 Aug 2025 23:50:19 -0500 Subject: [PATCH] Implement AbsoluteValue function in stdlib --- src/stdlib/functions/AbsoluteValue.cs | 37 ++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) diff --git a/src/stdlib/functions/AbsoluteValue.cs b/src/stdlib/functions/AbsoluteValue.cs index a350abb..4578600 100644 --- a/src/stdlib/functions/AbsoluteValue.cs +++ b/src/stdlib/functions/AbsoluteValue.cs @@ -13,15 +13,46 @@ namespace stdlib.functions { get { - yield return new FunctionArgument() { }; + yield return new FunctionArgument( + name: "value", + fv: new FunctionValue(ValueType.Numeric, 0m) + ); } } - public FunctionValue ReturnValue => throw new NotImplementedException(); + public FunctionValue ReturnValue => new FunctionValue(ValueType.Numeric, 0m); public FunctionValue Execute(params FunctionArgument[] args) { - throw new NotImplementedException(); + if (args == null || args.Length < 1 || args[0] == null || args[0].Value == null) + { + return new FunctionValue(ValueType.None, null); + } + + try + { + var input = args[0].Value; + // Try to interpret both numeric and numeric-like string inputs + decimal number; + if (input.Type == ValueType.Numeric) + { + number = Convert.ToDecimal(input.Value); + } + else if (input.Type == ValueType.String && input.Value is string s) + { + number = Convert.ToDecimal(s); + } + else + { + return new FunctionValue(ValueType.None, null); + } + + return new FunctionValue(ValueType.Numeric, Math.Abs(number)); + } + catch + { + return new FunctionValue(ValueType.None, null); + } } } }