| 1 |
/***************************************************************************** |
| 2 |
* Free42 -- an HP-42S calculator simulator |
| 3 |
* Copyright (C) 2004-2026 Thomas Okken |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License, version 2, |
| 7 |
* as published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program; if not, see http://www.gnu.org/licenses/. |
| 16 |
*****************************************************************************/ |
| 17 |
|
| 18 |
#include <stdlib.h> |
| 19 |
|
| 20 |
#include "core_commands6.h" |
| 21 |
#include "core_helpers.h" |
| 22 |
#include "core_main.h" |
| 23 |
#include "core_math2.h" |
| 24 |
#include "core_sto_rcl.h" |
| 25 |
#include "core_variables.h" |
| 26 |
#include "shell.h" |
| 27 |
|
| 28 |
/********************************************************/ |
| 29 |
/* Implementations of HP-42S built-in functions, part 6 */ |
| 30 |
/********************************************************/ |
| 31 |
|
| 32 |
static int mappable_sin_r(phloat x, phloat *y) { |
| 33 |
if (flags.f.rad) |
| 34 |
*y = sin(x); |
| 35 |
else if (flags.f.grad) |
| 36 |
*y = sin_grad(x); |
| 37 |
else |
| 38 |
*y = sin_deg(x); |
| 39 |
return ERR_NONE; |
| 40 |
} |
| 41 |
|
| 42 |
static int mappable_sin_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 43 |
/* NOTE: DEG/RAD/GRAD mode does not apply here. */ |
| 44 |
if (xim == 0) { |
| 45 |
*yre = sin(xre); |
| 46 |
*yim = 0; |
| 47 |
return ERR_NONE; |
| 48 |
} else if (xre == 0) { |
| 49 |
*yre = 0; |
| 50 |
*yim = sinh(xim); |
| 51 |
return ERR_NONE; |
| 52 |
} |
| 53 |
phloat sinxre, cosxre; |
| 54 |
phloat sinhxim, coshxim; |
| 55 |
int inf; |
| 56 |
p_sincos(xre, &sinxre, &cosxre); |
| 57 |
sinhxim = sinh(xim); |
| 58 |
coshxim = cosh(xim); |
| 59 |
*yre = sinxre * coshxim; |
| 60 |
if ((inf = p_isinf(*yre)) != 0) { |
| 61 |
if (flags.f.range_error_ignore) |
| 62 |
*yre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 63 |
else |
| 64 |
return ERR_OUT_OF_RANGE; |
| 65 |
} |
| 66 |
*yim = cosxre * sinhxim; |
| 67 |
if ((inf = p_isinf(*yim)) != 0) { |
| 68 |
if (flags.f.range_error_ignore) |
| 69 |
*yim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 70 |
else |
| 71 |
return ERR_OUT_OF_RANGE; |
| 72 |
} |
| 73 |
return ERR_NONE; |
| 74 |
} |
| 75 |
|
| 76 |
int docmd_sin(arg_struct *arg) { |
| 77 |
vartype *v; |
| 78 |
int err = map_unary(stack[sp], &v, mappable_sin_r, mappable_sin_c); |
| 79 |
if (err == ERR_NONE) |
| 80 |
unary_result(v); |
| 81 |
return err; |
| 82 |
} |
| 83 |
|
| 84 |
static int mappable_cos_r(phloat x, phloat *y) { |
| 85 |
if (flags.f.rad) |
| 86 |
*y = cos(x); |
| 87 |
else if (flags.f.grad) |
| 88 |
*y = cos_grad(x); |
| 89 |
else |
| 90 |
*y = cos_deg(x); |
| 91 |
return ERR_NONE; |
| 92 |
} |
| 93 |
|
| 94 |
static int mappable_cos_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 95 |
/* NOTE: DEG/RAD/GRAD mode does not apply here. */ |
| 96 |
if (xim == 0) { |
| 97 |
*yre = cos(xre); |
| 98 |
*yim = 0; |
| 99 |
return ERR_NONE; |
| 100 |
} else if (xre == 0) { |
| 101 |
*yre = cosh(xim); |
| 102 |
int inf; |
| 103 |
if ((inf = p_isinf(*yre)) != 0) |
| 104 |
if (flags.f.range_error_ignore) |
| 105 |
*yre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 106 |
else |
| 107 |
return ERR_OUT_OF_RANGE; |
| 108 |
*yim = 0; |
| 109 |
return ERR_NONE; |
| 110 |
} |
| 111 |
|
| 112 |
phloat sinxre, cosxre; |
| 113 |
phloat sinhxim, coshxim; |
| 114 |
int inf; |
| 115 |
p_sincos(xre, &sinxre, &cosxre); |
| 116 |
sinhxim = sinh(xim); |
| 117 |
coshxim = cosh(xim); |
| 118 |
*yre = cosxre * coshxim; |
| 119 |
if ((inf = p_isinf(*yre)) != 0) { |
| 120 |
if (flags.f.range_error_ignore) |
| 121 |
*yre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 122 |
else |
| 123 |
return ERR_OUT_OF_RANGE; |
| 124 |
} |
| 125 |
*yim = -sinxre * sinhxim; |
| 126 |
if ((inf = p_isinf(*yim)) != 0) { |
| 127 |
if (flags.f.range_error_ignore) |
| 128 |
*yim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 129 |
else |
| 130 |
return ERR_OUT_OF_RANGE; |
| 131 |
} |
| 132 |
return ERR_NONE; |
| 133 |
} |
| 134 |
|
| 135 |
int docmd_cos(arg_struct *arg) { |
| 136 |
vartype *v; |
| 137 |
int err = map_unary(stack[sp], &v, mappable_cos_r, mappable_cos_c); |
| 138 |
if (err == ERR_NONE) |
| 139 |
unary_result(v); |
| 140 |
return err; |
| 141 |
} |
| 142 |
|
| 143 |
static int mappable_tan_r(phloat x, phloat *y) { |
| 144 |
return math_tan(x, y, false); |
| 145 |
} |
| 146 |
|
| 147 |
static int mappable_tan_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 148 |
/* NOTE: DEG/RAD/GRAD mode does not apply here. */ |
| 149 |
|
| 150 |
if (xim == 0) { |
| 151 |
*yim = 0; |
| 152 |
return math_tan(xre, yre, true); |
| 153 |
} else if (xre == 0) { |
| 154 |
*yre = 0; |
| 155 |
*yim = tanh(xim); |
| 156 |
return ERR_NONE; |
| 157 |
} |
| 158 |
if (p_isnan(xre) || p_isnan(xim)) { |
| 159 |
*yre = NAN_PHLOAT; |
| 160 |
*yim = NAN_PHLOAT; |
| 161 |
return ERR_NONE; |
| 162 |
} |
| 163 |
|
| 164 |
phloat sinxre, cosxre; |
| 165 |
p_sincos(xre, &sinxre, &cosxre); |
| 166 |
phloat sinhxim = sinh(xim); |
| 167 |
phloat coshxim = cosh(xim); |
| 168 |
phloat d = cosxre * cosxre + sinhxim * sinhxim; |
| 169 |
if (d == 0) { |
| 170 |
if (flags.f.range_error_ignore) { |
| 171 |
*yre = POS_HUGE_PHLOAT; |
| 172 |
*yim = POS_HUGE_PHLOAT; |
| 173 |
return ERR_NONE; |
| 174 |
} else |
| 175 |
return ERR_OUT_OF_RANGE; |
| 176 |
} |
| 177 |
if (p_isinf(d) != 0) { |
| 178 |
*yre = 0; |
| 179 |
*yim = xim < 0 ? -1 : 1; |
| 180 |
return ERR_NONE; |
| 181 |
} |
| 182 |
*yre = sinxre * cosxre / d; |
| 183 |
*yim = sinhxim * coshxim / d; |
| 184 |
return ERR_NONE; |
| 185 |
} |
| 186 |
|
| 187 |
int docmd_tan(arg_struct *arg) { |
| 188 |
vartype *v; |
| 189 |
int err = map_unary(stack[sp], &v, mappable_tan_r, mappable_tan_c); |
| 190 |
if (err == ERR_NONE) |
| 191 |
unary_result(v); |
| 192 |
return err; |
| 193 |
} |
| 194 |
|
| 195 |
static int mappable_asin_r(phloat x, phloat *y) { |
| 196 |
if (x < -1 || x > 1) |
| 197 |
return ERR_INVALID_DATA; |
| 198 |
if (!flags.f.rad) { |
| 199 |
if (x == 1) { |
| 200 |
*y = flags.f.grad ? 100 : 90; |
| 201 |
return ERR_NONE; |
| 202 |
} else if (x == -1) { |
| 203 |
*y = flags.f.grad ? -100 : -90; |
| 204 |
return ERR_NONE; |
| 205 |
} |
| 206 |
} |
| 207 |
*y = rad_to_angle(asin(x)); |
| 208 |
return ERR_NONE; |
| 209 |
} |
| 210 |
|
| 211 |
static int mappable_asin_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 212 |
phloat tre, tim; |
| 213 |
int err = math_asinh(-xim, xre, &tre, &tim); |
| 214 |
*yre = tim; |
| 215 |
*yim = -tre; |
| 216 |
return err; |
| 217 |
} |
| 218 |
|
| 219 |
int docmd_asin(arg_struct *arg) { |
| 220 |
vartype *v; |
| 221 |
if (stack[sp]->type == TYPE_REAL) { |
| 222 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 223 |
if (x < -1 || x > 1) { |
| 224 |
if (flags.f.real_result_only) |
| 225 |
return ERR_INVALID_DATA; |
| 226 |
else { |
| 227 |
if (x > 0) |
| 228 |
v = new_complex(PI / 2, -acosh(x)); |
| 229 |
else |
| 230 |
v = new_complex(-PI / 2, acosh(-x)); |
| 231 |
} |
| 232 |
} else { |
| 233 |
v = new_real(0); |
| 234 |
if (v != NULL) |
| 235 |
mappable_asin_r(x, &((vartype_real *) v)->x); |
| 236 |
} |
| 237 |
if (v == NULL) |
| 238 |
return ERR_INSUFFICIENT_MEMORY; |
| 239 |
} else { |
| 240 |
int err = map_unary(stack[sp], &v, mappable_asin_r, mappable_asin_c); |
| 241 |
if (err != ERR_NONE) |
| 242 |
return err; |
| 243 |
} |
| 244 |
unary_result(v); |
| 245 |
return ERR_NONE; |
| 246 |
} |
| 247 |
|
| 248 |
static int mappable_acos_r(phloat x, phloat *y) { |
| 249 |
if (x < -1 || x > 1) |
| 250 |
return ERR_INVALID_DATA; |
| 251 |
if (!flags.f.rad) |
| 252 |
if (x == 0) { |
| 253 |
*y = flags.f.grad ? 100 : 90; |
| 254 |
return ERR_NONE; |
| 255 |
} else if (x == -1) { |
| 256 |
*y = flags.f.grad ? 200 : 180; |
| 257 |
return ERR_NONE; |
| 258 |
} |
| 259 |
*y = rad_to_angle(acos(x)); |
| 260 |
return ERR_NONE; |
| 261 |
} |
| 262 |
|
| 263 |
static int mappable_acos_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 264 |
phloat tre, tim; |
| 265 |
int err = math_acosh(xre, xim, &tre, &tim); |
| 266 |
if (xim < 0 || xim == 0 && xre > 1) { |
| 267 |
*yre = -tim; |
| 268 |
*yim = tre; |
| 269 |
} else { |
| 270 |
*yre = tim; |
| 271 |
*yim = -tre; |
| 272 |
} |
| 273 |
return err; |
| 274 |
} |
| 275 |
|
| 276 |
int docmd_acos(arg_struct *arg) { |
| 277 |
vartype *v; |
| 278 |
if (stack[sp]->type == TYPE_REAL) { |
| 279 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 280 |
if (x < -1 || x > 1) { |
| 281 |
if (flags.f.real_result_only) |
| 282 |
return ERR_INVALID_DATA; |
| 283 |
else { |
| 284 |
/* TODO: review */ |
| 285 |
if (x > 0) |
| 286 |
v = new_complex(0, acosh(x)); |
| 287 |
else |
| 288 |
v = new_complex(PI, -acosh(-x)); |
| 289 |
} |
| 290 |
} else { |
| 291 |
v = new_real(0); |
| 292 |
if (v != NULL) |
| 293 |
mappable_acos_r(x, &((vartype_real *) v)->x); |
| 294 |
} |
| 295 |
if (v == NULL) |
| 296 |
return ERR_INSUFFICIENT_MEMORY; |
| 297 |
} else { |
| 298 |
int err = map_unary(stack[sp], &v, mappable_acos_r, mappable_acos_c); |
| 299 |
if (err != ERR_NONE) |
| 300 |
return err; |
| 301 |
} |
| 302 |
unary_result(v); |
| 303 |
return ERR_NONE; |
| 304 |
} |
| 305 |
|
| 306 |
static int mappable_atan_r(phloat x, phloat *y) { |
| 307 |
if (!flags.f.rad) { |
| 308 |
if (p_isinf(x)) { |
| 309 |
*y = flags.f.grad ? 100 : 90; |
| 310 |
return ERR_NONE; |
| 311 |
} else if (x == 1) { |
| 312 |
*y = flags.f.grad ? 50 : 45; |
| 313 |
return ERR_NONE; |
| 314 |
} else if (x == -1) { |
| 315 |
*y = flags.f.grad ? -50 : -45; |
| 316 |
return ERR_NONE; |
| 317 |
} |
| 318 |
} |
| 319 |
*y = rad_to_angle(atan(x)); |
| 320 |
return ERR_NONE; |
| 321 |
} |
| 322 |
|
| 323 |
static int mappable_atan_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 324 |
phloat tre, tim; |
| 325 |
int err = math_atanh(xim, -xre, &tre, &tim); |
| 326 |
*yre = -tim; |
| 327 |
*yim = tre; |
| 328 |
return err; |
| 329 |
} |
| 330 |
|
| 331 |
int docmd_atan(arg_struct *arg) { |
| 332 |
vartype *v; |
| 333 |
int err = map_unary(stack[sp], &v, mappable_atan_r, mappable_atan_c); |
| 334 |
if (err == ERR_NONE) |
| 335 |
unary_result(v); |
| 336 |
return err; |
| 337 |
} |
| 338 |
|
| 339 |
static int mappable_log_r(phloat x, phloat *y) { |
| 340 |
if (x <= 0) |
| 341 |
return ERR_INVALID_DATA; |
| 342 |
else { |
| 343 |
*y = log10(x); |
| 344 |
return ERR_NONE; |
| 345 |
} |
| 346 |
} |
| 347 |
|
| 348 |
static int mappable_log_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 349 |
if (xim == 0) { |
| 350 |
if (xre == 0) |
| 351 |
return ERR_INVALID_DATA; |
| 352 |
if (xre > 0) { |
| 353 |
*yre = log10(xre); |
| 354 |
*yim = 0; |
| 355 |
} else { |
| 356 |
*yre = log10(-xre); |
| 357 |
*yim = PI / log(phloat(10.0)); |
| 358 |
} |
| 359 |
return ERR_NONE; |
| 360 |
} else if (xre == 0) { |
| 361 |
if (xim > 0) { |
| 362 |
*yre = log10(xim); |
| 363 |
*yim = PI / log(phloat(100.0)); |
| 364 |
} else { |
| 365 |
*yre = log10(-xim); |
| 366 |
*yim = -PI / log(phloat(100.0)); |
| 367 |
} |
| 368 |
return ERR_NONE; |
| 369 |
} else { |
| 370 |
math_ln(xre, xim, yre, yim); |
| 371 |
phloat s = log(phloat(10.0)); |
| 372 |
*yre /= s; |
| 373 |
*yim /= s; |
| 374 |
return ERR_NONE; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
int docmd_log(arg_struct *arg) { |
| 379 |
if (stack[sp]->type == TYPE_REAL) { |
| 380 |
vartype_real *x = (vartype_real *) stack[sp]; |
| 381 |
if (x->x == 0) |
| 382 |
return ERR_INVALID_DATA; |
| 383 |
else if (x->x < 0) { |
| 384 |
if (flags.f.real_result_only) |
| 385 |
return ERR_INVALID_DATA; |
| 386 |
else { |
| 387 |
vartype *r = new_complex(log10(-x->x), PI / log(phloat(10.0))); |
| 388 |
if (r == NULL) |
| 389 |
return ERR_INSUFFICIENT_MEMORY; |
| 390 |
else { |
| 391 |
unary_result(r); |
| 392 |
return ERR_NONE; |
| 393 |
} |
| 394 |
} |
| 395 |
} else { |
| 396 |
vartype *r = new_real(log10(x->x)); |
| 397 |
if (r == NULL) |
| 398 |
return ERR_INSUFFICIENT_MEMORY; |
| 399 |
else { |
| 400 |
unary_result(r); |
| 401 |
return ERR_NONE; |
| 402 |
} |
| 403 |
} |
| 404 |
} else { |
| 405 |
vartype *v; |
| 406 |
int err = map_unary(stack[sp], &v, mappable_log_r, mappable_log_c); |
| 407 |
if (err == ERR_NONE) |
| 408 |
unary_result(v); |
| 409 |
return err; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
static int mappable_10_pow_x_r(phloat x, phloat *y) { |
| 414 |
*y = pow(10, x); |
| 415 |
if (p_isinf(*y) != 0) { |
| 416 |
if (!flags.f.range_error_ignore) |
| 417 |
return ERR_OUT_OF_RANGE; |
| 418 |
*y = POS_HUGE_PHLOAT; |
| 419 |
} |
| 420 |
return ERR_NONE; |
| 421 |
} |
| 422 |
|
| 423 |
static int mappable_10_pow_x_c(phloat xre, phloat xim, phloat *yre, phloat *yim){ |
| 424 |
int inf; |
| 425 |
phloat h; |
| 426 |
xim *= log(phloat(10.0)); |
| 427 |
if ((inf = p_isinf(xim)) != 0) |
| 428 |
xim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 429 |
h = pow(10, xre); |
| 430 |
if ((inf = p_isinf(h)) == 0) { |
| 431 |
*yre = cos(xim) * h; |
| 432 |
*yim = sin(xim) * h; |
| 433 |
return ERR_NONE; |
| 434 |
} else if (flags.f.range_error_ignore) { |
| 435 |
phloat t = cos(xim); |
| 436 |
if (t == 0) |
| 437 |
*yre = 0; |
| 438 |
else if (t < 0) |
| 439 |
*yre = inf < 0 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 440 |
else |
| 441 |
*yre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 442 |
t = sin(xim); |
| 443 |
if (t == 0) |
| 444 |
*yim = 0; |
| 445 |
else if (t < 0) |
| 446 |
*yim = inf < 0 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 447 |
else |
| 448 |
*yim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 449 |
return ERR_NONE; |
| 450 |
} else |
| 451 |
return ERR_OUT_OF_RANGE; |
| 452 |
} |
| 453 |
|
| 454 |
int docmd_10_pow_x(arg_struct *arg) { |
| 455 |
vartype *v; |
| 456 |
int err = map_unary(stack[sp], &v, mappable_10_pow_x_r, |
| 457 |
mappable_10_pow_x_c); |
| 458 |
if (err == ERR_NONE) |
| 459 |
unary_result(v); |
| 460 |
return err; |
| 461 |
} |
| 462 |
|
| 463 |
static int mappable_ln_r(phloat x, phloat *y) { |
| 464 |
if (x <= 0) |
| 465 |
return ERR_INVALID_DATA; |
| 466 |
else { |
| 467 |
*y = log(x); |
| 468 |
return ERR_NONE; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
int docmd_ln(arg_struct *arg) { |
| 473 |
if (stack[sp]->type == TYPE_REAL) { |
| 474 |
vartype_real *x = (vartype_real *) stack[sp]; |
| 475 |
if (x->x == 0) |
| 476 |
return ERR_INVALID_DATA; |
| 477 |
else if (x->x < 0) { |
| 478 |
if (flags.f.real_result_only) |
| 479 |
return ERR_INVALID_DATA; |
| 480 |
else { |
| 481 |
vartype *r = new_complex(log(-x->x), PI); |
| 482 |
if (r == NULL) |
| 483 |
return ERR_INSUFFICIENT_MEMORY; |
| 484 |
else { |
| 485 |
unary_result(r); |
| 486 |
return ERR_NONE; |
| 487 |
} |
| 488 |
} |
| 489 |
} else { |
| 490 |
vartype *r = new_real(log(x->x)); |
| 491 |
if (r == NULL) |
| 492 |
return ERR_INSUFFICIENT_MEMORY; |
| 493 |
else { |
| 494 |
unary_result(r); |
| 495 |
return ERR_NONE; |
| 496 |
} |
| 497 |
} |
| 498 |
} else { |
| 499 |
vartype *v; |
| 500 |
int err = map_unary(stack[sp], &v, mappable_ln_r, math_ln); |
| 501 |
if (err == ERR_NONE) |
| 502 |
unary_result(v); |
| 503 |
return err; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
static int mappable_e_pow_x_r(phloat x, phloat *y) { |
| 508 |
*y = exp(x); |
| 509 |
if (p_isinf(*y) != 0) { |
| 510 |
if (!flags.f.range_error_ignore) |
| 511 |
return ERR_OUT_OF_RANGE; |
| 512 |
*y = POS_HUGE_PHLOAT; |
| 513 |
} |
| 514 |
return ERR_NONE; |
| 515 |
} |
| 516 |
|
| 517 |
static int mappable_e_pow_x_c(phloat xre, phloat xim, phloat *yre, phloat *yim){ |
| 518 |
phloat h = exp(xre); |
| 519 |
int inf = p_isinf(h); |
| 520 |
if (inf == 0) { |
| 521 |
*yre = cos(xim) * h; |
| 522 |
*yim = sin(xim) * h; |
| 523 |
return ERR_NONE; |
| 524 |
} else if (flags.f.range_error_ignore) { |
| 525 |
phloat t = cos(xim); |
| 526 |
if (t == 0) |
| 527 |
*yre = 0; |
| 528 |
else if (t < 0) |
| 529 |
*yre = inf < 0 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 530 |
else |
| 531 |
*yre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 532 |
t = sin(xim); |
| 533 |
if (t == 0) |
| 534 |
*yim = 0; |
| 535 |
else if (t < 0) |
| 536 |
*yim = inf < 0 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 537 |
else |
| 538 |
*yim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 539 |
return ERR_NONE; |
| 540 |
} else |
| 541 |
return ERR_OUT_OF_RANGE; |
| 542 |
} |
| 543 |
|
| 544 |
int docmd_e_pow_x(arg_struct *arg) { |
| 545 |
vartype *v; |
| 546 |
int err = map_unary(stack[sp], &v, mappable_e_pow_x_r, mappable_e_pow_x_c); |
| 547 |
if (err == ERR_NONE) |
| 548 |
unary_result(v); |
| 549 |
return err; |
| 550 |
} |
| 551 |
|
| 552 |
static int mappable_sqrt_r(phloat x, phloat *y) { |
| 553 |
if (x < 0) |
| 554 |
return ERR_INVALID_DATA; |
| 555 |
else { |
| 556 |
*y = sqrt(x); |
| 557 |
return ERR_NONE; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
int docmd_sqrt(arg_struct *arg) { |
| 562 |
if (stack[sp]->type == TYPE_REAL) { |
| 563 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 564 |
vartype *v; |
| 565 |
if (x < 0) { |
| 566 |
if (flags.f.real_result_only) |
| 567 |
return ERR_INVALID_DATA; |
| 568 |
v = new_complex(0, sqrt(-x)); |
| 569 |
} else |
| 570 |
v = new_real(sqrt(x)); |
| 571 |
if (v == NULL) |
| 572 |
return ERR_INSUFFICIENT_MEMORY; |
| 573 |
unary_result(v); |
| 574 |
return ERR_NONE; |
| 575 |
} else { |
| 576 |
vartype *v; |
| 577 |
int err = map_unary(stack[sp], &v, mappable_sqrt_r, math_sqrt); |
| 578 |
if (err != ERR_NONE) |
| 579 |
return err; |
| 580 |
unary_result(v); |
| 581 |
return ERR_NONE; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
static int mappable_square_r(phloat x, phloat *y) { |
| 586 |
phloat r = x * x; |
| 587 |
int inf; |
| 588 |
if ((inf = p_isinf(r)) != 0) { |
| 589 |
if (flags.f.range_error_ignore) |
| 590 |
r = inf == 1 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 591 |
else |
| 592 |
return ERR_OUT_OF_RANGE; |
| 593 |
} |
| 594 |
*y = r; |
| 595 |
return ERR_NONE; |
| 596 |
} |
| 597 |
|
| 598 |
static int mappable_square_c(phloat xre, phloat xim, phloat *yre, phloat *yim) { |
| 599 |
phloat rre = xre * xre - xim * xim; |
| 600 |
phloat rim = 2 * xre * xim; |
| 601 |
int inf; |
| 602 |
if ((inf = p_isinf(rre)) != 0) { |
| 603 |
if (flags.f.range_error_ignore) |
| 604 |
rre = inf == 1 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 605 |
else |
| 606 |
return ERR_OUT_OF_RANGE; |
| 607 |
} |
| 608 |
if ((inf = p_isinf(rim)) != 0) { |
| 609 |
if (flags.f.range_error_ignore) |
| 610 |
rim = inf == 1 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 611 |
else |
| 612 |
return ERR_OUT_OF_RANGE; |
| 613 |
} |
| 614 |
*yre = rre; |
| 615 |
*yim = rim; |
| 616 |
return ERR_NONE; |
| 617 |
} |
| 618 |
|
| 619 |
int docmd_square(arg_struct *arg) { |
| 620 |
vartype *v; |
| 621 |
int err = map_unary(stack[sp], &v, mappable_square_r, mappable_square_c); |
| 622 |
if (err == ERR_NONE) |
| 623 |
unary_result(v); |
| 624 |
return err; |
| 625 |
} |
| 626 |
|
| 627 |
static int mappable_inv_r(phloat x, phloat *y) { |
| 628 |
int inf; |
| 629 |
if (x == 0) |
| 630 |
return ERR_DIVIDE_BY_0; |
| 631 |
*y = 1 / x; |
| 632 |
if ((inf = p_isinf(*y)) != 0) { |
| 633 |
if (!flags.f.range_error_ignore) |
| 634 |
return ERR_OUT_OF_RANGE; |
| 635 |
*y = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 636 |
} |
| 637 |
return ERR_NONE; |
| 638 |
} |
| 639 |
|
| 640 |
int docmd_inv(arg_struct *arg) { |
| 641 |
vartype *v; |
| 642 |
int err = map_unary(stack[sp], &v, mappable_inv_r, math_inv); |
| 643 |
if (err == ERR_NONE) |
| 644 |
unary_result(v); |
| 645 |
return err; |
| 646 |
} |
| 647 |
|
| 648 |
int docmd_y_pow_x(arg_struct *arg) { |
| 649 |
phloat yr, yphi; |
| 650 |
int inf; |
| 651 |
vartype *res; |
| 652 |
|
| 653 |
if (false) { |
| 654 |
done: |
| 655 |
if (res == NULL) |
| 656 |
return ERR_INSUFFICIENT_MEMORY; |
| 657 |
else |
| 658 |
return binary_result(res); |
| 659 |
} |
| 660 |
|
| 661 |
if (stack[sp]->type == TYPE_STRING || stack[sp - 1]->type == TYPE_STRING) { |
| 662 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 663 |
} else if (stack[sp]->type != TYPE_REAL |
| 664 |
&& stack[sp]->type != TYPE_COMPLEX |
| 665 |
|| stack[sp - 1]->type != TYPE_REAL |
| 666 |
&& stack[sp - 1]->type != TYPE_COMPLEX) { |
| 667 |
return ERR_INVALID_TYPE; |
| 668 |
} else if (stack[sp]->type == TYPE_REAL) { |
| 669 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 670 |
if (x == to_int4(x)) { |
| 671 |
/* Integer exponent */ |
| 672 |
if (stack[sp - 1]->type == TYPE_REAL) { |
| 673 |
/* Real number to integer power */ |
| 674 |
phloat y = ((vartype_real *) stack[sp - 1])->x; |
| 675 |
if (y == 0 && x <= 0) |
| 676 |
return ERR_INVALID_DATA; |
| 677 |
phloat r = pow(y, x); |
| 678 |
if ((inf = p_isinf(r)) != 0) { |
| 679 |
if (!flags.f.range_error_ignore) |
| 680 |
return ERR_OUT_OF_RANGE; |
| 681 |
r = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 682 |
} |
| 683 |
res = new_real(r); |
| 684 |
goto done; |
| 685 |
} else { |
| 686 |
/* Complex number to integer power */ |
| 687 |
phloat yre = ((vartype_complex *) stack[sp - 1])->re; |
| 688 |
phloat yim = ((vartype_complex *) stack[sp - 1])->im; |
| 689 |
int4 ex = to_int4(x); |
| 690 |
|
| 691 |
if (yim == 0) { |
| 692 |
/* Pure real base */ |
| 693 |
if (yre == 0 && ex <= 0) |
| 694 |
return ERR_INVALID_DATA; |
| 695 |
phloat r = pow(yre, ex); |
| 696 |
if ((inf = p_isinf(r)) != 0) { |
| 697 |
if (!flags.f.range_error_ignore) |
| 698 |
return ERR_OUT_OF_RANGE; |
| 699 |
r = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 700 |
} |
| 701 |
res = new_complex(r, 0); |
| 702 |
goto done; |
| 703 |
} else if (yre == 0) { |
| 704 |
/* Pure imaginary base */ |
| 705 |
phloat r = pow(yim, ex); |
| 706 |
if ((inf = p_isinf(r)) != 0) { |
| 707 |
if (!flags.f.range_error_ignore) |
| 708 |
return ERR_OUT_OF_RANGE; |
| 709 |
r = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 710 |
} |
| 711 |
switch (ex & 3) { |
| 712 |
case 0: res = new_complex( r, 0); break; |
| 713 |
case 1: res = new_complex( 0, r); break; |
| 714 |
case 2: res = new_complex(-r, 0); break; |
| 715 |
case 3: res = new_complex( 0, -r); break; |
| 716 |
} |
| 717 |
goto done; |
| 718 |
} |
| 719 |
|
| 720 |
bool invert = ex < 0; |
| 721 |
if (invert) |
| 722 |
ex = -ex; |
| 723 |
phloat rre, rim; |
| 724 |
|
| 725 |
if (yre == yim || yre == -yim) { |
| 726 |
/* (a±ai)^n => pure real, pure imaginary, or r±ri */ |
| 727 |
#ifdef BCD_MATH |
| 728 |
phloat r = pow(yre * yre * 2, ex / 2); |
| 729 |
if (ex % 2 != 0) |
| 730 |
r *= yre; |
| 731 |
#else |
| 732 |
phloat r = scalbn(pow(yre, ex), ex / 2); |
| 733 |
#endif |
| 734 |
switch (ex % 8) { |
| 735 |
case 0: rre = r; rim = 0; break; |
| 736 |
case 1: rre = r; rim = r; break; |
| 737 |
case 2: rre = 0; rim = r; break; |
| 738 |
case 3: rre = -r; rim = r; break; |
| 739 |
case 4: rre = -r; rim = 0; break; |
| 740 |
case 5: rre = -r; rim = -r; break; |
| 741 |
case 6: rre = 0; rim = -r; break; |
| 742 |
case 7: rre = r; rim = -r; break; |
| 743 |
} |
| 744 |
if (yre != yim) |
| 745 |
rim = -rim; |
| 746 |
goto complex_pow_int_done; |
| 747 |
} |
| 748 |
|
| 749 |
if (x < -256.0 || x > 256.0) |
| 750 |
/* At some point, the cumulative error in repeated squaring |
| 751 |
* becomes larger than the error in the polar power |
| 752 |
* formula, so we only use repeated squaring for smallish |
| 753 |
* exponents. The limit of 256 is somewhat arbitrary, but |
| 754 |
* high enough that whenever possible, the function will |
| 755 |
* return exact results for short bases, i.e. bases where |
| 756 |
* first and last nonzero digits are close together. The |
| 757 |
* smallest non-trivial base is 1+i, which can be raised to |
| 758 |
* the power 225 in the decimal version with exact results. |
| 759 |
*/ |
| 760 |
goto complex_pow_real_1; |
| 761 |
rre = 1; |
| 762 |
rim = 0; |
| 763 |
while (1) { |
| 764 |
phloat tmp; |
| 765 |
if ((ex & 1) != 0) { |
| 766 |
tmp = rre * yre - rim * yim; |
| 767 |
rim = rre * yim + rim * yre; |
| 768 |
rre = tmp; |
| 769 |
if (p_isinf(rre) || p_isnan(rre) || p_isinf(rim) || p_isnan(rim)) |
| 770 |
goto complex_pow_real_1; |
| 771 |
if (rre == 0 && rim == 0) |
| 772 |
if (invert) |
| 773 |
goto complex_pow_real_1; |
| 774 |
else |
| 775 |
break; |
| 776 |
} |
| 777 |
ex >>= 1; |
| 778 |
if (ex == 0) |
| 779 |
break; |
| 780 |
tmp = yre * yre - yim * yim; |
| 781 |
yim = 2 * yre * yim; |
| 782 |
yre = tmp; |
| 783 |
if (p_isinf(yre) || p_isnan(yre) || p_isinf(yim) || p_isnan(yim)) |
| 784 |
goto complex_pow_real_1; |
| 785 |
} |
| 786 |
complex_pow_int_done: |
| 787 |
if (invert) { |
| 788 |
int err = math_inv(rre, rim, &rre, &rim); |
| 789 |
if (err != ERR_NONE) |
| 790 |
return err; |
| 791 |
} |
| 792 |
if ((inf = p_isinf(rre)) != 0) { |
| 793 |
if (!flags.f.range_error_ignore) |
| 794 |
return ERR_OUT_OF_RANGE; |
| 795 |
rre = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 796 |
} |
| 797 |
if ((inf = p_isinf(rim)) != 0) { |
| 798 |
if (!flags.f.range_error_ignore) |
| 799 |
return ERR_OUT_OF_RANGE; |
| 800 |
rim = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 801 |
} |
| 802 |
res = new_complex(rre, rim); |
| 803 |
goto done; |
| 804 |
} |
| 805 |
} else if (stack[sp - 1]->type == TYPE_REAL) { |
| 806 |
/* Real number to noninteger real power */ |
| 807 |
phloat y = ((vartype_real *) stack[sp - 1])->x; |
| 808 |
phloat r; |
| 809 |
if (y < 0) { |
| 810 |
if (flags.f.real_result_only) |
| 811 |
return ERR_INVALID_DATA; |
| 812 |
yr = -y; |
| 813 |
yphi = PI; |
| 814 |
goto complex_pow_real_2; |
| 815 |
} |
| 816 |
r = pow(y, x); |
| 817 |
inf = p_isinf(r); |
| 818 |
if (inf != 0) { |
| 819 |
if (!flags.f.range_error_ignore) |
| 820 |
return ERR_OUT_OF_RANGE; |
| 821 |
r = inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 822 |
} |
| 823 |
res = new_real(r); |
| 824 |
goto done; |
| 825 |
} else { |
| 826 |
/* Complex (or negative real) number to noninteger real power */ |
| 827 |
complex_pow_real_1: |
| 828 |
{ |
| 829 |
phloat yre = ((vartype_complex *) stack[sp - 1])->re; |
| 830 |
phloat yim = ((vartype_complex *) stack[sp - 1])->im; |
| 831 |
yr = hypot(yre, yim); |
| 832 |
if (yim == 0) |
| 833 |
yphi = yre >= 0 ? 0 : PI; |
| 834 |
else |
| 835 |
yphi = atan2(yim, yre); |
| 836 |
} |
| 837 |
complex_pow_real_2: |
| 838 |
yr = pow(yr, x); |
| 839 |
yphi *= x; |
| 840 |
phloat rre, rim; |
| 841 |
if ((inf = p_isinf(yr)) != 0) { |
| 842 |
if (!flags.f.range_error_ignore) |
| 843 |
return ERR_OUT_OF_RANGE; |
| 844 |
else { |
| 845 |
phloat re, im; |
| 846 |
p_sincos(yphi, &im, &re); |
| 847 |
rre = re == 0 ? 0 : |
| 848 |
re < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT; |
| 849 |
rim = im == 0 ? 0 : |
| 850 |
im < 0 ? POS_HUGE_PHLOAT : NEG_HUGE_PHLOAT; |
| 851 |
} |
| 852 |
} else { |
| 853 |
phloat re, im; |
| 854 |
p_sincos(yphi, &im, &re); |
| 855 |
rre = yr * re; |
| 856 |
rim = yr * im; |
| 857 |
} |
| 858 |
res = new_complex(rre, rim); |
| 859 |
goto done; |
| 860 |
} |
| 861 |
} else { |
| 862 |
/* Real or complex number to complex power */ |
| 863 |
phloat xre = ((vartype_complex *) stack[sp])->re; |
| 864 |
phloat xim = ((vartype_complex *) stack[sp])->im; |
| 865 |
phloat yre, yim; |
| 866 |
phloat lre, lim; |
| 867 |
phloat tmp; |
| 868 |
int err; |
| 869 |
if (stack[sp - 1]->type == TYPE_REAL) { |
| 870 |
yre = ((vartype_real *) stack[sp - 1])->x; |
| 871 |
yim = 0; |
| 872 |
} else { |
| 873 |
yre = ((vartype_complex *) stack[sp - 1])->re; |
| 874 |
yim = ((vartype_complex *) stack[sp - 1])->im; |
| 875 |
} |
| 876 |
if (yre == 0 && yim == 0) { |
| 877 |
if (xre <= 0) |
| 878 |
return ERR_INVALID_DATA; |
| 879 |
else |
| 880 |
res = new_complex(0, 0); |
| 881 |
goto done; |
| 882 |
} |
| 883 |
err = math_ln(yre, yim, &lre, &lim); |
| 884 |
if (err != ERR_NONE) |
| 885 |
return err; |
| 886 |
tmp = lre * xre - lim * xim; |
| 887 |
lim = lre * xim + lim * xre; |
| 888 |
lre = tmp; |
| 889 |
err = mappable_e_pow_x_c(lre, lim, &xre, &xim); |
| 890 |
if (err != ERR_NONE) |
| 891 |
return err; |
| 892 |
res = new_complex(xre, xim); |
| 893 |
goto done; |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
int docmd_anum(arg_struct *arg) { |
| 898 |
phloat res; |
| 899 |
if (!anum(reg_alpha, reg_alpha_length, &res)) |
| 900 |
return ERR_NONE; |
| 901 |
vartype *v = new_real(res); |
| 902 |
if (v == NULL) |
| 903 |
return ERR_INSUFFICIENT_MEMORY; |
| 904 |
flags.f.numeric_data_input = 1; |
| 905 |
return recall_result(v); |
| 906 |
} |
| 907 |
|
| 908 |
int docmd_x_swap_f(arg_struct *arg) { |
| 909 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 910 |
if (x < 0) |
| 911 |
x = -x; |
| 912 |
if (x >= 256) |
| 913 |
return ERR_INVALID_DATA; |
| 914 |
int f = 0; |
| 915 |
for (int i = 7; i >= 0; i--) |
| 916 |
f = (f << 1) | flags.farray[i]; |
| 917 |
int nf = to_int(x); |
| 918 |
for (int i = 0; i < 8; i++) { |
| 919 |
flags.farray[i] = nf & 1; |
| 920 |
nf >>= 1; |
| 921 |
} |
| 922 |
((vartype_real *) stack[sp])->x = f; |
| 923 |
return ERR_NONE; |
| 924 |
} |
| 925 |
|
| 926 |
int docmd_rclflag(arg_struct *arg) { |
| 927 |
uint8 lfs = 0, hfs = 0; |
| 928 |
uint8 p = 1; |
| 929 |
for (int i = 0; i < 50; i++) { |
| 930 |
int j = i + 50; |
| 931 |
char lf = virtual_flags[i] == '1' ? virtual_flag_handler(FLAGOP_FS_T, i) == ERR_YES : flags.farray[i] != 0; |
| 932 |
char hf = virtual_flags[j] == '1' ? virtual_flag_handler(FLAGOP_FS_T, j) == ERR_YES : flags.farray[j] != 0; |
| 933 |
if (lf) |
| 934 |
lfs += p; |
| 935 |
if (hf) |
| 936 |
hfs += p; |
| 937 |
p <<= 1; |
| 938 |
} |
| 939 |
#ifdef BCD_MATH |
| 940 |
vartype *v = new_complex(lfs, hfs); |
| 941 |
#else |
| 942 |
/* Silence warning about possible loss of precision in VS2019. |
| 943 |
* The warning is correct, in that we're assigning 64-bit integers |
| 944 |
* to floating-point types with only 53-bit mantissas; but it just |
| 945 |
* happens to be the case that we know we're only using 50 bits |
| 946 |
* so this really is okay. |
| 947 |
*/ |
| 948 |
vartype *v = new_complex((double) lfs, (double) hfs); |
| 949 |
#endif |
| 950 |
if (v == NULL) |
| 951 |
return ERR_INSUFFICIENT_MEMORY; |
| 952 |
return recall_result(v); |
| 953 |
} |
| 954 |
|
| 955 |
int docmd_stoflag(arg_struct *arg) { |
| 956 |
if (stack[sp]->type == TYPE_STRING) |
| 957 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 958 |
if (stack[sp]->type != TYPE_REAL && stack[sp]->type != TYPE_COMPLEX) |
| 959 |
return ERR_INVALID_DATA; |
| 960 |
vartype_complex *c; |
| 961 |
int b, e; |
| 962 |
if (stack[sp]->type == TYPE_COMPLEX) { |
| 963 |
c = (vartype_complex *) stack[sp]; |
| 964 |
b = 0; |
| 965 |
e = 99; |
| 966 |
} else { |
| 967 |
if (stack[sp - 1]->type == TYPE_STRING) |
| 968 |
return ERR_ALPHA_DATA_IS_INVALID; |
| 969 |
if (stack[sp - 1]->type != TYPE_COMPLEX) |
| 970 |
return ERR_INVALID_DATA; |
| 971 |
c = (vartype_complex *) stack[sp - 1]; |
| 972 |
phloat x = ((vartype_real *) stack[sp])->x; |
| 973 |
if (x < 0) |
| 974 |
x = -x; |
| 975 |
if (x >= 100) |
| 976 |
return ERR_INVALID_DATA; |
| 977 |
b = to_int(x); |
| 978 |
x = (x - b) * 100; |
| 979 |
#ifndef BCD_MATH |
| 980 |
x = x + 0.0000000005; |
| 981 |
#endif |
| 982 |
e = to_int(x); |
| 983 |
if (e > 99) |
| 984 |
e = 99; |
| 985 |
if (e < b) |
| 986 |
e = b; |
| 987 |
} |
| 988 |
|
| 989 |
char old_g = !flags.f.rad && flags.f.grad; |
| 990 |
char old_rad = flags.f.rad || flags.f.grad; |
| 991 |
|
| 992 |
phloat lo = c->re; |
| 993 |
phloat hi = c->im; |
| 994 |
if (lo < 0) |
| 995 |
lo = -lo; |
| 996 |
if (hi < 0) |
| 997 |
hi = -hi; |
| 998 |
if (lo >= 1LL << 50 || hi >= 1LL << 50) |
| 999 |
return ERR_INVALID_DATA; |
| 1000 |
uint8 lfs = to_int8(lo); |
| 1001 |
uint8 hfs = to_int8(hi); |
| 1002 |
uint8 p = 1; |
| 1003 |
for (int i = 0; i < 50; i++) { |
| 1004 |
int j = i + 50; |
| 1005 |
char lf = virtual_flags[i] == '1' ? 0 : (lfs & p) != 0; |
| 1006 |
char hf = virtual_flags[j] == '1' ? 0 : (hfs & p) != 0; |
| 1007 |
if (i >= b && i <= e) |
| 1008 |
flags.farray[i] = lf; |
| 1009 |
if (j >= b && j <= e && j != 80) |
| 1010 |
flags.farray[j] = hf; |
| 1011 |
p <<= 1; |
| 1012 |
} |
| 1013 |
|
| 1014 |
char new_g = !flags.f.rad && flags.f.grad; |
| 1015 |
char new_rad = flags.f.rad || flags.f.grad; |
| 1016 |
if (new_g == old_g) |
| 1017 |
new_g = -1; |
| 1018 |
if (new_rad == old_rad) |
| 1019 |
new_rad = -1; |
| 1020 |
if (new_g != -1 || new_rad != -1) |
| 1021 |
set_annunciators(-1, -1, -1, -1, new_g, new_rad); |
| 1022 |
|
| 1023 |
if (b <= 30 && e >= 30 && ((lfs >> 30) & 1) == 1) |
| 1024 |
mode_disable_stack_lift = true; |
| 1025 |
|
| 1026 |
return ERR_NONE; |
| 1027 |
} |