Extend calculator functionality #1009
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I was checking out Ulauncher when I noticed that the builtin calculator lacks math functions as well as constants. This PR adds this functionality and corresponding tests.
I had to change the
is_enabled
regex to allow expressions to start with functions or constants. This also allows expressions like+3
to be picked up, whereas it was not before. From my limited testing this expansion does not conflict with other operations, but expressions like)+3
are picked up. This seems like a good trade-off as such a string seems like a mathematical expression and the calculator just emits an invalid expression error.With mathematical functions expressions like
sin(pi)
are possible, which should be 0, but with machine precision it results in0.xxE-16
, therefore I added a rounding step after evaluating the expression.Checklist
./ul test
is passing (the CI server will check this if you don't)