-
Notifications
You must be signed in to change notification settings - Fork 6
/
fraction.h
59 lines (43 loc) · 1.62 KB
/
fraction.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
53
54
55
56
57
58
59
/*
fraction.h
SuperCalc
Created by C0deH4cker on 10/21/13.
Copyright (c) 2013 C0deH4cker. All rights reserved.
*/
#ifndef SC_FRACTION_H
#define SC_FRACTION_H
typedef struct Fraction Fraction;
#include "value.h"
#include "generic.h"
ASSUME_NONNULL_BEGIN
struct Fraction {
/* If the fraction's value is negative, the sign will be on the numerator */
long long n;
INVARIANT(d > 0) long long d;
};
/* Constructor */
RETURNS_OWNED Fraction* Fraction_new(long long numerator, INVARIANT(denominator != 0) long long denominator);
/* Destructor */
void Fraction_free(CONSUMED Fraction* _Nullable frac);
/* Copying */
RETURNS_OWNED Fraction* Fraction_copy(const Fraction* frac);
/* In-place simplification */
void Fraction_simplify(INOUT Fraction* frac);
void Fraction_reduce(INOUT Value* frac);
/* Arithmetic operations */
RETURNS_OWNED Value* Fraction_add(const Fraction* a, const Value* b);
RETURNS_OWNED Value* Fraction_sub(const Fraction* a, const Value* b);
RETURNS_OWNED Value* Fraction_mul(const Fraction* a, const Value* b);
RETURNS_OWNED Value* Fraction_div(const Fraction* a, const Value* b);
RETURNS_OWNED Value* Fraction_mod(const Fraction* a, const Value* b);
RETURNS_OWNED Value* Fraction_pow(const Fraction* base, const Value* exp);
RETURNS_OWNED Value* Fraction_rpow(const Fraction* exp, const Value* base);
/* Comparison */
int Fraction_cmp(const Fraction* a, const Value* b);
/* Conversion */
double Fraction_asReal(const Fraction* frac);
/* Printing */
RETURNS_OWNED char* Fraction_repr(const Fraction* frac, bool approx);
RETURNS_OWNED char* Fraction_xml(const Fraction* frac);
ASSUME_NONNULL_END
#endif /* SC_FRACTION_H */