Skip to content

Handle complex calculator results#47506

Open
MardSilva wants to merge 1 commit into
microsoft:mainfrom
MardSilva:BugFix-43937
Open

Handle complex calculator results#47506
MardSilva wants to merge 1 commit into
microsoft:mainfrom
MardSilva:BugFix-43937

Conversation

@MardSilva
Copy link
Copy Markdown

Summary of the Pull Request

Return a friendly calculator error when Mages evaluates an expression to a complex number instead of letting decimal conversion throw.

This fixes the PowerToys Run Calculator result for expressions such as sqrt(-1) by detecting System.Numerics.Complex results before decimal conversion and showing a localized error message instead.

Fixes #43937

PR Checklist

Detailed Description of the Pull Request / Additional comments

The Calculator plugin previously passed complex results from Mages into Convert.ToDecimal, which caused an exception for expressions like sqrt(-1).

This PR updates the calculator result transformation logic to detect System.Numerics.Complex and return a localized user-facing error message: Complex numbers are not supported.

It also updates calculator query tests to cover both direct keyword and global query behavior.

Validation Steps Performed

  • Added unit test coverage for =sqrt(-1) returning Complex numbers are not supported.
  • Added unit test coverage for global query sqrt(-1) returning no result instead of surfacing an unhandled exception.
  • Ran git diff --check.
  • Attempted local build/test with the PowerToys build scripts, but local validation was blocked by Visual Studio/VC tooling configuration issues unrelated to this change: PlatformToolsetVersion resolves to an empty value during restore/build.

Return a friendly calculator error when Mages evaluates an expression to a complex number instead of letting decimal conversion throw.

Fixes microsoft#43937
@michaeljolley michaeljolley added the Product-Command Palette Refers to the Command Palette utility label Apr 30, 2026
@michaeljolley
Copy link
Copy Markdown
Contributor

Same here @MardSilva. Can you confirm a build/test?

@MardSilva
Copy link
Copy Markdown
Author

I updated my local dev environment according to the dev docs and validated this PR locally.

Validation passed:

  • tools\build\build-essentials.cmd
  • tools\build\build.cmd -Path src\modules\launcher\Plugins\Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest
  • vstest.console.exe for Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest: 362/362 passed

@michaeljolley
Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

Copy link
Copy Markdown
Collaborator

@daverayment daverayment left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix looks sound, but there's only a single test case at the moment. Although it tests the specific case referred to in the original issue, it would be useful to expand coverage to handle other cases and representations like sqrt(-4), (-1)^0.5 and log(-1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-Command Palette Refers to the Command Palette utility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unhandled exception when evaluating sqrt(-1) in PowerToys Run Calculator

3 participants