From 8c61ee10bfdb876046d7b852441c0cd4a8c45775 Mon Sep 17 00:00:00 2001 From: Eymard Silva Date: Thu, 30 Apr 2026 15:02:16 +0100 Subject: [PATCH] Handle complex calculator results Return a friendly calculator error when Mages evaluates an expression to a complex number instead of letting decimal conversion throw. Fixes #43937 --- .../QueryTests.cs | 2 ++ .../CalculateEngine.cs | 6 ++++++ .../Properties/Resources.Designer.cs | 9 +++++++++ .../Properties/Resources.resx | 5 ++++- 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/QueryTests.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/QueryTests.cs index 2c3896cde66b..e61719842add 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/QueryTests.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator.UnitTest/QueryTests.cs @@ -37,6 +37,7 @@ public void Initialize() [DataRow("=2^96", "Result value was either too large or too small for a decimal number")] [DataRow("=+()", "Calculation result is not a valid number (NaN)")] [DataRow("=[10,10]", "Unsupported use of square brackets")] + [DataRow("=sqrt(-1)", "Complex numbers are not supported")] [DataRow("=5/0", "Expression contains division by zero")] [DataRow("=5 / 0", "Expression contains division by zero")] [DataRow("10+(8*9)/0+7", "Expression contains division by zero")] @@ -63,6 +64,7 @@ public void ErrorResultOnInvalidKeywordQuery(string typedString, string expected [DataRow("2^96")] [DataRow("+()")] [DataRow("[10,10]")] + [DataRow("sqrt(-1)")] [DataRow("5/0")] [DataRow("5 / 0")] [DataRow("10+(8*9)/0+7")] diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateEngine.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateEngine.cs index f44a72676c7b..a4c7b3025044 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateEngine.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/CalculateEngine.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Numerics; using System.Text.RegularExpressions; using Mages.Core; @@ -122,6 +123,11 @@ private static dynamic TransformResult(object result) return Properties.Resources.wox_plugin_calculator_double_array_returned; } + if (result is Complex) + { + return Properties.Resources.wox_plugin_calculator_complex_number_returned; + } + return result; } diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.Designer.cs b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.Designer.cs index 861993dbdd33..bb7efc291aba 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.Designer.cs +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.Designer.cs @@ -68,6 +68,15 @@ public static string wox_plugin_calculator_calculation_failed { return ResourceManager.GetString("wox_plugin_calculator_calculation_failed", resourceCulture); } } + + /// + /// Looks up a localized string similar to Complex numbers are not supported. + /// + public static string wox_plugin_calculator_complex_number_returned { + get { + return ResourceManager.GetString("wox_plugin_calculator_complex_number_returned", resourceCulture); + } + } /// /// Looks up a localized string similar to Copy failed, please try later. diff --git a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.resx b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.resx index 3c56eedf4b5d..7f913084d2d1 100644 --- a/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.resx +++ b/src/modules/launcher/Plugins/Microsoft.PowerToys.Run.Plugin.Calculator/Properties/Resources.resx @@ -141,6 +141,9 @@ Unsupported use of square brackets + + Complex numbers are not supported + Result value was either too large or too small for a decimal number @@ -187,4 +190,4 @@ Gradians Text for angle unit. - \ No newline at end of file +