forked from github/server
implement fractions
This commit is contained in:
parent
25d3aacffa
commit
158e0646e1
|
@ -56,6 +56,7 @@ strings.c
|
|||
translation.c
|
||||
umlaut.c
|
||||
unicode.c
|
||||
variant.c
|
||||
xml.c
|
||||
)
|
||||
FOREACH(_FILE ${_FILES})
|
||||
|
|
|
@ -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
|
|
@ -1,3 +1,5 @@
|
|||
#pragma once
|
||||
|
||||
#ifndef STRUCT_VARIANT_H
|
||||
#define STRUCT_VARIANT_H
|
||||
#ifdef __cplusplus
|
||||
|
@ -19,6 +21,10 @@ extern "C" {
|
|||
VAR_FLOAT
|
||||
} 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
|
||||
}
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue