Skip to content

[CmdPal and Run Calculator] Fix issues with log functions and spaces#47767

Open
daverayment wants to merge 1 commit into
microsoft:mainfrom
daverayment:fix/cmdpal-run-calculator-logfn
Open

[CmdPal and Run Calculator] Fix issues with log functions and spaces#47767
daverayment wants to merge 1 commit into
microsoft:mainfrom
daverayment:fix/cmdpal-run-calculator-logfn

Conversation

@daverayment
Copy link
Copy Markdown
Collaborator

Summary of the Pull Request

The Calculator components in Command Palette and Run produced incorrect results or errors for log and ln inputs 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:

  • Natural log would be called instead of log base 10 for log (n).
  • An error would be shown for ln (n).

For example:
image

PR Checklist

  • Communication: I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected
  • Tests: Added/updated and all pass
  • Localization: All end-user-facing strings can be localized
  • Dev docs: Added/updated
  • New binaries: Added on the required places
  • Documentation updated: If checked, please file a pull request on our docs repo and link it here: #xxx

Detailed Description of the Pull Request / Additional comments

The issue was with this code:

input = input.
Replace("log(", "log10(", true, CultureInfo.CurrentCulture).
Replace("ln(", "log(", true, CultureInfo.CurrentCulture);

This maps user input such as log(10) or ln(10) to the functions the back-end expression evaluator understands. For Mages and ExprTK, this is mapped to log10(n) for base 10 logs or log(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:

  • For log (n) - the log10 match is missed and the expression is interpreted as a natural log, producing incorrect results.
  • For 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 IgnoreCase set. I took the opportunity to remove the "en-US" culture from the DivisionByZeroRegex, 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 log and ln now produce correct results in both applications.

…ectly interpreted as ln if a space existed after the function name. Unit tests added.
@daverayment daverayment added Product-PowerToys Run Improved app launch PT Run (Win+R) Window Run-Plugin Things that relate with PowerToys Run's plugin interface Product-Command Palette Refers to the Command Palette utility CmdPal - Calculator Issues related to the Calculator built-in extension and removed Run-Plugin Things that relate with PowerToys Run's plugin interface labels May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CmdPal - Calculator Issues related to the Calculator built-in extension Product-Command Palette Refers to the Command Palette utility Product-PowerToys Run Improved app launch PT Run (Win+R) Window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CmdPal and Run Calculator] log functions produce incorrect results or error if spaces are present between function name and argument list

1 participant