Skip to content

Commit 3f7bb4e

Browse files
committed
Improved the big rational implementation by checking for values above long.MaxValue and values that have zero as the fractional part.
1 parent 29a6bfd commit 3f7bb4e

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Magick.NET.Core/Types/BigRational.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ public BigRational(double value, bool bestPrecision)
4444
return;
4545
}
4646

47+
if (value >= long.MaxValue)
48+
{
49+
Numerator = long.MaxValue;
50+
Denominator = 1;
51+
return;
52+
}
53+
54+
if (Math.Floor(value) == value)
55+
{
56+
Numerator = (long)value;
57+
Denominator = 1;
58+
return;
59+
}
60+
4761
Numerator = 1;
4862
Denominator = 1;
4963

tests/Magick.NET.Core.Tests/Types/RationalTests/TheConstructor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,14 @@ public void ShouldHandleZeroValue()
6363
Assert.Equal(0U, rational.Numerator);
6464
Assert.Equal(1U, rational.Denominator);
6565
}
66+
67+
[Fact]
68+
public void ShouldClampTheValue()
69+
{
70+
var rational = new Rational(long.MaxValue * 100.0);
71+
72+
Assert.Equal(uint.MaxValue, rational.Numerator);
73+
Assert.Equal(1U, rational.Denominator);
74+
}
6675
}
6776
}

tests/Magick.NET.Core.Tests/Types/SignedRationalTests/TheConstructor.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,14 @@ public void ShouldHandleZeroValue()
6363
Assert.Equal(0, rational.Numerator);
6464
Assert.Equal(1, rational.Denominator);
6565
}
66+
67+
[Fact]
68+
public void ShouldClampTheValue()
69+
{
70+
var rational = new SignedRational(long.MaxValue * 100.0);
71+
72+
Assert.Equal(-1, rational.Numerator);
73+
Assert.Equal(1, rational.Denominator);
74+
}
6675
}
6776
}

0 commit comments

Comments
 (0)