Debian Codegraph @

common/core_phloat.cc

raw
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 <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <math.h>
22 #include <float.h>
23
24 #include "core_phloat.h"
25 #include "core_helpers.h"
26 #include "shell.h"
27
28 #ifndef BCD_MATH
29 // We need these locally for BID128->double conversion
30 #include "bid_conf.h"
31 #include "bid_functions.h"
32 #endif
33
34
35 phloat POS_HUGE_PHLOAT;
36 phloat NEG_HUGE_PHLOAT;
37 phloat POS_TINY_PHLOAT;
38 phloat NEG_TINY_PHLOAT;
39 phloat NAN_PHLOAT;
40 phloat NAN_1_PHLOAT;
41 phloat NAN_2_PHLOAT;
42
43
44 #ifdef BCD_MATH
45
46
47 void phloat_init() {
48 BID_UINT128 posinf, neginf, zero, poshuge, neghuge, postiny, negtiny, nan;
49 bid128_from_string(&posinf, (char *) "+Inf");
50 bid128_from_string(&neginf, (char *) "-Inf");
51 int z = 0;
52 bid128_from_int32(&zero, &z);
53 bid128_nextafter(&poshuge, &posinf, &zero);
54 bid128_nextafter(&neghuge, &neginf, &zero);
55 bid128_nextafter(&postiny, &zero, &posinf);
56 bid128_nextafter(&negtiny, &zero, &neginf);
57 POS_HUGE_PHLOAT = poshuge;
58 NEG_HUGE_PHLOAT = neghuge;
59 POS_TINY_PHLOAT = postiny;
60 NEG_TINY_PHLOAT = negtiny;
61 bid128_div(&nan, &zero, &zero);
62 NAN_PHLOAT = nan;
63 bid128_nan(&NAN_1_PHLOAT.val, "1");
64 bid128_nan(&NAN_2_PHLOAT.val, "2");
65 }
66
67 int string2phloat(const char *buf, int buflen, phloat *d) {
68 /* Convert string to phloat.
69 * Return values:
70 * 0: no error
71 * 1: positive overflow
72 * 2: negative overflow
73 * 3: positive underflow
74 * 4: negative underflow
75 * 5: other error
76 */
77
78 // Special case: "-" by itself. bid128_from_string() doesn't like this,
79 // so handling it separately here.
80 if (buflen == 1 && buf[0] == '-') {
81 *d = 0;
82 return 0;
83 }
84
85 // Strip thousands separators, convert comma to dot if
86 // appropriate, and convert char(24) to 'E'.
87 // Also, reject numbers with more than MAX_MANT_DIGITS digits in the mantissa.
88 char buf2[100];
89 int buflen2 = 0;
90 char sep = flags.f.decimal_point ? ',' : '.';
91 int mantdigits = 0;
92 int expdigits = 0;
93 bool in_mant = true;
94 bool zero = true;
95 for (int i = 0; i < buflen; i++) {
96 char c = buf[i];
97 if (c == sep)
98 continue;
99 if (c == ',')
100 c = '.';
101 else if (c == 24) {
102 c = 'E';
103 in_mant = false;
104 } else if (c >= '0' && c <= '9') {
105 if (in_mant) {
106 if (++mantdigits > MAX_MANT_DIGITS)
107 return 5;
108 if (c != '0')
109 zero = false;
110 } else {
111 expdigits++;
112 }
113 }
114 buf2[buflen2++] = c;
115 }
116
117 if (!in_mant && mantdigits == 0) {
118 // A number like "E4"; not something the HP-41 or HP42S (or Free42, for
119 // that matter) will actually allow you to enter into a program, but
120 // the real calcs do accept this kind of thing (synthetically), and
121 // supply a mantissa of 1 (just like when you start number entry by
122 // pressing EEX (HP-41) or E (HP-42S).
123 for (int i = buflen2 - 1; i >= 0; i--)
124 buf2[i + 1] = buf2[i];
125 buf2[0] = '1';
126 buflen2++;
127 }
128
129 if (!in_mant && expdigits == 0)
130 buf2[buflen2++] = '0';
131
132 buf2[buflen2] = 0;
133 BID_UINT128 b;
134 bid128_from_string(&b, buf2);
135 int r;
136 if (bid128_isInf(&r, &b), r)
137 return (bid128_isSigned(&r, &b), r) ? 2 : 1;
138 if (!zero && (bid128_isZero(&r, &b), r))
139 return (bid128_isSigned(&r, &b), r) ? 4 : 3;
140 *d = b;
141 return 0;
142 }
143
144 /* public */
145 Phloat::Phloat(const char *str) {
146 bid128_from_string(&val, (char *) str);
147 }
148
149 /* public */
150 Phloat::Phloat(int numer, int denom) {
151 BID_UINT128 n, d;
152 bid128_from_int32(&n, &numer);
153 bid128_from_int32(&d, &denom);
154 bid128_div(&val, &n, &d);
155 }
156
157 /* public */
158 Phloat::Phloat(int8 numer, int8 denom) {
159 BID_UINT128 n, d;
160 bid128_from_int64(&n, &numer);
161 bid128_from_int64(&d, &denom);
162 bid128_div(&val, &n, &d);
163 }
164
165 /* public */
166 Phloat::Phloat(int i) {
167 bid128_from_int32(&val, &i);
168 }
169
170 /* public */
171 Phloat::Phloat(int8 i) {
172 bid128_from_int64(&val, &i);
173 }
174
175 /* public */
176 Phloat::Phloat(uint8 i) {
177 bid128_from_uint64(&val, &i);
178 }
179
180 /* public */
181 Phloat::Phloat(double d) {
182 BID_UINT64 tmp;
183 binary64_to_bid64(&tmp, &d);
184 bid64_to_bid128(&val, &tmp);
185 }
186
187 /* public */
188 Phloat::Phloat(const Phloat &p) {
189 val = p.val;
190 }
191
192 /* public */
193 Phloat Phloat::operator=(int i) {
194 bid128_from_int32(&val, &i);
195 return *this;
196 }
197
198 /* public */
199 Phloat Phloat::operator=(int8 i) {
200 bid128_from_int64(&val, &i);
201 return *this;
202 }
203
204 /* public */
205 Phloat Phloat::operator=(uint8 i) {
206 bid128_from_uint64(&val, &i);
207 return *this;
208 }
209
210 /* public */
211 Phloat Phloat::operator=(double d) {
212 BID_UINT64 tmp;
213 binary64_to_bid64(&tmp, &d);
214 bid64_to_bid128(&val, &tmp);
215 return *this;
216 }
217
218 /* public */
219 Phloat Phloat::operator=(Phloat p) {
220 val = p.val;
221 return *this;
222 }
223
224 /* public */
225 void Phloat::assign17digits(double d) {
226 if (isinf(d) || isnan(d)) {
227 binary64_to_bid128(&val, &d);
228 } else {
229 char buf[25];
230 snprintf(buf, 25, "%.15e", d);
231 double d2;
232 if (sscanf(buf, "%le", &d2) != 1 || d != d2)
233 snprintf(buf, 25, "%.16e", d);
234 bid128_from_string(&val, buf);
235 }
236 }
237
238 /* public */
239 bool Phloat::operator==(Phloat p) const {
240 int r;
241 bid128_quiet_equal(&r, (BID_UINT128 *) &val, &p.val);
242 return r != 0;
243 }
244
245 /* public */
246 bool Phloat::operator!=(Phloat p) const {
247 int r;
248 bid128_quiet_not_equal(&r, (BID_UINT128 *) &val, &p.val);
249 return r != 0;
250 }
251
252 /* public */
253 bool Phloat::operator<(Phloat p) const {
254 int r;
255 bid128_quiet_less(&r, (BID_UINT128 *) &val, &p.val);
256 return r != 0;
257 }
258
259 /* public */
260 bool Phloat::operator<=(Phloat p) const {
261 int r;
262 bid128_quiet_less_equal(&r, (BID_UINT128 *) &val, &p.val);
263 return r != 0;
264 }
265
266 /* public */
267 bool Phloat::operator>(Phloat p) const {
268 int r;
269 bid128_quiet_greater(&r, (BID_UINT128 *) &val, &p.val);
270 return r != 0;
271 }
272
273 /* public */
274 bool Phloat::operator>=(Phloat p) const {
275 int r;
276 bid128_quiet_greater_equal(&r, (BID_UINT128 *) &val, &p.val);
277 return r != 0;
278 }
279
280 /* public */
281 Phloat Phloat::operator-() const {
282 BID_UINT128 res;
283 bid128_negate(&res, (BID_UINT128 *) &val);
284 return Phloat(res);
285 }
286
287 /* public */
288 Phloat Phloat::operator*(Phloat p) const {
289 BID_UINT128 res;
290 bid128_mul(&res, (BID_UINT128 *) &val, &p.val);
291 return Phloat(res);
292 }
293
294 /* public */
295 Phloat Phloat::operator/(Phloat p) const {
296 BID_UINT128 res;
297 bid128_div(&res, (BID_UINT128 *) &val, &p.val);
298 return Phloat(res);
299 }
300
301 /* public */
302 Phloat Phloat::operator+(Phloat p) const {
303 BID_UINT128 res;
304 bid128_add(&res, (BID_UINT128 *) &val, &p.val);
305 return Phloat(res);
306 }
307
308 /* public */
309 Phloat Phloat::operator-(Phloat p) const {
310 BID_UINT128 res;
311 bid128_sub(&res, (BID_UINT128 *) &val, &p.val);
312 return Phloat(res);
313 }
314
315 /* public */
316 Phloat Phloat::operator*=(Phloat p) {
317 BID_UINT128 res;
318 bid128_mul(&res, &val, &p.val);
319 val = res;
320 return *this;
321 }
322
323 /* public */
324 Phloat Phloat::operator/=(Phloat p) {
325 BID_UINT128 res;
326 bid128_div(&res, &val, &p.val);
327 val = res;
328 return *this;
329 }
330
331 /* public */
332 Phloat Phloat::operator+=(Phloat p) {
333 BID_UINT128 res;
334 bid128_add(&res, &val, &p.val);
335 val = res;
336 return *this;
337 }
338
339 /* public */
340 Phloat Phloat::operator-=(Phloat p) {
341 BID_UINT128 res;
342 bid128_sub(&res, &val, &p.val);
343 val = res;
344 return *this;
345 }
346
347 /* public */
348 Phloat Phloat::operator++() {
349 // prefix
350 BID_UINT128 one;
351 int d1 = 1;
352 bid128_from_int32(&one, &d1);
353 BID_UINT128 temp;
354 bid128_add(&temp, &val, &one);
355 val = temp;
356 return *this;
357 }
358
359 /* public */
360 Phloat Phloat::operator++(int) {
361 // postfix
362 Phloat old = *this;
363 BID_UINT128 one;
364 int d1 = 1;
365 bid128_from_int32(&one, &d1);
366 bid128_add(&val, &old.val, &one);
367 return old;
368 }
369
370 /* public */
371 Phloat Phloat::operator--() {
372 // prefix
373 BID_UINT128 one;
374 int d1 = 1;
375 bid128_from_int32(&one, &d1);
376 BID_UINT128 temp;
377 bid128_sub(&temp, &val, &one);
378 val = temp;
379 return *this;
380 }
381
382 /* public */
383 Phloat Phloat::operator--(int) {
384 // postfix
385 Phloat old = *this;
386 BID_UINT128 one;
387 int d1 = 1;
388 bid128_from_int32(&one, &d1);
389 bid128_sub(&val, &old.val, &one);
390 return old;
391 }
392
393 int p_isinf(Phloat p) {
394 int r;
395 if (bid128_isInf(&r, &p.val), r)
396 return (bid128_isSigned(&r, &p.val), r) ? -1 : 1;
397 else
398 return 0;
399 }
400
401 int p_isnan(Phloat p) {
402 int r;
403 bid128_isNaN(&r, &p.val);
404 return r;
405 }
406
407 int p_isnormal(Phloat p) {
408 int r;
409 bid128_isNormal(&r, &p.val);
410 return r;
411 }
412
413 int to_digit(Phloat p) {
414 BID_UINT128 ten, res;
415 int d10 = 10;
416 int ires;
417 bid128_from_int32(&ten, &d10);
418 bid128_rem(&res, &p.val, &ten);
419 int numer_sign, res_sign;
420 bid128_isSigned(&numer_sign, &p.val);
421 bid128_isSigned(&res_sign, &res);
422 if (numer_sign ^ res_sign) {
423 BID_UINT128 r2;
424 if (res_sign)
425 bid128_add(&r2, &res, &ten);
426 else
427 bid128_sub(&r2, &res, &ten);
428 bid128_to_int32_xint(&ires, &r2);
429 } else
430 bid128_to_int32_xint(&ires, &res);
431 return ires;
432 }
433
434 char to_char(Phloat p) {
435 int4 res;
436 bid128_to_int32_xint(&res, &p.val);
437 return (char) res;
438 }
439
440 int to_int(Phloat p) {
441 int4 res;
442 bid128_to_int32_xint(&res, &p.val);
443 return (int) res;
444 }
445
446 int4 to_int4(Phloat p) {
447 int4 res;
448 bid128_to_int32_xint(&res, &p.val);
449 return res;
450 }
451
452 int8 to_int8(Phloat p) {
453 int8 res;
454 bid128_to_int64_xint(&res, &p.val);
455 return res;
456 }
457
458 uint8 to_uint8(Phloat p) {
459 uint8 res;
460 bid128_to_uint64_xint(&res, &p.val);
461 return res;
462 }
463
464 double to_double(Phloat p) {
465 double res;
466 bid128_to_binary64(&res, &p.val);
467 return res;
468 }
469
470 Phloat sin(Phloat p) {
471 BID_UINT128 res;
472 bid128_sin(&res, &p.val);
473 return Phloat(res);
474 }
475
476 Phloat cos(Phloat p) {
477 BID_UINT128 res;
478 bid128_cos(&res, &p.val);
479 return Phloat(res);
480 }
481
482 Phloat tan(Phloat p) {
483 BID_UINT128 res;
484 bid128_tan(&res, &p.val);
485 return Phloat(res);
486 }
487
488 Phloat asin(Phloat p) {
489 BID_UINT128 res;
490 bid128_asin(&res, &p.val);
491 return Phloat(res);
492 }
493
494 Phloat acos(Phloat p) {
495 if (p == -1)
496 // Intel library bug work-around
497 return PI;
498 BID_UINT128 res;
499 bid128_acos(&res, &p.val);
500 return Phloat(res);
501 }
502
503 Phloat atan(Phloat p) {
504 BID_UINT128 res;
505 bid128_atan(&res, &p.val);
506 return Phloat(res);
507 }
508
509 void p_sincos(Phloat phi, Phloat *s, Phloat *c) {
510 bid128_sin(&s->val, &phi.val);
511 bid128_cos(&c->val, &phi.val);
512 }
513
514 Phloat hypot(Phloat x, Phloat y) {
515 BID_UINT128 res;
516 bid128_hypot(&res, &x.val, &y.val);
517 return Phloat(res);
518 }
519
520 Phloat atan2(Phloat x, Phloat y) {
521 BID_UINT128 res;
522 bid128_atan2(&res, &x.val, &y.val);
523 return Phloat(res);
524 }
525
526 Phloat sinh(Phloat p) {
527 BID_UINT128 res;
528 bid128_sinh(&res, &p.val);
529 return Phloat(res);
530 }
531
532 Phloat cosh(Phloat p) {
533 BID_UINT128 res;
534 bid128_cosh(&res, &p.val);
535 return Phloat(res);
536 }
537
538 Phloat tanh(Phloat p) {
539 BID_UINT128 res;
540 bid128_tanh(&res, &p.val);
541 return Phloat(res);
542 }
543
544 Phloat asinh(Phloat p) {
545 BID_UINT128 res;
546 bid128_asinh(&res, &p.val);
547 return Phloat(res);
548 }
549
550 Phloat acosh(Phloat p) {
551 BID_UINT128 res;
552 bid128_acosh(&res, &p.val);
553 return Phloat(res);
554 }
555
556 Phloat atanh(Phloat p) {
557 BID_UINT128 res;
558 bid128_atanh(&res, &p.val);
559 return Phloat(res);
560 }
561
562 Phloat log(Phloat p) {
563 BID_UINT128 res;
564 bid128_log(&res, &p.val);
565 return Phloat(res);
566 }
567
568 Phloat log1p(Phloat p) {
569 BID_UINT128 res;
570 bid128_log1p(&res, &p.val);
571 return Phloat(res);
572 }
573
574 Phloat log10(Phloat p) {
575 BID_UINT128 res;
576 bid128_log10(&res, &p.val);
577 return Phloat(res);
578 }
579
580 Phloat exp(Phloat p) {
581 BID_UINT128 res;
582 bid128_exp(&res, &p.val);
583 return Phloat(res);
584 }
585
586 Phloat expm1(Phloat p) {
587 BID_UINT128 res;
588 bid128_expm1(&res, &p.val);
589 return Phloat(res);
590 }
591
592 Phloat tgamma(Phloat p) {
593 BID_UINT128 res;
594 bid128_tgamma(&res, &p.val);
595 return Phloat(res);
596 }
597
598 Phloat sqrt(Phloat p) {
599 BID_UINT128 res;
600 bid128_sqrt(&res, &p.val);
601 return Phloat(res);
602 }
603
604 Phloat fmod(Phloat x, Phloat y) {
605 BID_UINT128 res;
606 bid128_rem(&res, &x.val, &y.val);
607 int numer_sign, denom_sign, res_sign;
608 bid128_isSigned(&numer_sign, &x.val);
609 bid128_isSigned(&denom_sign, &y.val);
610 bid128_isSigned(&res_sign, &res);
611 if (numer_sign ^ res_sign) {
612 BID_UINT128 r2;
613 if (denom_sign ^ res_sign)
614 bid128_add(&r2, &res, &y.val);
615 else
616 bid128_sub(&r2, &res, &y.val);
617 return Phloat(r2);
618 } else
619 return Phloat(res);
620 }
621
622 Phloat fabs(Phloat p) {
623 BID_UINT128 res;
624 bid128_abs(&res, &p.val);
625 return Phloat(res);
626 }
627
628 Phloat pow(Phloat y, Phloat x) {
629 BID_UINT128 tmp, res;
630 bid128_round_integral_negative(&tmp, &x.val);
631 int r;
632 bid128_quiet_equal(&r, &tmp, &x.val);
633 if (r != 0) {
634 // Integral power. Use repeated squaring for these, at
635 // least as long as the calculations are exact. We make sure
636 // of this by scaling the number to make the mantissa the
637 // smallest possible integer, and then check whether it
638 // grows beyond 10^34-1.
639 if (x < -2147483647.0 || x > 2147483647.0)
640 goto inexact;
641 int4 ex = to_int4(x);
642 // Handle base of zero
643 bid128_isZero(&r, &y.val);
644 if (r != 0) {
645 if (ex < 0) {
646 BID_UINT128 zero;
647 int izero = 0;
648 bid128_from_int32(&zero, &izero);
649 bid128_div(&res, &zero, &zero); // 0/0 -> NaN
650 return res;
651 } else if (ex == 0)
652 return 1;
653 else
654 return 0;
655 }
656 // Handle negative base
657 bool result_negative;
658 BID_UINT128 yy;
659 bid128_isSigned(&r, &y.val);
660 if (r != 0) {
661 result_negative = (ex & 1) != 0;
662 bid128_negate(&yy, &y.val);
663 } else {
664 result_negative = false;
665 yy = y.val;
666 }
667 // Handle negative exponent
668 int ione = 1;
669 bid128_from_int32(&res, &ione);
670 if (ex < 0) {
671 bid128_div(&tmp, &res, &yy);
672 yy = tmp;
673 ex = -ex;
674 }
675 // Scale mantissa to smallest possible integer
676 int scale;
677 bid128_ilogb(&scale, &yy);
678 if (scale != 0) {
679 scale = -scale;
680 bid128_scalbn(&tmp, &yy, &scale);
681 scale = -scale;
682 yy = tmp;
683 }
684 while (true) {
685 bid128_round_integral_negative(&tmp, &yy);
686 bid128_quiet_equal(&r, &tmp, &yy);
687 if (r != 0)
688 break;
689 r = 1;
690 bid128_scalbn(&tmp, &yy, &r);
691 yy = tmp;
692 scale--;
693 }
694 int8 final_scale = scale;
695 final_scale *= ex;
696 if (final_scale > 6144 || final_scale < -6209)
697 // Out of range, but let bid128_pow() deal with it
698 goto inexact;
699 scale = (int) final_scale;
700 // Only perform repeated squaring if scaled mantissa != 1
701 bid128_quiet_equal(&r, &res, &yy);
702 if (r == 0) {
703 // Check if exponent so large that result can't possibly be exact
704 if (ex > 112)
705 goto inexact;
706 bid128_ilogb(&r, &yy);
707 if (ex * r > 33)
708 goto inexact;
709 // Perform exponentiation by repeated squaring
710 while (true) {
711 if ((ex & 1) != 0) {
712 bid128_mul(&tmp, &res, &yy);
713 res = tmp;
714 bid128_ilogb(&r, &res);
715 if (r > 33)
716 goto inexact;
717 }
718 ex >>= 1;
719 if (ex == 0)
720 break;
721 bid128_mul(&tmp, &yy, &yy);
722 yy = tmp;
723 }
724 }
725 bid128_scalbn(&tmp, &res, &scale);
726 if (!result_negative)
727 return Phloat(tmp);
728 bid128_negate(&res, &tmp);
729 return Phloat(res);
730 } else {
731 inexact:
732 bid128_pow(&res, &y.val, &x.val);
733 return Phloat(res);
734 }
735 }
736
737 Phloat floor(Phloat p) {
738 BID_UINT128 res;
739 bid128_round_integral_negative(&res, &p.val);
740 return Phloat(res);
741 }
742
743 Phloat fma(Phloat x, Phloat y, Phloat z) {
744 BID_UINT128 res;
745 bid128_fma(&res, &x.val, &y.val, &z.val);
746 return Phloat(res);
747 }
748
749 Phloat nextafter(Phloat x, Phloat y) {
750 BID_UINT128 res;
751 bid128_nextafter(&res, &x.val, &y.val);
752 return Phloat(res);
753 }
754
755 int ilogb(Phloat x) {
756 int res;
757 bid128_ilogb(&res, &x.val);
758 return res;
759 }
760
761 Phloat scalbn(Phloat x, int y) {
762 BID_UINT128 res;
763 bid128_scalbn(&res, &x.val, &y);
764 return Phloat(res);
765 }
766
767 Phloat copysign(Phloat x, Phloat y) {
768 BID_UINT128 res;
769 bid128_copySign(&res, &x.val, &y.val);
770 return Phloat(res);
771 }
772
773 Phloat operator*(int x, Phloat y) {
774 BID_UINT128 xx, res;
775 bid128_from_int32(&xx, &x);
776 bid128_mul(&res, &xx, &y.val);
777 return Phloat(res);
778 }
779
780 Phloat operator/(int x, Phloat y) {
781 BID_UINT128 xx, res;
782 bid128_from_int32(&xx, &x);
783 bid128_div(&res, &xx, &y.val);
784 return Phloat(res);
785 }
786
787 Phloat operator/(double x, Phloat y) {
788 BID_UINT128 xx, res;
789 BID_UINT64 tmp;
790 binary64_to_bid64(&tmp, &x);
791 bid64_to_bid128(&xx, &tmp);
792 bid128_div(&res, &xx, &y.val);
793 return Phloat(res);
794 }
795
796 Phloat operator+(int x, Phloat y) {
797 BID_UINT128 xx, res;
798 bid128_from_int32(&xx, &x);
799 bid128_add(&res, &xx, &y.val);
800 return Phloat(res);
801 }
802
803 Phloat operator-(int x, Phloat y) {
804 BID_UINT128 xx, res;
805 bid128_from_int32(&xx, &x);
806 bid128_sub(&res, &xx, &y.val);
807 return Phloat(res);
808 }
809
810 bool operator==(int4 x, Phloat y) {
811 BID_UINT128 xx;
812 bid128_from_int32(&xx, &x);
813 int r;
814 bid128_quiet_equal(&r, &xx, &y.val);
815 return r != 0;
816 }
817
818 Phloat PI("3.141592653589793238462643383279503");
819
820
821 #else // BCD_MATH
822
823
824 void phloat_init() {
825 POS_HUGE_PHLOAT = DBL_MAX;
826 NEG_HUGE_PHLOAT = -POS_HUGE_PHLOAT;
827 #ifdef WINDOWS
828 double d = 1;
829 while (1) {
830 double d2 = d / 2;
831 if (d2 == 0)
832 break;
833 d = d2;
834 }
835 POS_TINY_PHLOAT = d;
836 *(int8 *) &NAN_1_PHLOAT = 0x7ff8000000000001;
837 *(int8 *) &NAN_2_PHLOAT = 0x7ff8000000000002;
838 #else
839 POS_TINY_PHLOAT = nextafter(0.0, 1.0);
840 NAN_1_PHLOAT = nan("1");
841 NAN_2_PHLOAT = nan("2");
842 #endif
843 NEG_TINY_PHLOAT = -POS_TINY_PHLOAT;
844 NAN_PHLOAT = nan("");
845 }
846
847 int string2phloat(const char *buf, int buflen, phloat *d) {
848 /* Convert string to phloat.
849 * Return values:
850 * 0: no error
851 * 1: positive overflow
852 * 2: negative overflow
853 * 3: positive underflow
854 * 4: negative underflow
855 * 5: other error
856 */
857 char mantissa[MAX_MANT_DIGITS];
858 int mant_sign = 0;
859 int skipping_zeroes = 1;
860 int seen_dot = 0;
861 int mant_pos = 0;
862 int exp_offset = -1;
863 int in_exp = 0;
864 int exp = 0;
865 int exp_sign = 0;
866 int i, mant_digits = 0;
867 char dot = flags.f.decimal_point ? '.' : ',';
868 char sep = flags.f.decimal_point ? ',' : '.';
869 int is_zero = 1;
870 double res;
871
872 memset(mantissa, 0, MAX_MANT_DIGITS);
873
874 for (i = 0; i < buflen; i++) {
875 char c = buf[i];
876 if (c == 24) {
877 in_exp = 1;
878 if (mant_digits == 0) {
879 mant_digits++;
880 mantissa[mant_pos++] = 1;
881 is_zero = 0;
882 exp_offset = 0;
883 }
884 continue;
885 }
886 if (in_exp) {
887 if (c == '-')
888 exp_sign = 1;
889 else
890 exp = exp * 10 + (c - '0');
891 } else {
892 if (c == sep)
893 continue;
894 if (c == dot) {
895 seen_dot = 1;
896 skipping_zeroes = 0;
897 continue;
898 }
899 if (c == '-') {
900 mant_sign = 1;
901 continue;
902 }
903 /* Once we get here, c should be a digit */
904 if (++mant_digits > MAX_MANT_DIGITS)
905 /* Too many digits! We only allow the user to enter 17 (binary) or 34 (decimal). */
906 return 5;
907 if (c == '0' && skipping_zeroes)
908 continue;
909 skipping_zeroes = 0;
910 mantissa[mant_pos++] = c - '0';
911 if (c != '0')
912 is_zero = 0;
913 if (!seen_dot)
914 exp_offset = mant_pos - 1;
915 }
916 }
917
918 if (is_zero) {
919 *d = 0;
920 return 0;
921 }
922
923 if (exp_sign)
924 exp = -exp;
925 exp += exp_offset;
926
927 /* Get rid of leading zeroes in mantissa
928 * (the loop above removes redundant leading zeroes, e.g. the first two
929 * of 0012.345, but in the case of 0.001, the two zeroes following the
930 * decimal are not redundant, and they end up in the mantissa.
931 */
932 if (mantissa[0] == 0) {
933 int leadingzeroes = 0;
934 int i;
935 while (mantissa[leadingzeroes] == 0)
936 leadingzeroes++;
937 for (i = 0; i < mant_pos - leadingzeroes; i++)
938 mantissa[i] = mantissa[i + leadingzeroes];
939 for (i = mant_pos - leadingzeroes; i < mant_pos; i++)
940 mantissa[i] = 0;
941 exp -= leadingzeroes;
942 }
943
944 /* 'mantissa' now contains the normalized bcd mantissa;
945 * 'exp' contains the normalized signed exponent,
946 * and 'mant_sign' contains the mantissa's sign.
947 */
948 char decstr[35];
949 int pos = 0;
950 if (mant_sign)
951 decstr[pos++] = '-';
952 for (i = 0; i < MAX_MANT_DIGITS; i++) {
953 decstr[pos++] = mantissa[i] + '0';
954 if (i == 0)
955 decstr[pos++] = '.';
956 }
957 snprintf(decstr + pos, 35 - pos, "e%d", exp);
958 sscanf(decstr, "%le", &res);
959 if (isinf(res))
960 return mant_sign ? 2 : 1;
961 if (res == 0.0)
962 return mant_sign ? 4 : 3;
963 *d = res;
964 return 0;
965 }
966
967 double decimal2double(void *data, bool pin_magnitude /* = false */) {
968 double res;
969 BID_UINT128 *b, b2;
970 if ((((size_t) data) & 15) != 0) {
971 //b2 = *((BID_UINT128 *) data);
972 memcpy(&b2, data, 16);
973 b = &b2;
974 } else
975 b = (BID_UINT128 *) data;
976 bid128_to_binary64(&res, b);
977 if (isnan(res) || !pin_magnitude)
978 return res;
979 int r;
980 if (res == 0 && !(bid128_isZero(&r, b), r))
981 return (bid128_isSigned(&r, b), r) ? NEG_TINY_PHLOAT : POS_TINY_PHLOAT;
982 int inf = isinf(res);
983 return inf == 0 ? res : inf < 0 ? NEG_HUGE_PHLOAT : POS_HUGE_PHLOAT;
984 }
985
986
987 #endif // BCD_MATH
988
989
990 int phloat2string(phloat pd, char *buf, int buflen, int base_mode, int digits,
991 int dispmode, int thousandssep, int max_mant_digits,
992 const char *format) {
993 int group1, group2;
994 char dec, sep;
995 if (format == NULL) {
996 dec = flags.f.decimal_point ? '.' : ',';
997 sep = flags.f.decimal_point ? ',' : '.';
998 group1 = group2 = 3;
999 } else {
1000 dec = format[0];
1001 thousandssep = format[1] != 0;
1002 if (thousandssep) {
1003 sep = format[1];
1004 group1 = format[2] - '0';
1005 group2 = format[3] - '0';
1006 }
1007 }
1008
1009 if (pd == 0)
1010 pd = 0; // Suppress signed zero
1011
1012 int chars_so_far = 0;
1013
1014 if (p_isnan(pd)) {
1015 string2buf(buf, buflen, &chars_so_far, "<Not a Number>", 14);
1016 return chars_so_far;
1017 }
1018
1019 if (p_isinf(pd)) {
1020 char2buf(buf, buflen, &chars_so_far, '<');
1021 if (pd < 0)
1022 char2buf(buf, buflen, &chars_so_far, '-');
1023 string2buf(buf, buflen, &chars_so_far, "Infinity>", 9);
1024 return chars_so_far;
1025 }
1026
1027 /* base_mode: 0=only decimal, 1=all bases, 2=SHOW */
1028 int base = get_base();
1029 int wsize = effective_wsize();
1030 if (base_mode == 1
1031 && (base != 10 || mode_dec_int && mode_appmenu >= MENU_BASE1 && mode_appmenu <= MENU_BASE_DISP)
1032 || base_mode == 2
1033 && (base == 2 || base == 8 && wsize > 60)) {
1034
1035 uint8 n;
1036 int inexact, shift;
1037 bool too_big = false;
1038 char binbuf[80];
1039 int binbufptr = 0;
1040
1041 phloat high, low;
1042 if (flags.f.base_signed) {
1043 high = pow(phloat(2), wsize - 1);
1044 low = -high;
1045 high--;
1046 } else {
1047 high = pow(phloat(2), wsize) - 1;
1048 low = 0;
1049 }
1050 if (pd > high || pd < low) {
1051 if (flags.f.base_wrap) {
1052 too_big = true;
1053 phloat ipd = pd < 0 ? -floor(-pd) : floor(pd);
1054 inexact = base_mode == 1 && pd != ipd;
1055 phloat d = pow(phloat(2), wsize);
1056 phloat r = fmod(ipd, d);
1057 if (r < 0)
1058 r += d;
1059 n = (int8) to_uint8(r);
1060 if (flags.f.base_signed) {
1061 int8 m = 1LL << (wsize - 1);
1062 if ((n & m) != 0)
1063 n |= -1LL << (wsize - 1);
1064 else
1065 n &= (1LL << (wsize - 1)) - 1;
1066 } else {
1067 if (wsize < 64)
1068 n &= (1ULL << wsize) - 1;
1069 }
1070 } else {
1071 if (base_mode == 2)
1072 goto decimal_after_all;
1073 else {
1074 if (!flags.f.base_signed && pd < 0)
1075 string2buf(buf, buflen, &chars_so_far, "<Negative>", 10);
1076 else
1077 string2buf(buf, buflen, &chars_so_far, "<Too Big>", 9);
1078 return chars_so_far;
1079 }
1080 }
1081 } else {
1082 if (flags.f.base_signed) {
1083 int8 sn = to_int8(pd);
1084 inexact = base_mode == 1 && pd != sn;
1085 n = (uint8) sn;
1086 } else {
1087 n = to_uint8(pd);
1088 inexact = base_mode == 1 && pd != n;
1089 }
1090 }
1091 if (wsize < 64)
1092 n &= (1ULL << wsize) - 1;
1093
1094 if (base_mode == 2 && base == 2) {
1095 int maxbits = 44;
1096 if (mode_bin_sep)
1097 maxbits = ((maxbits + 1) * 4) / 5;
1098 if (inexact)
1099 maxbits--;
1100 if (too_big)
1101 maxbits--;
1102 int8 mask = -1LL << maxbits;
1103 if ((n & mask) != 0) {
1104 // Too many bits; won't fit. Use hex instead.
1105 string2buf(buf, buflen, &chars_so_far, "hex ", 4);
1106 base = 16;
1107 }
1108 }
1109 if (base == 10) {
1110 bool sign = false;
1111 if (flags.f.base_signed) {
1112 uint8 s = 1ULL << (wsize - 1);
1113 if ((n & s) != 0) {
1114 sign = true;
1115 n = ~n + 1;
1116 if (wsize < 64)
1117 n &= (1ULL << wsize) - 1;
1118 }
1119 }
1120 int p = 0;
1121 while (n != 0) {
1122 if (mode_dec_sep && ++p == 4) {
1123 binbuf[binbufptr++] = sep;
1124 p = 1;
1125 }
1126 int digit = n % 10;
1127 binbuf[binbufptr++] = '0' + digit;
1128 n /= 10;
1129 }
1130 if (sign)
1131 binbuf[binbufptr++] = '-';
1132 } else {
1133 shift = base == 2 ? 1 : base == 8 ? 3 : 4;
1134 bool do_sep = base == 2 ? mode_bin_sep : base == 8 ? mode_oct_sep : mode_hex_sep;
1135 int p = 0;
1136 while (n != 0) {
1137 if (do_sep && ++p == 5) {
1138 binbuf[binbufptr++] = ' ';
1139 p = 1;
1140 }
1141 int digit = (int) (n & (base - 1));
1142 char c = digit < 10 ? '0' + digit : 'A' + digit - 10;
1143 binbuf[binbufptr++] = c;
1144 n >>= shift;
1145 }
1146 }
1147 if (binbufptr == 0)
1148 binbuf[binbufptr++] = '0';
1149
1150 if (too_big)
1151 char2buf(buf, buflen, &chars_so_far, 26);
1152 while (binbufptr > 0)
1153 char2buf(buf, buflen, &chars_so_far, binbuf[--binbufptr]);
1154 if (inexact)
1155 char2buf(buf, buflen, &chars_so_far, dec);
1156 return chars_so_far;
1157
1158 decimal_after_all:;
1159 }
1160
1161 char bcd_mantissa[MAX_MANT_DIGITS];
1162 memset(bcd_mantissa, 0, MAX_MANT_DIGITS);
1163 int bcd_exponent = 0;
1164 int bcd_mantissa_sign = 0;
1165
1166 char decstr[50];
1167
1168 #ifndef BCD_MATH
1169 double d = to_double(pd);
1170 snprintf(decstr, 50, "%.*e", MAX_MANT_DIGITS - 2, d);
1171 double d2;
1172 if (sscanf(decstr, "%le", &d2) != 1 || d != d2)
1173 snprintf(decstr, 50, "%.*e", MAX_MANT_DIGITS - 1, d);
1174 #else
1175 bid128_to_string(decstr, &pd.val);
1176 #endif
1177
1178 char *p = decstr;
1179 int mant_index = 0;
1180 bcd_mantissa_sign = 0;
1181 bool seen_dot = false;
1182 bool in_leading_zeroes = true;
1183 int exp_offset = -1;
1184
1185 while (*p != 0) {
1186 char c = *p++;
1187 if (c == '-') {
1188 bcd_mantissa_sign = 1;
1189 continue;
1190 }
1191 if (c == '+')
1192 continue;
1193 if (c == '.') {
1194 seen_dot = true;
1195 continue;
1196 }
1197 if (c == 'e' || c == 'E') {
1198 if (!in_leading_zeroes) {
1199 sscanf(p, "%d", &bcd_exponent);
1200 bcd_exponent += exp_offset;
1201 }
1202 break;
1203 }
1204 // Can only be decimal digit at this point
1205 if (c == '0') {
1206 if (in_leading_zeroes)
1207 continue;
1208 } else
1209 in_leading_zeroes = false;
1210 if (!seen_dot)
1211 exp_offset++;
1212 if (mant_index < MAX_MANT_DIGITS)
1213 bcd_mantissa[mant_index++] = c - '0';
1214 }
1215
1216 int max_int_digits = max_mant_digits;
1217 int max_frac_digits = MAX_MANT_DIGITS + max_int_digits - 1;
1218
1219 if (dispmode == 0 || dispmode == 3) {
1220
1221 /* FIX and ALL modes */
1222
1223 char norm_ip[MAX_MANT_DIGITS];
1224 memset(norm_ip, 0, max_int_digits);
1225 char norm_fp[MAX_MANT_DIGITS * 2 - 1];
1226 memset(norm_fp, 0, max_frac_digits);
1227
1228 int i;
1229 int int_digits, frac_digits;
1230
1231 int digits2;
1232 if (dispmode == 0)
1233 digits2 = digits;
1234 else
1235 digits2 = max_int_digits - 1;
1236
1237 if (bcd_exponent >= max_int_digits || -bcd_exponent > digits2 + 1)
1238 goto do_sci;
1239 for (i = 0; i < MAX_MANT_DIGITS; i++) {
1240 if (i <= bcd_exponent)
1241 norm_ip[max_int_digits - 1 - bcd_exponent + i] = bcd_mantissa[i];
1242 else
1243 norm_fp[-1 - bcd_exponent + i] = bcd_mantissa[i];
1244 }
1245
1246 if (dispmode == 0) {
1247 /* NOTE: I don't simply round norm_fp[digits], because of a
1248 * circumstance that cannot happen on a real HP-42S: there
1249 * may not be enough positions in the display to show a number
1250 * at the requested accuracy (e.g. 123456123456.5 at FIX 01)
1251 * so I first calculate how many positions I actually have
1252 * available, and then I round to that. As a result of this
1253 * rounding, the number may overflow the FIX representation,
1254 * in which case I fall back on SCI.
1255 */
1256 int carry;
1257 int visdigits = max_int_digits - 1 - bcd_exponent;
1258 if (visdigits > digits)
1259 visdigits = digits;
1260 carry = norm_fp[visdigits] >= 5;
1261 for (i = visdigits; i < max_frac_digits; i++)
1262 norm_fp[i] = 0;
1263 if (!carry)
1264 goto done_rounding;
1265 for (i = visdigits - 1; i >= 0; i--) {
1266 char c = norm_fp[i] + 1;
1267 if (c < 10) {
1268 norm_fp[i] = c;
1269 goto done_rounding;
1270 } else
1271 norm_fp[i] = c - 10;
1272 }
1273 for (i = max_int_digits - 1; i >= 0; i--) {
1274 char c = norm_ip[i] + 1;
1275 if (c < 10) {
1276 norm_ip[i] = c;
1277 goto done_rounding;
1278 } else
1279 norm_ip[i] = c - 10;
1280 }
1281 /* If we get here, the carry went past the 12th integer digit,
1282 * and we have to use SCI mode instead.
1283 */
1284 goto do_sci;
1285 done_rounding:;
1286 } else if (max_int_digits < MAX_MANT_DIGITS) {
1287 /* ALL mode: for HP-42S compatibility, round to max_int_digits
1288 * digits before proceeding.
1289 */
1290 int f = 1000;
1291 for (i = 0; i < max_int_digits + max_frac_digits; i++) {
1292 char c = i < max_int_digits ? norm_ip[i] : norm_fp[i - max_int_digits];
1293 if (c != 0 && f == 1000)
1294 f = i;
1295 if (i == f + max_int_digits) {
1296 int carry = c >= 5;
1297 if (carry) {
1298 int j;
1299 for (j = i - 1; j >= 0; j--) {
1300 char c2 = j < max_int_digits ? norm_ip[j] : norm_fp[j - max_int_digits];
1301 c2++;
1302 if (c2 < 10)
1303 carry = 0;
1304 else {
1305 c2 -= 10;
1306 carry = 1;
1307 }
1308 if (j < max_int_digits)
1309 norm_ip[j] = c2;
1310 else
1311 norm_fp[j - max_int_digits] = c2;
1312 if (!carry)
1313 break;
1314 }
1315 if (carry)
1316 /* Rounding is making the integer part max_int_digits + 1 digits
1317 * long; must go to SCI mode.
1318 */
1319 goto do_sci;
1320 }
1321 }
1322 if (i >= f + max_int_digits) {
1323 if (i < max_int_digits)
1324 norm_ip[i] = 0;
1325 else
1326 norm_fp[i - max_int_digits] = 0;
1327 }
1328 }
1329 }
1330
1331 /* Check if the number is still within bounds for FIX or ALL */
1332 if (pd != 0) {
1333 /* Make sure that nonzero numbers are not
1334 * displayed as zero because of the rounding.
1335 */
1336 for (i = 0; i < max_int_digits; i++)
1337 if (norm_ip[i] != 0)
1338 goto fix_ok;
1339 for (i = 0; i < digits2; i++)
1340 if (norm_fp[i] != 0)
1341 goto fix_ok;
1342 /* Uh-oh, the number is nonzero, but its rounded representation
1343 * is zero. That's not good; use SCI mode instead.
1344 */
1345 goto do_sci;
1346 fix_ok:
1347 if (dispmode == 3) {
1348 /* Make sure we're not throwing away anything in ALL mode */
1349 for (i = max_int_digits - 1; i < max_frac_digits; i++)
1350 if (norm_fp[i] != 0)
1351 goto do_sci;
1352 }
1353 }
1354
1355 int_digits = 1;
1356 for (i = 0; i < max_int_digits; i++)
1357 if (norm_ip[i] != 0) {
1358 int_digits = max_int_digits - i;
1359 break;
1360 }
1361
1362 if (bcd_mantissa_sign)
1363 char2buf(buf, buflen, &chars_so_far, '-');
1364
1365 for (i = int_digits - 1; i >= 0; i--) {
1366 if (thousandssep && i >= group1 - 1 && (i - group1 + 1) % group2 == 0 && i != int_digits - 1)
1367 if (sep == ' ')
1368 // U+2009: Unicode thin space
1369 string2buf(buf, buflen, &chars_so_far, "\342\200\211", 3);
1370 else
1371 char2buf(buf, buflen, &chars_so_far, sep);
1372 char2buf(buf, buflen, &chars_so_far, (char)('0' + norm_ip[max_int_digits - 1 - i]));
1373 }
1374
1375 if (dispmode == 0)
1376 frac_digits = digits;
1377 else {
1378 frac_digits = 0;
1379 for (i = 0; i < max_frac_digits; i++)
1380 if (norm_fp[i] != 0)
1381 frac_digits = i + 1;
1382 }
1383 if (frac_digits + int_digits > max_int_digits)
1384 frac_digits = max_int_digits - int_digits;
1385
1386 if (frac_digits > 0 || (dispmode == 0 && thousandssep)) {
1387 char2buf(buf, buflen, &chars_so_far, dec);
1388 for (i = 0; i < frac_digits; i++)
1389 char2buf(buf, buflen, &chars_so_far, (char) ('0' + norm_fp[i]));
1390 }
1391
1392 return chars_so_far;
1393
1394 } else {
1395
1396 /* SCI and ENG modes */
1397 /* Also fall-through from FIX and ALL */
1398
1399 int m_digits;
1400 int carry;
1401 char norm_mantissa[MAX_MANT_DIGITS];
1402 int norm_exponent, e3;
1403 int i;
1404
1405 do_sci:
1406
1407 for (i = 0; i < MAX_MANT_DIGITS; i++)
1408 norm_mantissa[i] = bcd_mantissa[i];
1409 norm_exponent = bcd_exponent;
1410
1411 if (dispmode == 3) {
1412 /* Round to max_int_digits digits before doing anything else;
1413 * this is needed to handle mantissas like 9.99999999999999,
1414 * which would otherwise end up getting displayed as
1415 * 10.0000000000 instead of 10.
1416 */
1417
1418 sci_all_round:
1419
1420 carry = max_int_digits < MAX_MANT_DIGITS
1421 && norm_mantissa[max_int_digits] >= 5;
1422 for (i = max_int_digits; i < MAX_MANT_DIGITS; i++)
1423 norm_mantissa[i] = 0;
1424 if (carry) {
1425 for (i = max_int_digits - 1; i >= 0; i--) {
1426 char c = norm_mantissa[i] + carry;
1427 if (c < 10) {
1428 norm_mantissa[i] = c;
1429 carry = 0;
1430 break;
1431 } else {
1432 norm_mantissa[i] = c - 10;
1433 carry = 1;
1434 }
1435 }
1436 }
1437 if (carry) {
1438 /* Don't round by an additional digit: that would mean
1439 * we're rounding the same number twice, which is bad
1440 * (think about what happens when you round one digit off
1441 * 0.45, twice -- you get first 0.5, then 1... Oops).
1442 * So, we start over.
1443 */
1444 for (i = 0; i < MAX_MANT_DIGITS - 1; i++)
1445 norm_mantissa[i + 1] = bcd_mantissa[i];
1446 norm_mantissa[0] = 0;
1447 norm_exponent = bcd_exponent + 1;
1448 goto sci_all_round;
1449 }
1450 m_digits = 0;
1451 for (i = max_int_digits - 1; i >= 0; i--)
1452 if (norm_mantissa[i] != 0) {
1453 m_digits = i;
1454 break;
1455 }
1456 } else {
1457 m_digits = digits;
1458
1459 sci_round:
1460 carry = m_digits + 1 < MAX_MANT_DIGITS
1461 && norm_mantissa[m_digits + 1] >= 5;
1462 for (i = m_digits + 1; i < MAX_MANT_DIGITS; i++)
1463 norm_mantissa[i] = 0;
1464 if (carry) {
1465 for (i = m_digits; i >= 0; i--) {
1466 char c = norm_mantissa[i] + carry;
1467 if (c < 10) {
1468 norm_mantissa[i] = c;
1469 carry = 0;
1470 break;
1471 } else {
1472 norm_mantissa[i] = c - 10;
1473 carry = 1;
1474 }
1475 }
1476 }
1477 if (carry) {
1478 /* Don't round by an additional digit: that would mean
1479 * we're rounding the same number twice, which is bad
1480 * (think about what happens when you round one digit off
1481 * 0.45, twice -- you get first 0.5, then 1... Oops).
1482 * So, we start over.
1483 */
1484 for (i = 0; i < MAX_MANT_DIGITS - 1; i++)
1485 norm_mantissa[i + 1] = bcd_mantissa[i];
1486 norm_mantissa[0] = 0;
1487 norm_exponent = bcd_exponent + 1;
1488 goto sci_round;
1489 }
1490 }
1491
1492 if (bcd_mantissa_sign)
1493 char2buf(buf, buflen, &chars_so_far, '-');
1494 if (dispmode == 2) {
1495 e3 = norm_exponent % 3;
1496 if (e3 < 0)
1497 e3 += 3;
1498 if (m_digits < e3)
1499 m_digits = e3;
1500 norm_exponent -= e3;
1501 } else
1502 e3 = 0;
1503 for (i = 0; i <= m_digits; i++) {
1504 char2buf(buf, buflen, &chars_so_far,
1505 (char) ('0' + norm_mantissa[i]));
1506 if (i == e3)
1507 char2buf(buf, buflen, &chars_so_far, dec);
1508 }
1509
1510 char2buf(buf, buflen, &chars_so_far, 24);
1511 i = int2string(norm_exponent, buf + chars_so_far,
1512 buflen - chars_so_far);
1513 chars_so_far += i;
1514
1515 return chars_so_far;
1516 }
1517 }