1 | // |
---|
2 | // Lol Engine |
---|
3 | // |
---|
4 | // Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net> |
---|
5 | // This program is free software; you can redistribute it and/or |
---|
6 | // modify it under the terms of the Do What The Fuck You Want To |
---|
7 | // Public License, Version 2, as published by Sam Hocevar. See |
---|
8 | // http://sam.zoy.org/projects/COPYING.WTFPL for more details. |
---|
9 | // |
---|
10 | |
---|
11 | // |
---|
12 | // The Real class |
---|
13 | // -------------- |
---|
14 | // |
---|
15 | |
---|
16 | #if !defined __LOL_REAL_H__ |
---|
17 | #define __LOL_REAL_H__ |
---|
18 | |
---|
19 | #include <stdint.h> |
---|
20 | |
---|
21 | namespace lol |
---|
22 | { |
---|
23 | |
---|
24 | class real |
---|
25 | { |
---|
26 | public: |
---|
27 | inline real() { } |
---|
28 | |
---|
29 | real(float f); |
---|
30 | real(double f); |
---|
31 | real(int i); |
---|
32 | real(unsigned int i); |
---|
33 | |
---|
34 | operator float() const; |
---|
35 | operator double() const; |
---|
36 | operator int() const; |
---|
37 | operator unsigned int() const; |
---|
38 | |
---|
39 | real operator +() const; |
---|
40 | real operator -() const; |
---|
41 | real operator +(real const &x) const; |
---|
42 | real operator -(real const &x) const; |
---|
43 | real operator *(real const &x) const; |
---|
44 | real operator /(real const &x) const; |
---|
45 | real &operator +=(real const &x); |
---|
46 | real &operator -=(real const &x); |
---|
47 | real &operator *=(real const &x); |
---|
48 | real &operator /=(real const &x); |
---|
49 | |
---|
50 | real operator <<(int x) const; |
---|
51 | real operator >>(int x) const; |
---|
52 | real &operator <<=(int x); |
---|
53 | real &operator >>=(int x); |
---|
54 | |
---|
55 | bool operator ==(real const &x) const; |
---|
56 | bool operator !=(real const &x) const; |
---|
57 | bool operator <(real const &x) const; |
---|
58 | bool operator >(real const &x) const; |
---|
59 | bool operator <=(real const &x) const; |
---|
60 | bool operator >=(real const &x) const; |
---|
61 | |
---|
62 | bool operator !() const; |
---|
63 | operator bool() const; |
---|
64 | |
---|
65 | /* Trigonometric functions */ |
---|
66 | friend real sin(real const &x); |
---|
67 | friend real cos(real const &x); |
---|
68 | friend real tan(real const &x); |
---|
69 | friend real asin(real const &x); |
---|
70 | friend real acos(real const &x); |
---|
71 | friend real atan(real const &x); |
---|
72 | /* FIXME: atan2 */ |
---|
73 | |
---|
74 | /* Hyperbolic functions */ |
---|
75 | friend real sinh(real const &x); |
---|
76 | friend real cosh(real const &x); |
---|
77 | friend real tanh(real const &x); |
---|
78 | |
---|
79 | /* Exponential and logarithmic functions */ |
---|
80 | friend real exp(real const &x); |
---|
81 | friend real exp2(real const &x); |
---|
82 | friend real log(real const &x); |
---|
83 | friend real log2(real const &x); |
---|
84 | friend real log10(real const &x); |
---|
85 | /* FIXME: frexp ldexp modf */ |
---|
86 | |
---|
87 | /* Power functions */ |
---|
88 | friend real re(real const &x); |
---|
89 | friend real sqrt(real const &x); |
---|
90 | friend real cbrt(real const &x); |
---|
91 | friend real pow(real const &x, real const &y); |
---|
92 | |
---|
93 | /* Rounding, absolute value, remainder etc. */ |
---|
94 | friend real ceil(real const &x); |
---|
95 | friend real copysign(real const &x, real const &y); |
---|
96 | friend real floor(real const &x); |
---|
97 | friend real fabs(real const &x); |
---|
98 | friend real round(real const &x); |
---|
99 | friend real fmod(real const &x, real const &y); |
---|
100 | |
---|
101 | void hexprint() const; |
---|
102 | void print(int ndigits = 150) const; |
---|
103 | |
---|
104 | static real const R_0; |
---|
105 | static real const R_1; |
---|
106 | static real const R_2; |
---|
107 | static real const R_3; |
---|
108 | static real const R_10; |
---|
109 | |
---|
110 | static real const R_E; |
---|
111 | static real const R_LOG2E; |
---|
112 | static real const R_LOG10E; |
---|
113 | static real const R_LN2; |
---|
114 | static real const R_LN10; |
---|
115 | static real const R_PI; |
---|
116 | static real const R_PI_2; |
---|
117 | static real const R_PI_3; |
---|
118 | static real const R_PI_4; |
---|
119 | static real const R_1_PI; |
---|
120 | static real const R_2_PI; |
---|
121 | static real const R_2_SQRTPI; |
---|
122 | static real const R_SQRT2; |
---|
123 | static real const R_SQRT3; |
---|
124 | static real const R_SQRT1_2; |
---|
125 | |
---|
126 | /* XXX: changing this requires tuning real::fres (the number of |
---|
127 | * Newton-Raphson iterations) and real::print (the number of printed |
---|
128 | * digits) */ |
---|
129 | static int const BIGITS = 16; |
---|
130 | static int const BIGIT_BITS = 32; |
---|
131 | |
---|
132 | private: |
---|
133 | uint32_t m_size; |
---|
134 | uint32_t m_signexp; |
---|
135 | uint32_t m_mantissa[BIGITS]; |
---|
136 | }; |
---|
137 | |
---|
138 | } /* namespace lol */ |
---|
139 | |
---|
140 | #endif // __LOL_REAL_H__ |
---|
141 | |
---|