Legofit
infers population history from nucleotide site patterns.
Functions
rational.c File Reference
#include "rational.h"
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <assert.h>

Functions

long gcd (long x, long y)
 Greatest common divisor: Lame's method.
 
Rational Rational_normalize (Rational x)
 Remove common factors from numerator and denominator. More...
 
Rational Rational_set (long num, long den)
 Set value, normalize.
 
Rational Rational_add (Rational x, Rational y)
 Add.
 
Rational Rational_sub (Rational x, Rational y)
 Subtract.
 
Rational Rational_neg (Rational x)
 Negate.
 
Rational Rational_inv (Rational x)
 Invert.
 
Rational Rational_mul (Rational x, Rational y)
 Multiply Before multiplying, this function first removes the greatest common divisor of (x.num, y.den) and (x.den, y.num). More...
 
Rational Rational_div (Rational x, Rational y)
 Divide.
 
void Rational_pr (Rational x, FILE *fp)
 Print.
 
int Rational_eq (Rational x, Rational y)
 Return 1 if x and y are equal, 0 otherwise.
 
long double Rational_ldbl (Rational x)
 Convert to long double.
 
double Rational_dbl (Rational x)
 Convert to double.
 

Function Documentation

◆ Rational_mul()

Rational Rational_mul ( Rational  x,
Rational  y 
)

Multiply Before multiplying, this function first removes the greatest common divisor of (x.num, y.den) and (x.den, y.num).

This reduces the chance of an integer overflow. No comparable reduction is done to the numerator and denominator of x or of y, because I assume they are already in normal form.

References gcd(), and Rational_normalize().

◆ Rational_normalize()

Rational Rational_normalize ( Rational  x)

Remove common factors from numerator and denominator.

Make denominator positive.

References gcd().

Referenced by Rational_add(), Rational_div(), Rational_mul(), Rational_set(), and Rational_sub().