Browse Source

fix for Retional.FromDouble(0)

pull/2453/head
Tigran 3 years ago
parent
commit
c09d20e3e9
  1. 9
      src/ImageSharp/Primitives/LongRational.cs
  2. 5
      src/ImageSharp/Primitives/Rational.cs
  3. 12
      tests/ImageSharp.Tests/Numerics/RationalTests.cs

9
src/ImageSharp/Primitives/LongRational.cs

@ -153,6 +153,10 @@ internal readonly struct LongRational : IEquatable<LongRational>
double df = numerator / (double)denominator; double df = numerator / (double)denominator;
double epsilon = bestPrecision ? double.Epsilon : .000001; double epsilon = bestPrecision ? double.Epsilon : .000001;
if(val < epsilon) {
return new LongRational(0, 1);
}
while (Math.Abs(df - val) > epsilon) while (Math.Abs(df - val) > epsilon)
{ {
if (df < val) if (df < val)
@ -201,11 +205,6 @@ internal readonly struct LongRational : IEquatable<LongRational>
return this; return this;
} }
if (this.Numerator == 0)
{
return new LongRational(0, 1);
}
if (this.Numerator == this.Denominator) if (this.Numerator == this.Denominator)
{ {
return new LongRational(1, 1); return new LongRational(1, 1);

5
src/ImageSharp/Primitives/Rational.cs

@ -74,11 +74,6 @@ public readonly struct Rational : IEquatable<Rational>
this.Numerator = (uint)rational.Numerator; this.Numerator = (uint)rational.Numerator;
this.Denominator = (uint)rational.Denominator; this.Denominator = (uint)rational.Denominator;
if(this.Numerator == 0 && this.Denominator == 0)
{
this.Denominator = 1;
}
} }
/// <summary> /// <summary>

12
tests/ImageSharp.Tests/Numerics/RationalTests.cs

@ -41,6 +41,18 @@ public class RationalTests
Assert.True(first != second); Assert.True(first != second);
} }
/// <summary>
/// Tests the correct FromDouble(0).
/// </summary>
[Fact]
public void FromDouble0Non0Denominator()
{
var r = Rational.FromDouble(0);
Assert.Equal(0, r.Numerator);
Assert.Equal(1, r.Denominator);
}
/// <summary> /// <summary>
/// Tests whether the Rational constructor correctly assign properties. /// Tests whether the Rational constructor correctly assign properties.
/// </summary> /// </summary>

Loading…
Cancel
Save