-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfloat.h
87 lines (70 loc) · 1.94 KB
/
float.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#ifndef FLOAT_H_
#define FLOAT_H_
#include "basic.h"
// Float representation: Integer part + Floating part
// Ex: 4.78 -> i=4, f=78
template <int INT, int FLT>
struct float_meta
{
enum {
i = INT,
f = FLT
};
};
#define f(A, B) float_meta<A, B>
// Adds two float numbers
template <class F1, class F2>
struct add_meta
{
typedef float_meta<
F1::i + F2::i + (F1::f + F2::f) / 100 ,
(F1::f + F2::f) % 100> value;
};
// Divides two float numbers
template <class F1, class F2>
struct div_meta
{
typedef float_meta<
static_cast<int>((F1::i * 100 + F1::f) / (F2::i * 100 + F2::f)),
static_cast<int>(((F1::i * 100.0 + F1::f) / (F2::i * 100.0 + F2::f)) * 100.0 -
(F1::i * 100 + F1::f) / (F2::i * 100 + F2::f) * 100)> value;
};
// Multiplies two float numbers
template <class F1, class F2>
struct mult_meta
{
typedef float_meta<
static_cast<int>((F1::i + F1::f / 100.f) * (F2::i + F2::f / 100.f)),
static_cast<int>(((F1::i + F1::f / 100.f) *
(F2::i + F2::f / 100.f)) * 100.f -
static_cast<int>((F1::i + F1::f / 100.f) *
(F2::i + F2::f / 100.f)) * 100)> value;
};
// Subtracts two float numbers
template <class F1, class F2>
struct sub_meta
{
typedef float_meta<
static_cast<int>((F1::i + F1::f / 100.f) - (F2::i + F2::f / 100.f)),
static_cast<int>(((F1::i + F1::f / 100.f) -
(F2::i + F2::f / 100.f)) * 100.f -
static_cast<int>((F1::i + F1::f / 100.f) -
(F2::i + F2::f / 100.f)) * 100)> value;
};
// Operator '<' for float numbers
template <class F1, class F2>
struct inf_meta
{
enum {
value = (F1::i * 100 + F1::f) < (F2::i * 100 + F2::f)
};
};
// Operator '==' for float numbers
template <class F1, class F2>
struct equal_float
{
enum {
value = (static_cast<int>(F1::i) == static_cast<int>(F2::i)) && (static_cast<int>(F1::f) == static_cast<int>(F2::f))
};
};
#endif