Skip to content

Commit c09d20e

Browse files
fix for Retional.FromDouble(0)
1 parent 9b3d84f commit c09d20e

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

src/ImageSharp/Primitives/LongRational.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ public static LongRational FromDouble(double value, bool bestPrecision)
153153
double df = numerator / (double)denominator;
154154
double epsilon = bestPrecision ? double.Epsilon : .000001;
155155

156+
if(val < epsilon) {
157+
return new LongRational(0, 1);
158+
}
159+
156160
while (Math.Abs(df - val) > epsilon)
157161
{
158162
if (df < val)
@@ -201,11 +205,6 @@ public LongRational Simplify()
201205
return this;
202206
}
203207

204-
if (this.Numerator == 0)
205-
{
206-
return new LongRational(0, 1);
207-
}
208-
209208
if (this.Numerator == this.Denominator)
210209
{
211210
return new LongRational(1, 1);

src/ImageSharp/Primitives/Rational.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,6 @@ public Rational(double value, bool bestPrecision)
7474

7575
this.Numerator = (uint)rational.Numerator;
7676
this.Denominator = (uint)rational.Denominator;
77-
78-
if(this.Numerator == 0 && this.Denominator == 0)
79-
{
80-
this.Denominator = 1;
81-
}
8277
}
8378

8479
/// <summary>

tests/ImageSharp.Tests/Numerics/RationalTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ public void AreNotEqual()
4141
Assert.True(first != second);
4242
}
4343

44+
/// <summary>
45+
/// Tests the correct FromDouble(0).
46+
/// </summary>
47+
[Fact]
48+
public void FromDouble0Non0Denominator()
49+
{
50+
var r = Rational.FromDouble(0);
51+
52+
Assert.Equal(0, r.Numerator);
53+
Assert.Equal(1, r.Denominator);
54+
}
55+
4456
/// <summary>
4557
/// Tests whether the Rational constructor correctly assign properties.
4658
/// </summary>

0 commit comments

Comments
 (0)