Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Flow.Launcher.Test/Plugins/CalculatorTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Reflection;
using Flow.Launcher.Plugin.Calculator;
Expand Down Expand Up @@ -114,6 +114,10 @@ public void ThousandsSeparatorTest_LargeNumber()
[TestCase(@"sqrt(-1)", "")]
[TestCase(@"log(0)", "")]
[TestCase(@"invalid_expression", "")]
// Floating point precision
[TestCase(@"3000000-2911111.82", "88888.18")]
[TestCase(@"123456789012345", "123456789012345")]
[TestCase(@"9007199254740991", "9007199254740991")]
public void CalculatorTest(string expression, string result)
{
_settings.UseThousandsSeparator = false;
Expand Down
16 changes: 13 additions & 3 deletions Plugins/Flow.Launcher.Plugin.Calculator/Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
Expand Down Expand Up @@ -128,7 +128,18 @@ public List<Result> Query(Query query)

if (!string.IsNullOrEmpty(result.ToString()))
{
decimal roundedResult = Math.Round(Convert.ToDecimal(result), _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero);
double rawValue = Convert.ToDouble(result);
decimal decValue;
if (double.IsNaN(rawValue) || double.IsInfinity(rawValue))
{
decValue = (decimal)rawValue;
}
Comment on lines +133 to +136

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2: The explicit NaN/Infinity branch casts special double values to decimal, which throws an OverflowException instead of handling them. decimal has no representation for NaN or Infinity, so this check gives a false impression of handling while guaranteeing a throw.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At Plugins/Flow.Launcher.Plugin.Calculator/Main.cs, line 133:

<comment>The explicit NaN/Infinity branch casts special double values to `decimal`, which throws an `OverflowException` instead of handling them. `decimal` has no representation for `NaN` or `Infinity`, so this check gives a false impression of handling while guaranteeing a throw.</comment>

<file context>
@@ -130,13 +130,14 @@ public List<Result> Query(Query query)
                     double rawValue = Convert.ToDouble(result);
                     decimal decValue;
-                    try
+                    if (double.IsNaN(rawValue) || double.IsInfinity(rawValue))
                     {
-                        decValue = decimal.Parse(rawValue.ToString("G14", CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture);
</file context>
Suggested change
if (double.IsNaN(rawValue) || double.IsInfinity(rawValue))
{
decValue = (decimal)rawValue;
}
if (double.IsNaN(rawValue) || double.IsInfinity(rawValue))
{
return new List<Result>
{
new Result
{
Title = rawValue.ToString(CultureInfo.InvariantCulture),
IcoPath = IcoPath,
Score = 300
}
};
}

else
{
string format = (rawValue % 1 == 0) ? "G17" : "G14";
decValue = decimal.Parse(rawValue.ToString(format, CultureInfo.InvariantCulture), NumberStyles.Any, CultureInfo.InvariantCulture);
}
decimal roundedResult = Math.Round(decValue, _settings.MaxDecimalPlaces, MidpointRounding.AwayFromZero);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
string newResult = FormatResult(roundedResult);

return
Expand All @@ -138,7 +149,6 @@ public List<Result> Query(Query query)
Title = newResult,
IcoPath = IcoPath,
Score = 300,
// Check context nullability for unit testing
SubTitle = Context == null ? string.Empty : Localize.flowlauncher_plugin_calculator_copy_number_to_clipboard(),
CopyText = newResult,
Action = c =>
Expand Down