using CSMic; namespace CSMic.StandardLibrary.Functions { /// A function that returns 1 if the expression is positive and -1 if it is negative. public class Sign : FunctionBase, ICodedFunction { /// (Immutable) The return value representing "positive". private const decimal POSITIVE = 1; /// (Immutable) The return value representing "negative". private const decimal NEGATIVE = -1; /// Gets the name of the function. /// sign. public string Name { get { return "sign"; } } /// Gets the expected arguments. /// The expected arguments. public override IEnumerable ExpectedArguments { get { yield return new FunctionArgument("value", FunctionValue.NUMBER); } } /// Executes the function with the given arguments. /// A variable-length parameters list containing arguments. /// A representing the result of the function execution. public FunctionValue Execute(params FunctionArgument[] args) { return base.Execute(args, (_args) => { var input = _args[0].Value; decimal number = Convert.ToDecimal(input.Value); return new FunctionValue(FunctionValueType.Numeric, number >= 0 ? POSITIVE : NEGATIVE); }); } } }