Fix unhandled exception when evaluating sqrt(-1) in PowerToys Run Calculator#47399
Closed
Copilot wants to merge 2 commits into
Closed
Fix unhandled exception when evaluating sqrt(-1) in PowerToys Run Calculator#47399Copilot wants to merge 2 commits into
Copilot wants to merge 2 commits into
Conversation
… Complex number results Agent-Logs-Url: https://github.com/microsoft/PowerToys/sessions/fb3c68a1-171a-4418-947d-2a37617c8500 Co-authored-by: MuyuanMS <116717757+MuyuanMS@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix unhandled exception for sqrt(-1) in PowerToys Run Calculator
Fix unhandled exception when evaluating sqrt(-1) in PowerToys Run Calculator
Apr 29, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
Prevents a crash in the PowerToys Run Calculator plugin when the Mages engine evaluates expressions that yield an imaginary/complex result (e.g., sqrt(-1)), by converting that case into a localized, user-facing error.
Changes:
- Added a
System.Numerics.Complexguard inCalculateEngine.TransformResult()to return an error string instead of reachingConvert.ToDecimal(...). - Introduced a new localized resource string for complex-number results.
- Added unit test coverage to verify the user-visible error behavior for
=sqrt(-1)and that the engine path no longer throws.
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.resx | Adds new localized string for complex-number results. |
| src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.Designer.cs | Exposes the new resource string via the strongly-typed resource accessor. |
| src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateEngine.cs | Guards complex results in TransformResult() to avoid invalid decimal conversion. |
| src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/QueryTests.cs | Verifies =sqrt(-1) produces the expected user-facing error. |
| src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/ExtendedCalculatorParserTests.cs | Ensures sqrt(-1) yields no result (no exception) in engine-level parsing. |
Files not reviewed (1)
- src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.Designer.cs: Language not supported
MuyuanMS
added a commit
that referenced
this pull request
May 12, 2026
Added Complex type guard in TransformResult() so imaginary results (e.g. sqrt(-1)) surface as a user-friendly error message instead of crashing with an InvalidCastException when Convert.ToDecimal() is called on a System.Numerics.Complex value. Changes: - CalculateEngine.cs: Added using System.Numerics and Complex check - Resources.resx/Designer.cs: New resource string for complex results - Tests: Added sqrt(-1) to both QueryTests and ParserTests Closes #47399 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
MuyuanMS
approved these changes
May 13, 2026
LegendaryBlair
approved these changes
May 13, 2026
Contributor
|
Seems to be duplicated with work #47506 |
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.
sqrt(-1)caused an unhandledInvalidCastExceptionin the Calculator plugin because the Mages engine returnsSystem.Numerics.Complexfor imaginary results, andConvert.ToDecimal()doesn't support it.Summary of the Pull Request
Added a
Complextype guard inTransformResult()so imaginary results surface as a user-friendly error instead of crashing.PR Checklist
Detailed Description of the Pull Request / Additional comments
CalculateEngine.cs— Addedusing System.Numerics;and aComplexcheck inTransformResult()before theConvert.ToDecimal()call that was throwing.Resources.resx/Resources.Designer.cs— New resource stringwox_plugin_calculator_complex_number_result="Result is a complex number".The existing pattern for unrepresentable results (
NaN,Function,double[,]) already returns an error string fromTransformResult(); this fix follows the same convention.Validation Steps Performed
[DataRow("=sqrt(-1)", "Result is a complex number")]toQueryTests.ErrorResultOnInvalidKeywordQuery.[DataRow("sqrt(-1)")]toExtendedCalculatorParserTests.Interpret_NoResult_WhenCalledto confirm the engine returnsdefaultwith a populated error string rather than throwing.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
o3svsblobprodcus318.vsblob.vsassets.io/usr/bin/dotnet dotnet build -c Debug(dns block)If you need me to access, download, or install something from one of these locations, you can either: