Skip to content

Commit f5e4605

Browse files
Merge pull request #2453 from tsardaryanCamenAI/patch-1
bugfix in Rational.cs, when both Nominator and Denominator equals to 0
2 parents 6a25432 + 6f3d8de commit f5e4605

File tree

2 files changed

+37
-18
lines changed

2 files changed

+37
-18
lines changed

src/ImageSharp/Primitives/LongRational.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ public string ToString(IFormatProvider provider)
131131
/// <param name="bestPrecision">Whether to use the best possible precision when parsing the value.</param>
132132
public static LongRational FromDouble(double value, bool bestPrecision)
133133
{
134+
if (value == 0.0)
135+
{
136+
return new LongRational(0, 1);
137+
}
138+
134139
if (double.IsNaN(value))
135140
{
136141
return new LongRational(0, 0);
@@ -201,11 +206,6 @@ public LongRational Simplify()
201206
return this;
202207
}
203208

204-
if (this.Numerator == 0)
205-
{
206-
return new LongRational(0, 0);
207-
}
208-
209209
if (this.Numerator == this.Denominator)
210210
{
211211
return new LongRational(1, 1);

tests/ImageSharp.Tests/Numerics/RationalTests.cs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Six Labors.
1+
// Copyright (c) Six Labors.
22
// Licensed under the Six Labors Split License.
33

44
namespace SixLabors.ImageSharp.Tests;
@@ -14,15 +14,15 @@ public class RationalTests
1414
[Fact]
1515
public void AreEqual()
1616
{
17-
var r1 = new Rational(3, 2);
18-
var r2 = new Rational(3, 2);
17+
Rational r1 = new(3, 2);
18+
Rational r2 = new(3, 2);
1919

2020
Assert.Equal(r1, r2);
2121
Assert.True(r1 == r2);
2222

23-
var r3 = new Rational(7.55);
24-
var r4 = new Rational(755, 100);
25-
var r5 = new Rational(151, 20);
23+
Rational r3 = new(7.55);
24+
Rational r4 = new(755, 100);
25+
Rational r5 = new(151, 20);
2626

2727
Assert.Equal(r3, r4);
2828
Assert.Equal(r4, r5);
@@ -34,20 +34,39 @@ public void AreEqual()
3434
[Fact]
3535
public void AreNotEqual()
3636
{
37-
var first = new Rational(0, 100);
38-
var second = new Rational(100, 100);
37+
Rational first = new(0, 100);
38+
Rational second = new(100, 100);
3939

4040
Assert.NotEqual(first, second);
4141
Assert.True(first != second);
4242
}
4343

44+
/// <summary>
45+
/// Tests known out-of-range values.
46+
/// </summary>
47+
/// <param name="value">The input value.</param>
48+
/// <param name="numerator">The expected numerator.</param>
49+
/// <param name="denominator">The expected denominator.</param>
50+
[Theory]
51+
[InlineData(0, 0, 1)]
52+
[InlineData(double.NaN, 0, 0)]
53+
[InlineData(double.PositiveInfinity, 1, 0)]
54+
[InlineData(double.NegativeInfinity, 1, 0)]
55+
public void FromDoubleOutOfRange(double value, uint numerator, uint denominator)
56+
{
57+
Rational r = Rational.FromDouble(value);
58+
59+
Assert.Equal(numerator, r.Numerator);
60+
Assert.Equal(denominator, r.Denominator);
61+
}
62+
4463
/// <summary>
4564
/// Tests whether the Rational constructor correctly assign properties.
4665
/// </summary>
4766
[Fact]
4867
public void ConstructorAssignsProperties()
4968
{
50-
var rational = new Rational(7, 55);
69+
Rational rational = new(7, 55);
5170
Assert.Equal(7U, rational.Numerator);
5271
Assert.Equal(55U, rational.Denominator);
5372

@@ -71,15 +90,15 @@ public void ConstructorAssignsProperties()
7190
[Fact]
7291
public void Fraction()
7392
{
74-
var first = new Rational(1.0 / 1600);
75-
var second = new Rational(1.0 / 1600, true);
93+
Rational first = new(1.0 / 1600);
94+
Rational second = new(1.0 / 1600, true);
7695
Assert.False(first.Equals(second));
7796
}
7897

7998
[Fact]
8099
public void ToDouble()
81100
{
82-
var rational = new Rational(0, 0);
101+
Rational rational = new(0, 0);
83102
Assert.Equal(double.NaN, rational.ToDouble());
84103

85104
rational = new Rational(2, 0);
@@ -89,7 +108,7 @@ public void ToDouble()
89108
[Fact]
90109
public void ToStringRepresentation()
91110
{
92-
var rational = new Rational(0, 0);
111+
Rational rational = new(0, 0);
93112
Assert.Equal("[ Indeterminate ]", rational.ToString());
94113

95114
rational = new Rational(double.PositiveInfinity);

0 commit comments

Comments
 (0)