This repository has been archived on 2022-05-21. You can view files and clone it, but cannot push or open issues or pull requests.
LEFTONE/util/math/rational.h
falsycat 84c3a02b9a [RELEASE] u22-v03
This version is submitted to U22 breau.
2020-09-14 00:00:00 +00:00

85 lines
1.1 KiB
C

#pragma once
#include <stdbool.h>
#include <stdint.h>
typedef struct {
int64_t num;
int64_t den;
} rational_t;
#define rational(n, d) ((rational_t) { \
.num = n, \
.den = d, \
})
bool
rational_valid(
const rational_t* x
);
void
rational_add(
rational_t* x,
const rational_t* a,
const rational_t* b
);
void
rational_addeq(
rational_t* x,
const rational_t* a
);
void
rational_sub(
rational_t* x,
const rational_t* a,
const rational_t* b
);
void
rational_subeq(
rational_t* x,
const rational_t* a
);
void
rational_mul(
rational_t* x,
const rational_t* a,
const rational_t* b
);
void
rational_muleq(
rational_t* x,
const rational_t* a
);
void
rational_div(
rational_t* x,
const rational_t* a,
const rational_t* b
);
void
rational_diveq(
rational_t* x,
const rational_t* a
);
void
rational_simplify(
rational_t* x
);
/* Don't forget that this operation may cause a discrepancy. */
void
rational_normalize(
rational_t* x,
int64_t den
);
float
rational_calculate(
const rational_t* x
);