-
Notifications
You must be signed in to change notification settings - Fork 0
/
fractions.h
52 lines (46 loc) · 1.47 KB
/
fractions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
* @file fractions.h
* @brief Definitions for fractions.c
* @see fractions.c
*/
#ifndef FRACTIONS_H
#define FRACTIONS_H
#include <stdbool.h>
/**
* @brief A structure to store fractions.
*
* A fraction is written in the form \f$\frac{a}{b}\f$, where \f$a\f$ is the
* numerator of the fraction and \f$b\f$ its denominator.
*
* Since negative signs cancel each other out, storing one for the whole
* fraction is enough.
*/
/*
* This implementation is sightly less memory-efficient than storing signed
* integers, but it allows for a) more precision and b) less integer overflow
* problems.
*/
struct fraction {
/** Whether the fraction is negative. This is effectively a sign byte.
*/
bool negative;
/** The numerator of the fraction */
unsigned int numerator;
/** The denominator of the fraction */
unsigned int denominator;
};
/**
* @brief Definition of a type from the fraction structure.
* @see struct fraction
*/
typedef struct fraction fraction;
void multiply_fractions(const fraction *const, const fraction *const,
fraction *const);
void substract_fractions(const fraction *const, const fraction *const,
fraction *const);
int compare_fractions(const fraction *const, const fraction *const);
bool simplify_fraction(fraction *const);
void invert_fraction(const fraction *const, fraction *const);
char fraction_sign_as_character(const fraction *const);
void fraction_from_int(int, fraction *const);
#endif /* FRACTIONS_H */