[CmdPal and Run Calculator] Fix issues with log functions and spaces#47767
Open
daverayment wants to merge 1 commit into
Open
[CmdPal and Run Calculator] Fix issues with log functions and spaces#47767daverayment wants to merge 1 commit into
daverayment wants to merge 1 commit into
Conversation
…ectly interpreted as ln if a space existed after the function name. Unit tests added.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary of the Pull Request
The Calculator components in Command Palette and Run produced incorrect results or errors for
logandlninputs where spaces exist between the function name and the argument list. This is because input validation allowed for those spaces, but this was not respected in the log mapping code which transforms user input into a string for consumption by the expression evaluator engine.The result of this is discrepancy was that:
log (n).ln (n).For example:

PR Checklist
Detailed Description of the Pull Request / Additional comments
The issue was with this code:
PowerToys/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateEngine.cs
Lines 56 to 58 in a650504
This maps user input such as
log(10)orln(10)to the functions the back-end expression evaluator understands. For Mages and ExprTK, this is mapped tolog10(n)for base 10 logs orlog(n)for natural logs.Unfortunately, the input validator regex allows for spaces between the function name and the start of the argument list, and the string replace does not. When spaces exist:
log (n)- the log10 match is missed and the expression is interpreted as a natural log, producing incorrect results.ln (n)- the string is passed as-is to the back-end. Neither Mages nor ExprTK recognise it, so an error is returned.The fix is to replace the string replacement code with two regexes. These are both forgiving of spaces between the function name and argument list:
For log base 10:
"log(?![0-9])\\s*\\("For natural log:
"ln\\s*\\("Both are culture-agnostic and have
IgnoreCaseset. I took the opportunity to remove the "en-US" culture from theDivisionByZeroRegex, as it is not required.Validation Steps Performed
Added unit tests which ensure the log and ln functions perform identically with or without spaces. Confirmed all unit tests for Command Palette and Run still pass.
Manually confirmed that
logandlnnow produce correct results in both applications.