Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

A new Function #21

Open
ibt1haj opened this issue Oct 25, 2023 · 1 comment
Open

A new Function #21

ibt1haj opened this issue Oct 25, 2023 · 1 comment

Comments

@ibt1haj
Copy link

ibt1haj commented Oct 25, 2023

i am using ncalc and i have a use case for a new function,so i have create a pull request for it

#20

some details are given below

Function name: Pos()
Working: returns zero if the input is negative, and returns the input value if it is positive
Example:
Pos(-1) = 0
Pos(1) =1

@yallie
Copy link
Member

yallie commented Oct 26, 2023

Hi @ibt1haj,

sorry for my late reply!

Pos doesn't look like standard math function if I'm not mistaken.
Please consider using EvaluateFunctionAsync event handler for custom functions:

var e = new Expression("Pos(1) + Pos(-1)");

e.EvaluateFunctionAsync += async (name, args) =>
{
  if (name == "Pos")
  {
    var temp = Convert.ToDouble(await args.Parameters[0].EvaluateAsync());
    args.Result = temp > 0 ? temp : 0;
  }
};

More examples can be found in the unit tests: Fixtures.cs

Regards, Alex.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants