Sitelet https://github.com/SixLabors/ImageSharp/pull/2453/commits/973e6f1fa9e0836ea5a94f1678b721e5da904452
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix implementation and tests
  • Loading branch information
JimBobSquarePants committed Jun 12, 2023
commit 973e6f1fa9e0836ea5a94f1678b721e5da904452
9 changes: 5 additions & 4 deletions src/ImageSharp/Primitives/LongRational.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ public string ToString(IFormatProvider provider)
/// <param name="bestPrecision">Whether to use the best possible precision when parsing the value.</param>
public static LongRational FromDouble(double value, bool bestPrecision)
{
if (value == 0.0)
{
return new LongRational(0, 1);
}

if (double.IsNaN(value))
{
return new LongRational(0, 0);
Expand All @@ -153,10 +158,6 @@ public static LongRational FromDouble(double value, bool bestPrecision)
double df = numerator / (double)denominator;
double epsilon = bestPrecision ? double.Epsilon : .000001;

if(val < epsilon) {
return new LongRational(0, 1);
}

while (Math.Abs(df - val) > epsilon)
{
if (df < val)
Expand Down
45 changes: 26 additions & 19 deletions tests/ImageSharp.Tests/Numerics/RationalTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Six Labors.
// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

namespace SixLabors.ImageSharp.Tests;
Expand All @@ -14,15 +14,15 @@ public class RationalTests
[Fact]
public void AreEqual()
{
var r1 = new Rational(3, 2);
var r2 = new Rational(3, 2);
Rational r1 = new(3, 2);
Rational r2 = new(3, 2);

Assert.Equal(r1, r2);
Assert.True(r1 == r2);

var r3 = new Rational(7.55);
var r4 = new Rational(755, 100);
var r5 = new Rational(151, 20);
Rational r3 = new(7.55);
Rational r4 = new(755, 100);
Rational r5 = new(151, 20);

Assert.Equal(r3, r4);
Assert.Equal(r4, r5);
Expand All @@ -34,23 +34,30 @@ public void AreEqual()
[Fact]
public void AreNotEqual()
{
var first = new Rational(0, 100);
var second = new Rational(100, 100);
Rational first = new(0, 100);
Rational second = new(100, 100);

Assert.NotEqual(first, second);
Assert.True(first != second);
}

/// <summary>
/// Tests the correct FromDouble(0).
/// Tests known out-of-range values.
/// </summary>
[Fact]
public void FromDouble0Non0Denominator()
/// <param name="value">The input value.</param>
/// <param name="numerator">The expected numerator.</param>
/// <param name="denominator">The expected denominator.</param>
[Theory]
[InlineData(0, 0, 1)]
[InlineData(double.NaN, 0, 0)]
[InlineData(double.PositiveInfinity, 1, 0)]
[InlineData(double.NegativeInfinity, 1, 0)]
public void FromDoubleOutOfRange(double value, uint numerator, uint denominator)
{
var r = Rational.FromDouble(0);
Rational r = Rational.FromDouble(value);

Assert.Equal(0, r.Numerator);
Assert.Equal(1, r.Denominator);
Assert.Equal(numerator, r.Numerator);
Assert.Equal(denominator, r.Denominator);
}

/// <summary>
Expand All @@ -59,7 +66,7 @@ public void FromDouble0Non0Denominator()
[Fact]
public void ConstructorAssignsProperties()
{
var rational = new Rational(7, 55);
Rational rational = new(7, 55);
Assert.Equal(7U, rational.Numerator);
Assert.Equal(55U, rational.Denominator);

Expand All @@ -83,15 +90,15 @@ public void ConstructorAssignsProperties()
[Fact]
public void Fraction()
{
var first = new Rational(1.0 / 1600);
var second = new Rational(1.0 / 1600, true);
Rational first = new(1.0 / 1600);
Rational second = new(1.0 / 1600, true);
Assert.False(first.Equals(second));
}

[Fact]
public void ToDouble()
{
var rational = new Rational(0, 0);
Rational rational = new(0, 0);
Assert.Equal(double.NaN, rational.ToDouble());

rational = new Rational(2, 0);
Expand All @@ -101,7 +108,7 @@ public void ToDouble()
[Fact]
public void ToStringRepresentation()
{
var rational = new Rational(0, 0);
Rational rational = new(0, 0);
Assert.Equal("[ Indeterminate ]", rational.ToString());

rational = new Rational(double.PositiveInfinity);
Expand Down