implement fractions

This commit is contained in:
Enno Rehling 2017-02-24 15:48:49 +01:00
parent 25d3aacffa
commit 158e0646e1
3 changed files with 43 additions and 0 deletions

View File

@ -56,6 +56,7 @@ strings.c
translation.c translation.c
umlaut.c umlaut.c
unicode.c unicode.c
variant.c
xml.c xml.c
) )
FOREACH(_FILE ${_FILES}) FOREACH(_FILE ${_FILES})

36
src/util/variant.c Normal file
View File

@ -0,0 +1,36 @@
#include "variant.h"
#include <limits.h>
static int lcd(int a, int b) {
return a * b;
}
variant frac_add(variant a, variant b)
{
int num, den;
variant v;
den = lcd(a.sa[1], b.sa[1]);
num = a.sa[0] * lcd / a.sa[1] + b.sa[0] * lcd / b.sa[1];
assert(num >= SHRT_MIN && num <= SHRT_MAX);
assert(den >= SHRT_MIN && den <= SHRT_MAX);
v.sa[0] = (short)num;
v.sa[1] = (short)den;
return v;
}
variant frac_mul(variant a, variant b)
{
variant v = a;
return v;
}
variant frac_div(variant a, variant b)
{
variant v = a;
return v;
}
#ifdef __cplusplus
}
#endif
#endif

View File

@ -1,3 +1,5 @@
#pragma once
#ifndef STRUCT_VARIANT_H #ifndef STRUCT_VARIANT_H
#define STRUCT_VARIANT_H #define STRUCT_VARIANT_H
#ifdef __cplusplus #ifdef __cplusplus
@ -19,6 +21,10 @@ extern "C" {
VAR_FLOAT VAR_FLOAT
} variant_type; } variant_type;
variant frac_add(variant a, variant b);
variant frac_mul(variant a, variant b);
variant frac_div(variant a, variant b);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif