add logic for pure quadratics

This commit is contained in:
hugogogo
2026-05-14 23:11:37 +02:00
parent 840f5bcfdf
commit 6c6accc289
7 changed files with 301 additions and 51 deletions

View File

@@ -10,3 +10,48 @@ bool is_nearly_equal_zero(double num)
return false;
return true;
}
bool has_decimal_part(double num)
{
return (num != (int)num);
}
bool any_has_decimal_part(double *num, size_t len)
{
while (len > 0)
{
if (has_decimal_part(num[len - 1]))
return true;
len--;
}
return false;
}
// find GCD of two integers using euclidean algorithm
int gcd_int(int a, int b)
{
int tmp;
while (b != 0)
{
tmp = b;
b = a % b;
a = tmp;
}
return abs(a);
}
// returns the gcd, and modify arguments with new reduced values
int reduce_fraction(double *numerator, double *denominator)
{
int gcd;
if (any_has_decimal_part((double[]){*numerator, *denominator}, 2))
return 0;
gcd = gcd_int((int)*numerator, (int)*denominator);
*numerator /= gcd;
*denominator /= gcd;
return gcd;
}