| 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 |
#ifndef CORE_PHLOAT_H |
| 19 |
#define CORE_PHLOAT_H 1 |
| 20 |
|
| 21 |
|
| 22 |
#include <stddef.h> |
| 23 |
|
| 24 |
#include "free42.h" |
| 25 |
#ifdef BCD_MATH |
| 26 |
#include "bid_conf.h" |
| 27 |
#include "bid_functions.h" |
| 28 |
#endif |
| 29 |
|
| 30 |
|
| 31 |
#ifdef BCD_MATH |
| 32 |
#define MAX_MANT_DIGITS 34 |
| 33 |
#define ALWAYS_INT_FROM (pow(phloat(10), MAX_MANT_DIGITS)) |
| 34 |
#else |
| 35 |
#define MAX_MANT_DIGITS 17 |
| 36 |
#define ALWAYS_INT_FROM ((double) (1LL << 53)) |
| 37 |
#endif |
| 38 |
|
| 39 |
|
| 40 |
#ifndef BCD_MATH |
| 41 |
|
| 42 |
|
| 43 |
#define phloat double |
| 44 |
|
| 45 |
#define p_isinf(x) (isinf(x) ? (x) > 0 ? 1 : -1 : 0) |
| 46 |
#define p_isnan isnan |
| 47 |
#define p_isnormal isnormal |
| 48 |
#define to_digit(x) ((int) fmod((x), 10.0)) |
| 49 |
#define to_char(x) ((char) (x)) |
| 50 |
#define to_int(x) ((int) (x)) |
| 51 |
#define to_int4(x) ((int4) (x)) |
| 52 |
#define to_int8(x) ((int8) (x)) |
| 53 |
#define to_uint8(x) ((uint8) (x)) |
| 54 |
#define to_double(x) ((double) (x)) |
| 55 |
|
| 56 |
#ifdef HAVE_SINCOS |
| 57 |
#define p_sincos sincos |
| 58 |
#else |
| 59 |
#define p_sincos(x, s, c) { *(s) = sin(x); *(c) = cos(x); } |
| 60 |
#endif |
| 61 |
|
| 62 |
#define PI 3.1415926535897932384626433 |
| 63 |
#define P 7 |
| 64 |
|
| 65 |
double decimal2double(void *data, bool pin_magnitude = false); |
| 66 |
|
| 67 |
|
| 68 |
#else // BCD_MATH |
| 69 |
|
| 70 |
|
| 71 |
#define phloat Phloat |
| 72 |
|
| 73 |
class Phloat { |
| 74 |
public: |
| 75 |
BID_UINT128 val; |
| 76 |
|
| 77 |
Phloat() {} |
| 78 |
Phloat(const BID_UINT128 &b) : val(b) {} |
| 79 |
Phloat(const char *str); |
| 80 |
Phloat(int numer, int denom); |
| 81 |
Phloat(int8 numer, int8 denom); |
| 82 |
Phloat(int i); |
| 83 |
Phloat(int8 i); |
| 84 |
Phloat(uint8 i); |
| 85 |
Phloat(double d); |
| 86 |
Phloat(const Phloat &p); |
| 87 |
Phloat operator=(const BID_UINT128 &b) { val = b; return *this; } |
| 88 |
Phloat operator=(int i); |
| 89 |
Phloat operator=(int8 i); |
| 90 |
Phloat operator=(uint8 i); |
| 91 |
Phloat operator=(double d); |
| 92 |
Phloat operator=(Phloat p); |
| 93 |
void assign17digits(double d); |
| 94 |
bool operator==(Phloat p) const; |
| 95 |
bool operator!=(Phloat p) const; |
| 96 |
bool operator<(Phloat p) const; |
| 97 |
bool operator<=(Phloat p) const; |
| 98 |
bool operator>(Phloat p) const; |
| 99 |
bool operator>=(Phloat p) const; |
| 100 |
Phloat operator-() const; |
| 101 |
Phloat operator*(Phloat p) const; |
| 102 |
Phloat operator/(Phloat p) const; |
| 103 |
Phloat operator+(Phloat p) const; |
| 104 |
Phloat operator-(Phloat p) const; |
| 105 |
Phloat operator*=(Phloat p); |
| 106 |
Phloat operator/=(Phloat p); |
| 107 |
Phloat operator+=(Phloat p); |
| 108 |
Phloat operator-=(Phloat p); |
| 109 |
Phloat operator++(); // prefix |
| 110 |
Phloat operator++(int); // postfix |
| 111 |
Phloat operator--(); // prefix |
| 112 |
Phloat operator--(int); // postfix |
| 113 |
}; |
| 114 |
|
| 115 |
// I can't simply overload isinf() and isnan(), because the Linux math.h |
| 116 |
// defines them as macros. |
| 117 |
int p_isinf(Phloat p); |
| 118 |
int p_isnan(Phloat p); |
| 119 |
int p_isnormal(Phloat p); |
| 120 |
|
| 121 |
// We don't define type cast operators, because they just lead |
| 122 |
// to tons of ambiguities. Defining explicit conversions instead. |
| 123 |
// Note that these conversion routines assume that the value to be |
| 124 |
// converted actually fits in the returned type; if not, the result |
| 125 |
// is undefined, except for to_char(), which will handle the range |
| 126 |
// -128..255 correctly. |
| 127 |
int to_digit(Phloat p); // Returns digit in units position |
| 128 |
char to_char(Phloat p); |
| 129 |
int to_int(Phloat p); |
| 130 |
int4 to_int4(Phloat p); |
| 131 |
int8 to_int8(Phloat p); |
| 132 |
uint8 to_uint8(Phloat p); |
| 133 |
double to_double(Phloat p); |
| 134 |
|
| 135 |
Phloat sin(Phloat p); |
| 136 |
Phloat cos(Phloat p); |
| 137 |
Phloat tan(Phloat p); |
| 138 |
Phloat asin(Phloat p); |
| 139 |
Phloat acos(Phloat p); |
| 140 |
Phloat atan(Phloat p); |
| 141 |
void p_sincos(Phloat phi, Phloat *s, Phloat *c); |
| 142 |
Phloat hypot(Phloat x, Phloat y); |
| 143 |
Phloat atan2(Phloat x, Phloat y); |
| 144 |
Phloat sinh(Phloat p); |
| 145 |
Phloat cosh(Phloat p); |
| 146 |
Phloat tanh(Phloat p); |
| 147 |
Phloat asinh(Phloat p); |
| 148 |
Phloat acosh(Phloat p); |
| 149 |
Phloat atanh(Phloat p); |
| 150 |
Phloat log(Phloat p); |
| 151 |
Phloat log1p(Phloat p); |
| 152 |
Phloat log10(Phloat p); |
| 153 |
Phloat exp(Phloat p); |
| 154 |
Phloat expm1(Phloat p); |
| 155 |
Phloat tgamma(Phloat p); |
| 156 |
Phloat sqrt(Phloat p); |
| 157 |
Phloat fmod(Phloat x, Phloat y); |
| 158 |
Phloat fabs(Phloat p); |
| 159 |
Phloat pow(Phloat x, Phloat y); |
| 160 |
Phloat floor(Phloat x); |
| 161 |
Phloat fma(Phloat x, Phloat y, Phloat z); |
| 162 |
Phloat nextafter(Phloat x, Phloat y); |
| 163 |
int ilogb(Phloat x); |
| 164 |
Phloat scalbn(Phloat x, int y); |
| 165 |
Phloat copysign(Phloat x, Phloat y); |
| 166 |
|
| 167 |
Phloat operator*(int x, Phloat y); |
| 168 |
Phloat operator/(int x, Phloat y); |
| 169 |
Phloat operator/(double x, Phloat y); |
| 170 |
Phloat operator+(int x, Phloat y); |
| 171 |
Phloat operator-(int x, Phloat y); |
| 172 |
bool operator==(int4 x, Phloat y); |
| 173 |
|
| 174 |
extern Phloat PI; |
| 175 |
|
| 176 |
|
| 177 |
#endif // BCD_MATH |
| 178 |
|
| 179 |
|
| 180 |
extern phloat POS_HUGE_PHLOAT; |
| 181 |
extern phloat NEG_HUGE_PHLOAT; |
| 182 |
extern phloat POS_TINY_PHLOAT; |
| 183 |
extern phloat NEG_TINY_PHLOAT; |
| 184 |
extern phloat NAN_PHLOAT; |
| 185 |
extern phloat NAN_1_PHLOAT; |
| 186 |
extern phloat NAN_2_PHLOAT; |
| 187 |
|
| 188 |
void phloat_init(); |
| 189 |
int phloat2string(phloat d, char *buf, int buflen, |
| 190 |
int base_mode, int digits, int dispmode, |
| 191 |
int thousandssep, int max_mant_digits = 12, |
| 192 |
const char *format = NULL); |
| 193 |
int string2phloat(const char *buf, int buflen, phloat *d); |
| 194 |
|
| 195 |
|
| 196 |
#endif |