Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ternary operator support #108

Open
maheshkurmi opened this issue Jun 24, 2020 · 1 comment
Open

Ternary operator support #108

maheshkurmi opened this issue Jun 24, 2020 · 1 comment

Comments

@maheshkurmi
Copy link

Is there any plan of introducing ternary operator support in exp4j? I think it will be a very useful feature. Please look at the link to get an idea I am talking about
http://math.hws.edu/eck/jsdemo/grapher.html

@dreamscached
Copy link

dreamscached commented May 31, 2024

This can be implemented as a function (although, which lacks the property of short circuit evaluation as it will always compute both arguments), here's what I could come up with:

public final class IfFunction extends Function {
    public IfFunction() {
        super("if", 3);
    }

    @Override
    public double apply(double... doubles) {
        return doubles[0] != 0 ? doubles[1] : doubles[2];
    }
}

Can be used like this:

if(condition,true,not_true)

Where condition is 0 for false and 1 for true, and true/not_true are correspondingly values the function returns if condition is met or not.

It'd be still very nice to have arbitrary arity support for operators, but I understand it will be quite a challenge.

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

No branches or pull requests

2 participants