diff options
author | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-07-25 21:32:07 +0100 |
---|---|---|
committer | Aryadev Chavali <aryadev@aryadevchavali.com> | 2024-07-26 02:54:02 +0100 |
commit | 0727126061f5ac48984beae72f2f50e92c7e2225 (patch) | |
tree | 25301840dc3f159ea13db7340f6aad99cddee659 /src | |
parent | 6f7be667e2658e3413a5c489611d25e7afc64f0d (diff) | |
download | cw_tree-0727126061f5ac48984beae72f2f50e92c7e2225.tar.gz cw_tree-0727126061f5ac48984beae72f2f50e92c7e2225.tar.bz2 cw_tree-0727126061f5ac48984beae72f2f50e92c7e2225.zip |
Simple fraction structure
Holds numerator and denominator. Can self simplify, but not
automatically.
Diffstat (limited to 'src')
-rw-r--r-- | src/main.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp index 5d7858f..143816a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,9 +14,42 @@ * Description: Entrypoint */ +#include <cstdint> #include <cstdio> #include <raylib.h> +typedef uint64_t word_t; + +struct Fraction +{ + word_t numerator, denominator; + bool is_simplified; + + Fraction(word_t numerator = 0, word_t denominator = 1, + bool is_simplified = false) + : numerator{numerator}, denominator{denominator}, + is_simplified{is_simplified} + { + } + + void simplify(void) + { + // No-Op if already simplified + if (is_simplified) + return; + word_t a = numerator < denominator ? numerator : denominator, + b = numerator < denominator ? denominator : numerator; + // Euclidean algorithm + for (word_t r = b % a; r != 0; b = a, a = r, r = b % a) + continue; + // a will be the gcd + numerator /= a; + denominator /= a; + // Ensure this is a noop after this + is_simplified = true; + } +}; + int main(void) { puts("Hello, world!"); |