Skip to content

Fix unhandled exception when evaluating sqrt(-1) in PowerToys Run Calculator#47399

Closed
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-sqrt-negative-one-error
Closed

Fix unhandled exception when evaluating sqrt(-1) in PowerToys Run Calculator#47399
Copilot wants to merge 2 commits into
mainfrom
copilot/fix-sqrt-negative-one-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 29, 2026

sqrt(-1) caused an unhandled InvalidCastException in the Calculator plugin because the Mages engine returns System.Numerics.Complex for imaginary results, and Convert.ToDecimal() doesn't support it.

Summary of the Pull Request

Added a Complex type guard in TransformResult() so imaginary results surface as a user-friendly error instead of crashing.

// CalculateEngine.cs — TransformResult()
if (result is Complex)
{
    return Properties.Resources.wox_plugin_calculator_complex_number_result; // "Result is a complex number"
}

PR Checklist

  • Closes: #xxx
  • 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

  • CalculateEngine.cs — Added using System.Numerics; and a Complex check in TransformResult() before the Convert.ToDecimal() call that was throwing.
  • Resources.resx / Resources.Designer.cs — New resource string wox_plugin_calculator_complex_number_result = "Result is a complex number".

The existing pattern for unrepresentable results (NaN, Function, double[,]) already returns an error string from TransformResult(); this fix follows the same convention.

Validation Steps Performed

  • Added [DataRow("=sqrt(-1)", "Result is a complex number")] to QueryTests.ErrorResultOnInvalidKeywordQuery.
  • Added [DataRow("sqrt(-1)")] to ExtendedCalculatorParserTests.Interpret_NoResult_WhenCalled to confirm the engine returns default with 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
    • Triggering command: /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:

… 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
Copilot AI requested a review from MuyuanMS April 29, 2026 10:07
@niels9001 niels9001 added the Product-PowerToys Run Improved app launch PT Run (Win+R) Window label Apr 29, 2026
@MuyuanMS MuyuanMS requested a review from Copilot May 7, 2026 08:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.Complex guard in CalculateEngine.TransformResult() to return an error string instead of reaching Convert.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 MuyuanMS marked this pull request as ready for review May 7, 2026 10:22
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
Copy link
Copy Markdown
Contributor

Seems to be duplicated with work #47506

@MuyuanMS MuyuanMS closed this May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Product-PowerToys Run Improved app launch PT Run (Win+R) Window

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants