mirror of
https://github.com/n00mkrad/flowframes.git
synced 2025-12-21 10:49:25 +01:00
Possibly fixed Fraction division-by-zero exception? (can't reproduce)
This commit is contained in:
@@ -111,20 +111,27 @@ namespace Flowframes.Data
|
|||||||
//Reduce the fraction to lowest terms
|
//Reduce the fraction to lowest terms
|
||||||
Fraction modifiedFraction = this;
|
Fraction modifiedFraction = this;
|
||||||
|
|
||||||
//While the numerator and denominator share a greatest common denominator,
|
try
|
||||||
//keep dividing both by it
|
|
||||||
int gcd = 0;
|
|
||||||
while (Math.Abs(gcd = getGCD(modifiedFraction.Numerator, modifiedFraction.Denominator)) != 1)
|
|
||||||
{
|
{
|
||||||
modifiedFraction.Numerator /= gcd;
|
//While the numerator and denominator share a greatest common denominator,
|
||||||
modifiedFraction.Denominator /= gcd;
|
//keep dividing both by it
|
||||||
}
|
int gcd = 0;
|
||||||
|
while (Math.Abs(gcd = getGCD(modifiedFraction.Numerator, modifiedFraction.Denominator)) != 1)
|
||||||
|
{
|
||||||
|
modifiedFraction.Numerator /= gcd;
|
||||||
|
modifiedFraction.Denominator /= gcd;
|
||||||
|
}
|
||||||
|
|
||||||
//Make sure only a single negative sign is on the numerator
|
//Make sure only a single negative sign is on the numerator
|
||||||
if (modifiedFraction.Denominator < 0)
|
if (modifiedFraction.Denominator < 0)
|
||||||
|
{
|
||||||
|
modifiedFraction.Numerator = -this.Numerator;
|
||||||
|
modifiedFraction.Denominator = -this.Denominator;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
modifiedFraction.Numerator = -this.Numerator;
|
Logger.Log($"Failed to reduce fraction ({modifiedFraction}): {e.Message}", true);
|
||||||
modifiedFraction.Denominator = -this.Denominator;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return modifiedFraction;
|
return modifiedFraction;
|
||||||
|
|||||||
Reference in New Issue
Block a user