-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.cpp
More file actions
273 lines (235 loc) · 6.06 KB
/
Copy pathmath.cpp
File metadata and controls
273 lines (235 loc) · 6.06 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
268
269
270
271
272
//-----------------------------------------------------------------------------
// C++ implementation of a Forth interpreter
// Copyright (c) Paulo Custodio, 2020-2026
// License: GPL3 https://www.gnu.org/licenses/gpl-3.0.html
//-----------------------------------------------------------------------------
#include "errors.h"
#include "forth.h"
#include "math.h"
#include "stack.h"
#include "vm.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <limits>
#include <sstream>
int f_mod(int a, int b) {
if (b == 0) {
error(Error::DivisionByZero);
return 0; // not reached
}
// Handle edge cases where negation does not change the value
else if (b == std::numeric_limits<int>::min()) {
// Only possible remainders are a or a + b, depending on sign
int ret = a % b;
if (ret < 0) {
ret += b;
}
return ret;
}
else if (a == std::numeric_limits<int>::min() && b == -1) {
// Avoid overflow
return 0;
}
else if (b < 0) {
return -f_mod(-a, -b);
}
else {
int ret = a % b;
if (ret < 0) {
ret += b;
}
return ret;
}
}
int f_div(int a, int b) {
int rem = f_mod(a, b);
int quot = (a - rem) / b;
return quot;
}
double f_div(double a, double b) {
return a / b;
}
void f_div_mod() {
int b = pop();
int a = pop();
push(f_mod(a, b));
push(f_div(a, b));
}
dint f_dmod(dint a, dint b) {
if (b == 0) {
error(Error::DivisionByZero);
return 0; // not reached
}
else if (b < 0) {
return -f_dmod(-a, -b);
}
else {
dint ret = a % b;
if (ret < 0) {
ret += b;
}
return ret;
}
}
dint f_ddiv(dint a, dint b) {
dint rem = f_dmod(a, b);
dint quot = (a - rem) / b;
return quot;
}
void f_ddiv_mod() {
dint b = dpop();
dint a = dpop();
dpush(f_dmod(a, b));
dpush(f_ddiv(a, b));
}
void f_fm_div_mod() {
dint n = (dint)pop();
dint d = dpop();
push(static_cast<int>(f_dmod(d, n)));
push(static_cast<int>(f_ddiv(d, n)));
}
void f_sm_div_rem() {
dint n = static_cast<dint>(pop());
dint d = dpop();
if (n == 0) {
error(Error::DivisionByZero);
}
else {
push(static_cast<int>(d % n));
push(static_cast<int>(d / n));
}
}
void f_um_div_mod() {
udint n = static_cast<uint>(pop());
udint d = dpop();
if (n == 0) {
error(Error::DivisionByZero);
}
else {
push(static_cast<int>(d % n));
push(static_cast<int>(d / n));
}
}
static void mul_div_mod(int a, int b, int c, int& quot, int& rem) {
dint prod = (dint)a * (dint)b;
quot = static_cast<int>(f_ddiv(prod, c));
rem = static_cast<int>(f_dmod(prod, c));
}
void f_mul_div_mod() {
int c = pop(), b = pop(), a = pop(), quot, rem;
mul_div_mod(a, b, c, quot, rem);
push(rem);
push(quot);
}
void f_mul_div() {
int c = pop(), b = pop(), a = pop(), quot, rem;
mul_div_mod(a, b, c, quot, rem);
push(quot);
}
bool within(uint x, uint lo, uint hi) {
// implement the same logic as Forth's WITHIN word
return (x - lo) < (hi - lo);
}
void f_within() {
uint hi = pop();
uint lo = pop();
uint x = pop();
push(f_bool(within(x, lo, hi)));
}
void f_um_mult() {
udint b = static_cast<uint>(pop());
udint a = static_cast<uint>(pop());
udint result = a * b;
dpush(result);
}
void f_m_plus() {
dint n = pop();
dint d = dpop();
dpush(d + n);
}
void d_to_f() {
dint d = dpop();
double f = static_cast<double>(d);
fpush(f);
}
void f_to_d() {
double f = fpop();
dint d = static_cast<dint>(std::trunc(f));
dpush(d);
}
void s_to_f() {
int d = pop();
double f = static_cast<double>(d);
fpush(f);
}
void f_to_s() {
double f = fpop();
int d = static_cast<int>(std::trunc(f));
push(d);
}
RepresentResult f_represent(double x, int significant_digits) {
RepresentResult result;
// Handle sign
result.is_negative = std::signbit(x);
double abs_x = std::fabs(x);
// Use scientific notation to extract exponent and digits
std::ostringstream oss;
oss << std::scientific
<< std::setprecision(significant_digits - 1)
<< abs_x;
std::string sci = oss.str(); // e.g., "1.234567890123456e+03"
// Parse digits and exponent
size_t e_pos = sci.find('e');
std::string mantissa = sci.substr(0, e_pos);
std::string exponent_str = sci.substr(e_pos + 1);
// Remove decimal point from mantissa
mantissa.erase(std::remove(mantissa.begin(), mantissa.end(), '.'),
mantissa.end());
result.digits = mantissa;
// Adjust exponent for leading digit
result.exponent = std::stoi(exponent_str) + 1;
return result;
}
void f_represent() {
uint size = pop();
uint addr = pop();
char* buffer = mem_char_ptr(addr, size);
double f = fpop();
RepresentResult res = f_represent(f, size);
memset(buffer, BL, size);
memcpy(buffer, res.digits.c_str(),
std::min(size, static_cast<uint>(res.digits.size())));
push(res.exponent);
push(res.is_negative ? F_TRUE : F_FALSE);
push(F_TRUE); // success
}
bool f_f_tilde() {
double tolerance = fpop();
double b = fpop();
double a = fpop();
return f_f_tilde(a, b, tolerance);
}
// Bitwise comparison for exact encoding (handles +0 vs -0)
static bool bitwise_equal(double a, double b) {
static_assert(sizeof(double) == sizeof(uint64_t),
"Unexpected double size");
uint64_t ua, ub;
std::memcpy(&ua, &a, sizeof(double));
std::memcpy(&ub, &b, sizeof(double));
return ua == ub;
}
bool f_f_tilde(double a, double b, double tolerance) {
if (tolerance > 0.0) {
return std::fabs(a - b) < tolerance;
}
else if (std::fabs(tolerance) < EPSILON) {
return bitwise_equal(a, b);
}
else {
double scaled_tolerance =
std::fabs(tolerance) * (std::fabs(a) + std::fabs(b));
return std::fabs(a - b) < scaled_tolerance;
}
}