Repository navigation
Expand file tree
/
Copy pathFloat64.cpp
More file actions
269 lines (222 loc) · 4.98 KB
/
Copy pathFloat64.cpp
File metadata and controls
269 lines (222 loc) · 4.98 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/* f64.cpp
* A 64-bit floating-point C++ wrapper for Berkeley SoftFloat.
* Author: Michael Moller
* Date: 2016.
* Inspired by Nick Gammon's BigNumber bc wrapper
*/
#include <stdlib.h>
#include "Float64.h"
// constructor
f64::f64 ()
{
init();
num_ = i32_to_f64(0); // default to zero
} // end of constructor from NULL
/*
// constructor
f64::f64 (float32_t f)
{
init();
num_ = f32_to_f64(f);
} // end of constructor from float
// constructor
// doubles are 32-bit floats on Arduino
f64::f64 (double f)
{
float32_t *fp = (float32_t *)&f;
init();
if(sizeof(double)==4){
num_ = f32_to_f64(*fp);
}
else{
num_ = *(float64_t*)&f;
}
} // end of constructor from double
*/
f64::f64 (float64_t f)
{
init();
num_ = f;
} // end of constructor from float
f64::f64 (const int n) // constructor from int
{
init();
num_ = i32_to_f64(n);
} // end of constructor from int
f64::f64 (const long n) // constructor from int
{
init();
num_ = i32_to_f64(n);
} // end of constructor from int
/*
f64::f64 (const int32_t n) // constructor from int
{
init();
num_ = i32_to_f64(n);
} // end of constructor from int
*/
f64::f64 (const uint32_t n) // constructor from uint
{
init();
num_ = ui32_to_f64(n);
} // end of constructor from int
/*
f64::f64 (const int64_t n) // constructor from int64_t
{
init();
num_ = i64_to_f64(n);
} // end of constructor from int
f64::f64 (const uint64_t n) // constructor from uint64_t
{
init();
num_ = ui64_to_f64(n);
} // end of constructor from int
*/
f64::f64 (const uint32_t hi, const uint32_t lo) // constructor from two uint32_t
{
init();
*(uint32_t *)&num_ = lo;
*(((uint32_t *)&num_)+1) = hi;
} // end of constructor from int
// copy constructor
f64::f64 (const f64 & rhs)
{
init();
if (this != &rhs)
num_ = rhs.num_;
} // end of f64::f64
//operator=
f64 & f64::operator= (const f64 & rhs)
{
// gracefully handle self-assignment (eg. a = a;)
if (this == &rhs )
return *this;
num_ = rhs.num_;
return *this;
} // end of f64::f64 & operator=
f64 & f64::operator= (const float64_t & rhs)
{
num_ = rhs;
return *this;
} // end of f64::f64 & operator=
/*
float64_t f64::s_f64(void)
{
return num_;
}
*/
// destructor - free memory used, if any
f64::~f64 ()
{
//free(str_);
} // end of destructor
// initialize package
void f64::init (void)
{
}
f64::operator int () const
{
return f64_to_i32(num_, softfloat_round_max, 0);
} // end of f64::operator int
#if defined(ARDUINO)
f64::operator long () const
{
return f64_to_i32(num_, softfloat_round_max, 0);
} // end of f64::operator long
#endif
/*
f64::operator double () const
{
return *(double*)&num_;
}
f64::operator float32_t () const
{
return f64_to_f32(num_);
} // end of f64::operator long
*/
f64::operator float64_t () const
{
return num_;
} // end of f64::operator long
f64::operator int64_t () const
{
return f64_to_i64(num_, softfloat_round_max, 0);
} // end of f64::operator long
// add
f64 & f64::operator+= (const f64 & n)
{
num_ = f64_add (num_, n.num_);
return *this;
} // end of f64::operator+=
// subtract
f64 & f64::operator-= (const f64 & n)
{
num_ = f64_sub (num_, n.num_);
return *this;
} // end of f64::operator-=
// divide
f64 & f64::operator/= (const f64 & n)
{
num_ = f64_div (num_, n.num_);
return *this;
} // end of f64::operator/=
// multiply
f64 & f64::operator*= (const f64 & n)
{
num_ = f64_mul (num_, n.num_);
return *this;
} // end of f64::operator*=
// modulo
f64 & f64::operator%= (const f64 & n)
{
num_ = f64_rem (num_, n.num_);
return *this;
} // end of f64::operator%=
// ----------------------------- COMPARISONS ------------------------------
// compare less with another f64
bool f64::operator< (const f64 & rhs) const
{
return f64_lt(num_, rhs.num_);
} // end of f64::operator<
// compare greater with another f64
bool f64::operator> (const f64 & rhs) const
{
//return !f64_le(num_, rhs.num_); //too big
return !(f64_lt(num_, rhs.num_) || f64_eq(num_, rhs.num_));
} // end of f64::operator>
// compare less-or-equal with another f64
bool f64::operator<= (const f64 & rhs) const
{
//return f64_le(num_, rhs.num_); //too big
return f64_lt(num_, rhs.num_) || f64_eq(num_, rhs.num_);
} // end of f64::operator<=
// compare greater-or-equal with another f64
bool f64::operator>= (const f64 & rhs) const
{
return !f64_lt(num_, rhs.num_);
} // end of f64::operator>=
// compare not equal with another f64
bool f64::operator!= (const f64 & rhs) const
{
return !f64_eq(num_, rhs.num_);
} // end of f64::operator!=
// compare equal with another f64
bool f64::operator== (const f64 & rhs) const
{
return f64_eq(num_, rhs.num_);
} // end of f64::operator==
// special comparisons
bool f64::isNegative () const
{
//return f64_lt(num_,i32_to_f64(0));
return signF64UI(num_.v);
} // end of f64::isNegative
bool f64::isZero () const
{
return !num_.v;
//return f64_eq(num_,i32_to_f64(0));
} // end of f64::isZero
int64_t f64::ipart() const
{
return f64_to_i64(num_,softfloat_round_minMag, 0);
}