Skip to content

Commit 973e6f1

Browse files
Fix implementation and tests
1 parent 9291581 commit 973e6f1

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

src/ImageSharp/Primitives/LongRational.cs

Lines changed: 5 additions & 4 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);
@@ -153,10 +158,6 @@ public static LongRational FromDouble(double value, bool bestPrecision)
153158
double df = numerator / (double)denominator;
154159
double epsilon = bestPrecision ? double.Epsilon : .000001;
155160

156-
if(val < epsilon) {
157-
return new LongRational(0, 1);
158-
}
159-
160161
while (Math.Abs(df - val) > epsilon)
161162
{
162163
if (df < val)

tests/ImageSharp.Tests/Numerics/RationalTests.cs

Lines changed: 26 additions & 19 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,23 +34,30 @@ 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

4444
/// <summary>
45-
/// Tests the correct FromDouble(0).
45+
/// Tests known out-of-range values.
4646
/// </summary>
47-
[Fact]
48-
public void FromDouble0Non0Denominator()
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)
4956
{
50-
var r = Rational.FromDouble(0);
57+
Rational r = Rational.FromDouble(value);
5158

52-
Assert.Equal(0, r.Numerator);
53-
Assert.Equal(1, r.Denominator);
59+
Assert.Equal(numerator, r.Numerator);
60+
Assert.Equal(denominator, r.Denominator);
5461
}
5562

5663
/// <summary>
@@ -59,7 +66,7 @@ public void FromDouble0Non0Denominator()
5966
[Fact]
6067
public void ConstructorAssignsProperties()
6168
{
62-
var rational = new Rational(7, 55);
69+
Rational rational = new(7, 55);
6370
Assert.Equal(7U, rational.Numerator);
6471
Assert.Equal(55U, rational.Denominator);
6572

@@ -83,15 +90,15 @@ public void ConstructorAssignsProperties()
8390
[Fact]
8491
public void Fraction()
8592
{
86-
var first = new Rational(1.0 / 1600);
87-
var second = new Rational(1.0 / 1600, true);
93+
Rational first = new(1.0 / 1600);
94+
Rational second = new(1.0 / 1600, true);
8895
Assert.False(first.Equals(second));
8996
}
9097

9198
[Fact]
9299
public void ToDouble()
93100
{
94-
var rational = new Rational(0, 0);
101+
Rational rational = new(0, 0);
95102
Assert.Equal(double.NaN, rational.ToDouble());
96103

97104
rational = new Rational(2, 0);
@@ -101,7 +108,7 @@ public void ToDouble()
101108
[Fact]
102109
public void ToStringRepresentation()
103110
{
104-
var rational = new Rational(0, 0);
111+
Rational rational = new(0, 0);
105112
Assert.Equal("[ Indeterminate ]", rational.ToString());
106113

107114
rational = new Rational(double.PositiveInfinity);

0 commit comments

Comments
 (0)