Skip to content

Commit c46083d

Browse files
authored
Handle complex calculator results (#47506)
## 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 - [x] Closes: #43937 - [ ] **Communication:** I've discussed this with core contributors already. If the work hasn't been agreed, this work might be rejected - [x] **Tests:** Added/updated and all pass - [x] **Localization:** All end-user-facing strings can be localized - [x] **Dev docs:** Added/updated - [x] **New binaries:** Added on the required places - [x] [JSON for signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json) for new binaries - [x] [WXS for installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs) for new binaries and localization folder - [x] [YML for CI pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml) for new test projects - [x] [YML for signed pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml) - [x] **Documentation updated:** Not required for this bug fix. ## 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.
1 parent 65112a7 commit c46083d

4 files changed

Lines changed: 21 additions & 1 deletion

File tree

src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/QueryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public void Initialize()
3737
[DataRow("=2^96", "Result value was either too large or too small for a decimal number")]
3838
[DataRow("=+()", "Calculation result is not a valid number (NaN)")]
3939
[DataRow("=[10,10]", "Unsupported use of square brackets")]
40+
[DataRow("=sqrt(-1)", "Complex numbers are not supported")]
4041
[DataRow("=5/0", "Expression contains division by zero")]
4142
[DataRow("=5 / 0", "Expression contains division by zero")]
4243
[DataRow("10+(8*9)/0+7", "Expression contains division by zero")]
@@ -63,6 +64,7 @@ public void ErrorResultOnInvalidKeywordQuery(string typedString, string expected
6364
[DataRow("2^96")]
6465
[DataRow("+()")]
6566
[DataRow("[10,10]")]
67+
[DataRow("sqrt(-1)")]
6668
[DataRow("5/0")]
6769
[DataRow("5 / 0")]
6870
[DataRow("10+(8*9)/0+7")]

src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateEngine.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Globalization;
8+
using System.Numerics;
89
using System.Text.RegularExpressions;
910

1011
using Mages.Core;
@@ -124,6 +125,11 @@ private static dynamic TransformResult(object result)
124125
return Properties.Resources.wox_plugin_calculator_double_array_returned;
125126
}
126127

128+
if (result is Complex)
129+
{
130+
return Properties.Resources.wox_plugin_calculator_complex_number_returned;
131+
}
132+
127133
return result;
128134
}
129135

src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@
141141
<data name="wox_plugin_calculator_double_array_returned" xml:space="preserve">
142142
<value>Unsupported use of square brackets</value>
143143
</data>
144+
<data name="wox_plugin_calculator_complex_number_returned" xml:space="preserve">
145+
<value>Complex numbers are not supported</value>
146+
</data>
144147
<data name="wox_plugin_calculator_not_covert_to_decimal" xml:space="preserve">
145148
<value>Result value was either too large or too small for a decimal number</value>
146149
</data>
@@ -187,4 +190,4 @@
187190
<value>Gradians</value>
188191
<comment>Text for angle unit.</comment>
189192
</data>
190-
</root>
193+
</root>

0 commit comments

Comments
 (0)