3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008 by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "That only makes eleven (plus one mislaid) and not fourteen,
13 * unless wizards count differently to other people." --Beorn
15 * [p.115 of _The Hobbit_: "Queer Lodgings"]
19 =head1 Numeric functions
21 This file contains all the stuff needed by perl for manipulating numeric
22 values, including such things as replacements for the OS's atof() function
29 #define PERL_IN_NUMERIC_C
33 Perl_cast_ulong(pTHX_ NV f)
37 return f < I32_MIN ? (U32) I32_MIN : (U32)(I32) f;
40 if (f < U32_MAX_P1_HALF)
43 return ((U32) f) | (1 + U32_MAX >> 1);
48 return f > 0 ? U32_MAX : 0 /* NaN */;
52 Perl_cast_i32(pTHX_ NV f)
56 return f < I32_MIN ? I32_MIN : (I32) f;
59 if (f < U32_MAX_P1_HALF)
62 return (I32)(((U32) f) | (1 + U32_MAX >> 1));
67 return f > 0 ? (I32)U32_MAX : 0 /* NaN */;
71 Perl_cast_iv(pTHX_ NV f)
75 return f < IV_MIN ? IV_MIN : (IV) f;
78 /* For future flexibility allowing for sizeof(UV) >= sizeof(IV) */
79 if (f < UV_MAX_P1_HALF)
82 return (IV)(((UV) f) | (1 + UV_MAX >> 1));
87 return f > 0 ? (IV)UV_MAX : 0 /* NaN */;
91 Perl_cast_uv(pTHX_ NV f)
95 return f < IV_MIN ? (UV) IV_MIN : (UV)(IV) f;
98 if (f < UV_MAX_P1_HALF)
101 return ((UV) f) | (1 + UV_MAX >> 1);
106 return f > 0 ? UV_MAX : 0 /* NaN */;
112 converts a string representing a binary number to numeric form.
114 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
115 conversion flags, and I<result> should be NULL or a pointer to an NV.
116 The scan stops at the end of the string, or the first invalid character.
117 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
118 invalid character will also trigger a warning.
119 On return I<*len> is set to the length of the scanned string,
120 and I<*flags> gives output flags.
122 If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
123 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_bin>
124 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
125 and writes the value to I<*result> (or the value is discarded if I<result>
128 The binary number may optionally be prefixed with "0b" or "b" unless
129 C<PERL_SCAN_DISALLOW_PREFIX> is set in I<*flags> on entry. If
130 C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the binary
131 number may use '_' characters to separate digits.
137 Perl_grok_bin(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
139 const char *s = start;
144 const UV max_div_2 = UV_MAX / 2;
145 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
146 bool overflowed = FALSE;
149 PERL_ARGS_ASSERT_GROK_BIN;
151 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
152 /* strip off leading b or 0b.
153 for compatibility silently suffer "b" and "0b" as valid binary
160 else if (len >= 2 && s[0] == '0' && s[1] == 'b') {
167 for (; len-- && (bit = *s); s++) {
168 if (bit == '0' || bit == '1') {
169 /* Write it in this wonky order with a goto to attempt to get the
170 compiler to make the common case integer-only loop pretty tight.
171 With gcc seems to be much straighter code than old scan_bin. */
174 if (value <= max_div_2) {
175 value = (value << 1) | (bit - '0');
178 /* Bah. We're just overflowed. */
179 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
180 "Integer overflow in binary number");
182 value_nv = (NV) value;
185 /* If an NV has not enough bits in its mantissa to
186 * represent a UV this summing of small low-order numbers
187 * is a waste of time (because the NV cannot preserve
188 * the low-order bits anyway): we could just remember when
189 * did we overflow and in the end just multiply value_nv by the
191 value_nv += (NV)(bit - '0');
194 if (bit == '_' && len && allow_underscores && (bit = s[1])
195 && (bit == '0' || bit == '1'))
201 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
202 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
203 "Illegal binary digit '%c' ignored", *s);
207 if ( ( overflowed && value_nv > 4294967295.0)
209 || (!overflowed && value > 0xffffffff )
212 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
213 "Binary number > 0b11111111111111111111111111111111 non-portable");
220 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
229 converts a string representing a hex number to numeric form.
231 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
232 conversion flags, and I<result> should be NULL or a pointer to an NV.
233 The scan stops at the end of the string, or the first invalid character.
234 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
235 invalid character will also trigger a warning.
236 On return I<*len> is set to the length of the scanned string,
237 and I<*flags> gives output flags.
239 If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
240 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_hex>
241 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
242 and writes the value to I<*result> (or the value is discarded if I<result>
245 The hex number may optionally be prefixed with "0x" or "x" unless
246 C<PERL_SCAN_DISALLOW_PREFIX> is set in I<*flags> on entry. If
247 C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the hex
248 number may use '_' characters to separate digits.
254 Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
257 const char *s = start;
261 const UV max_div_16 = UV_MAX / 16;
262 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
263 bool overflowed = FALSE;
265 PERL_ARGS_ASSERT_GROK_HEX;
267 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
268 /* strip off leading x or 0x.
269 for compatibility silently suffer "x" and "0x" as valid hex numbers.
276 else if (len >= 2 && s[0] == '0' && s[1] == 'x') {
283 for (; len-- && *s; s++) {
284 const char *hexdigit = strchr(PL_hexdigit, *s);
286 /* Write it in this wonky order with a goto to attempt to get the
287 compiler to make the common case integer-only loop pretty tight.
288 With gcc seems to be much straighter code than old scan_hex. */
291 if (value <= max_div_16) {
292 value = (value << 4) | ((hexdigit - PL_hexdigit) & 15);
295 /* Bah. We're just overflowed. */
296 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
297 "Integer overflow in hexadecimal number");
299 value_nv = (NV) value;
302 /* If an NV has not enough bits in its mantissa to
303 * represent a UV this summing of small low-order numbers
304 * is a waste of time (because the NV cannot preserve
305 * the low-order bits anyway): we could just remember when
306 * did we overflow and in the end just multiply value_nv by the
307 * right amount of 16-tuples. */
308 value_nv += (NV)((hexdigit - PL_hexdigit) & 15);
311 if (*s == '_' && len && allow_underscores && s[1]
312 && (hexdigit = strchr(PL_hexdigit, s[1])))
318 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
319 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
320 "Illegal hexadecimal digit '%c' ignored", *s);
324 if ( ( overflowed && value_nv > 4294967295.0)
326 || (!overflowed && value > 0xffffffff )
329 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
330 "Hexadecimal number > 0xffffffff non-portable");
337 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
346 converts a string representing an octal number to numeric form.
348 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
349 conversion flags, and I<result> should be NULL or a pointer to an NV.
350 The scan stops at the end of the string, or the first invalid character.
351 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
352 invalid character will also trigger a warning.
353 On return I<*len> is set to the length of the scanned string,
354 and I<*flags> gives output flags.
356 If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
357 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_oct>
358 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
359 and writes the value to I<*result> (or the value is discarded if I<result>
362 If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the octal
363 number may use '_' characters to separate digits.
369 Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
371 const char *s = start;
375 const UV max_div_8 = UV_MAX / 8;
376 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
377 bool overflowed = FALSE;
379 PERL_ARGS_ASSERT_GROK_OCT;
381 for (; len-- && *s; s++) {
382 /* gcc 2.95 optimiser not smart enough to figure that this subtraction
383 out front allows slicker code. */
384 int digit = *s - '0';
385 if (digit >= 0 && digit <= 7) {
386 /* Write it in this wonky order with a goto to attempt to get the
387 compiler to make the common case integer-only loop pretty tight.
391 if (value <= max_div_8) {
392 value = (value << 3) | digit;
395 /* Bah. We're just overflowed. */
396 Perl_ck_warner_d(aTHX_ packWARN(WARN_OVERFLOW),
397 "Integer overflow in octal number");
399 value_nv = (NV) value;
402 /* If an NV has not enough bits in its mantissa to
403 * represent a UV this summing of small low-order numbers
404 * is a waste of time (because the NV cannot preserve
405 * the low-order bits anyway): we could just remember when
406 * did we overflow and in the end just multiply value_nv by the
407 * right amount of 8-tuples. */
408 value_nv += (NV)digit;
411 if (digit == ('_' - '0') && len && allow_underscores
412 && (digit = s[1] - '0') && (digit >= 0 && digit <= 7))
418 /* Allow \octal to work the DWIM way (that is, stop scanning
419 * as soon as non-octal characters are seen, complain only if
420 * someone seems to want to use the digits eight and nine). */
421 if (digit == 8 || digit == 9) {
422 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
423 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
424 "Illegal octal digit '%c' ignored", *s);
429 if ( ( overflowed && value_nv > 4294967295.0)
431 || (!overflowed && value > 0xffffffff )
434 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
435 "Octal number > 037777777777 non-portable");
442 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
451 For backwards compatibility. Use C<grok_bin> instead.
455 For backwards compatibility. Use C<grok_hex> instead.
459 For backwards compatibility. Use C<grok_oct> instead.
465 Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
468 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
469 const UV ruv = grok_bin (start, &len, &flags, &rnv);
471 PERL_ARGS_ASSERT_SCAN_BIN;
474 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
478 Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
481 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
482 const UV ruv = grok_oct (start, &len, &flags, &rnv);
484 PERL_ARGS_ASSERT_SCAN_OCT;
487 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
491 Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
494 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
495 const UV ruv = grok_hex (start, &len, &flags, &rnv);
497 PERL_ARGS_ASSERT_SCAN_HEX;
500 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
504 =for apidoc grok_numeric_radix
506 Scan and skip for a numeric decimal separator (radix).
511 Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send)
513 #ifdef USE_LOCALE_NUMERIC
516 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
518 if (PL_numeric_radix_sv && IN_LOCALE) {
520 const char * const radix = SvPV(PL_numeric_radix_sv, len);
521 if (*sp + len <= send && memEQ(*sp, radix, len)) {
526 /* always try "." if numeric radix didn't match because
527 * we may have data from different locales mixed */
530 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
532 if (*sp < send && **sp == '.') {
540 =for apidoc grok_number
542 Recognise (or not) a number. The type of the number is returned
543 (0 if unrecognised), otherwise it is a bit-ORed combination of
544 IS_NUMBER_IN_UV, IS_NUMBER_GREATER_THAN_UV_MAX, IS_NUMBER_NOT_INT,
545 IS_NUMBER_NEG, IS_NUMBER_INFINITY, IS_NUMBER_NAN (defined in perl.h).
547 If the value of the number can fit an in UV, it is returned in the *valuep
548 IS_NUMBER_IN_UV will be set to indicate that *valuep is valid, IS_NUMBER_IN_UV
549 will never be set unless *valuep is valid, but *valuep may have been assigned
550 to during processing even though IS_NUMBER_IN_UV is not set on return.
551 If valuep is NULL, IS_NUMBER_IN_UV will be set for the same cases as when
552 valuep is non-NULL, but no actual assignment (or SEGV) will occur.
554 IS_NUMBER_NOT_INT will be set with IS_NUMBER_IN_UV if trailing decimals were
555 seen (in which case *valuep gives the true value truncated to an integer), and
556 IS_NUMBER_NEG if the number is negative (in which case *valuep holds the
557 absolute value). IS_NUMBER_IN_UV is not set if e notation was used or the
558 number is larger than a UV.
563 Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep)
566 const char * const send = pv + len;
567 const UV max_div_10 = UV_MAX / 10;
568 const char max_mod_10 = UV_MAX % 10;
573 PERL_ARGS_ASSERT_GROK_NUMBER;
575 while (s < send && isSPACE(*s))
579 } else if (*s == '-') {
581 numtype = IS_NUMBER_NEG;
589 /* next must be digit or the radix separator or beginning of infinity */
591 /* UVs are at least 32 bits, so the first 9 decimal digits cannot
594 /* This construction seems to be more optimiser friendly.
595 (without it gcc does the isDIGIT test and the *s - '0' separately)
596 With it gcc on arm is managing 6 instructions (6 cycles) per digit.
597 In theory the optimiser could deduce how far to unroll the loop
598 before checking for overflow. */
600 int digit = *s - '0';
601 if (digit >= 0 && digit <= 9) {
602 value = value * 10 + digit;
605 if (digit >= 0 && digit <= 9) {
606 value = value * 10 + digit;
609 if (digit >= 0 && digit <= 9) {
610 value = value * 10 + digit;
613 if (digit >= 0 && digit <= 9) {
614 value = value * 10 + digit;
617 if (digit >= 0 && digit <= 9) {
618 value = value * 10 + digit;
621 if (digit >= 0 && digit <= 9) {
622 value = value * 10 + digit;
625 if (digit >= 0 && digit <= 9) {
626 value = value * 10 + digit;
629 if (digit >= 0 && digit <= 9) {
630 value = value * 10 + digit;
632 /* Now got 9 digits, so need to check
633 each time for overflow. */
635 while (digit >= 0 && digit <= 9
636 && (value < max_div_10
637 || (value == max_div_10
638 && digit <= max_mod_10))) {
639 value = value * 10 + digit;
645 if (digit >= 0 && digit <= 9
648 skip the remaining digits, don't
649 worry about setting *valuep. */
652 } while (s < send && isDIGIT(*s));
654 IS_NUMBER_GREATER_THAN_UV_MAX;
674 numtype |= IS_NUMBER_IN_UV;
679 if (GROK_NUMERIC_RADIX(&s, send)) {
680 numtype |= IS_NUMBER_NOT_INT;
681 while (s < send && isDIGIT(*s)) /* optional digits after the radix */
685 else if (GROK_NUMERIC_RADIX(&s, send)) {
686 numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */
687 /* no digits before the radix means we need digits after it */
688 if (s < send && isDIGIT(*s)) {
691 } while (s < send && isDIGIT(*s));
693 /* integer approximation is valid - it's 0. */
699 } else if (*s == 'I' || *s == 'i') {
700 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
701 s++; if (s == send || (*s != 'F' && *s != 'f')) return 0;
702 s++; if (s < send && (*s == 'I' || *s == 'i')) {
703 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
704 s++; if (s == send || (*s != 'I' && *s != 'i')) return 0;
705 s++; if (s == send || (*s != 'T' && *s != 't')) return 0;
706 s++; if (s == send || (*s != 'Y' && *s != 'y')) return 0;
710 } else if (*s == 'N' || *s == 'n') {
711 /* XXX TODO: There are signaling NaNs and quiet NaNs. */
712 s++; if (s == send || (*s != 'A' && *s != 'a')) return 0;
713 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
720 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
721 numtype |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
723 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
724 numtype |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
725 } else if (s < send) {
726 /* we can have an optional exponent part */
727 if (*s == 'e' || *s == 'E') {
728 /* The only flag we keep is sign. Blow away any "it's UV" */
729 numtype &= IS_NUMBER_NEG;
730 numtype |= IS_NUMBER_NOT_INT;
732 if (s < send && (*s == '-' || *s == '+'))
734 if (s < send && isDIGIT(*s)) {
737 } while (s < send && isDIGIT(*s));
743 while (s < send && isSPACE(*s))
747 if (len == 10 && memEQ(pv, "0 but true", 10)) {
750 return IS_NUMBER_IN_UV;
756 S_mulexp10(NV value, I32 exponent)
768 /* On OpenVMS VAX we by default use the D_FLOAT double format,
769 * and that format does not have *easy* capabilities [1] for
770 * overflowing doubles 'silently' as IEEE fp does. We also need
771 * to support G_FLOAT on both VAX and Alpha, and though the exponent
772 * range is much larger than D_FLOAT it still doesn't do silent
773 * overflow. Therefore we need to detect early whether we would
774 * overflow (this is the behaviour of the native string-to-float
775 * conversion routines, and therefore of native applications, too).
777 * [1] Trying to establish a condition handler to trap floating point
778 * exceptions is not a good idea. */
780 /* In UNICOS and in certain Cray models (such as T90) there is no
781 * IEEE fp, and no way at all from C to catch fp overflows gracefully.
782 * There is something you can do if you are willing to use some
783 * inline assembler: the instruction is called DFI-- but that will
784 * disable *all* floating point interrupts, a little bit too large
785 * a hammer. Therefore we need to catch potential overflows before
788 #if ((defined(VMS) && !defined(__IEEE_FP)) || defined(_UNICOS)) && defined(NV_MAX_10_EXP)
790 const NV exp_v = log10(value);
791 if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP)
794 if (-(exponent + exp_v) >= NV_MAX_10_EXP)
796 while (-exponent >= NV_MAX_10_EXP) {
797 /* combination does not overflow, but 10^(-exponent) does */
807 exponent = -exponent;
809 for (bit = 1; exponent; bit <<= 1) {
810 if (exponent & bit) {
813 /* Floating point exceptions are supposed to be turned off,
814 * but if we're obviously done, don't risk another iteration.
816 if (exponent == 0) break;
820 return negative ? value / result : value * result;
824 Perl_my_atof(pTHX_ const char* s)
827 #ifdef USE_LOCALE_NUMERIC
830 PERL_ARGS_ASSERT_MY_ATOF;
832 if (PL_numeric_local && IN_LOCALE) {
835 /* Scan the number twice; once using locale and once without;
836 * choose the larger result (in absolute value). */
838 SET_NUMERIC_STANDARD();
841 if ((y < 0.0 && y < x) || (y > 0.0 && y > x))
853 Perl_my_atof2(pTHX_ const char* orig, NV* value)
855 NV result[3] = {0.0, 0.0, 0.0};
856 const char* s = orig;
858 UV accumulator[2] = {0,0}; /* before/after dp */
860 const char* send = s + strlen(orig) - 1;
862 I32 exp_adjust[2] = {0,0};
863 I32 exp_acc[2] = {-1, -1};
864 /* the current exponent adjust for the accumulators */
869 I32 sig_digits = 0; /* noof significant digits seen so far */
871 PERL_ARGS_ASSERT_MY_ATOF2;
873 /* There is no point in processing more significant digits
874 * than the NV can hold. Note that NV_DIG is a lower-bound value,
875 * while we need an upper-bound value. We add 2 to account for this;
876 * since it will have been conservative on both the first and last digit.
877 * For example a 32-bit mantissa with an exponent of 4 would have
878 * exact values in the set
886 * where for the purposes of calculating NV_DIG we would have to discount
887 * both the first and last digit, since neither can hold all values from
888 * 0..9; but for calculating the value we must examine those two digits.
890 #define MAX_SIG_DIGITS (NV_DIG+2)
892 /* the max number we can accumulate in a UV, and still safely do 10*N+9 */
893 #define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10))
895 /* leading whitespace */
908 /* punt to strtod for NaN/Inf; if no support for it there, tough luck */
911 if (*s == 'n' || *s == 'N' || *s == 'i' || *s == 'I') {
912 const char *p = negative ? s - 1 : s;
915 rslt = strtod(p, &endp);
923 /* we accumulate digits into an integer; when this becomes too
924 * large, we add the total to NV and start again */
934 /* don't start counting until we see the first significant
935 * digit, eg the 5 in 0.00005... */
936 if (!sig_digits && digit == 0)
939 if (++sig_digits > MAX_SIG_DIGITS) {
940 /* limits of precision reached */
942 ++accumulator[seen_dp];
943 } else if (digit == 5) {
944 if (old_digit % 2) { /* round to even - Allen */
945 ++accumulator[seen_dp];
953 /* skip remaining digits */
954 while (isDIGIT(*s)) {
960 /* warn of loss of precision? */
963 if (accumulator[seen_dp] > MAX_ACCUMULATE) {
964 /* add accumulator to result and start again */
965 result[seen_dp] = S_mulexp10(result[seen_dp],
967 + (NV)accumulator[seen_dp];
968 accumulator[seen_dp] = 0;
969 exp_acc[seen_dp] = 0;
971 accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit;
975 else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) {
977 if (sig_digits > MAX_SIG_DIGITS) {
980 } while (isDIGIT(*s));
989 result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0];
991 result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1];
994 if (seen_digit && (*s == 'e' || *s == 'E')) {
995 bool expnegative = 0;
1006 exponent = exponent * 10 + (*s++ - '0');
1008 exponent = -exponent;
1013 /* now apply the exponent */
1016 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0])
1017 + S_mulexp10(result[1],exponent-exp_adjust[1]);
1019 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]);
1022 /* now apply the sign */
1024 result[2] = -result[2];
1025 #endif /* USE_PERL_ATOF */
1030 #if ! defined(HAS_MODFL) && defined(HAS_AINTL) && defined(HAS_COPYSIGNL)
1032 Perl_my_modfl(long double x, long double *ip)
1035 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
1039 #if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
1041 Perl_my_frexpl(long double x, int *e) {
1042 *e = x == 0.0L ? 0 : ilogbl(x) + 1;
1043 return (scalbnl(x, -*e));
1048 =for apidoc Perl_signbit
1050 Return a non-zero integer if the sign bit on an NV is set, and 0 if
1053 If Configure detects this system has a signbit() that will work with
1054 our NVs, then we just use it via the #define in perl.h. Otherwise,
1055 fall back on this implementation. As a first pass, this gets everything
1056 right except -0.0. Alas, catching -0.0 is the main use for this function,
1057 so this is not too helpful yet. Still, at least we have the scaffolding
1058 in place to support other systems, should that prove useful.
1061 Configure notes: This function is called 'Perl_signbit' instead of a
1062 plain 'signbit' because it is easy to imagine a system having a signbit()
1063 function or macro that doesn't happen to work with our particular choice
1064 of NVs. We shouldn't just re-#define signbit as Perl_signbit and expect
1065 the standard system headers to be happy. Also, this is a no-context
1066 function (no pTHX_) because Perl_signbit() is usually re-#defined in
1067 perl.h as a simple macro call to the system's signbit().
1068 Users should just always call Perl_signbit().
1072 #if !defined(HAS_SIGNBIT)
1074 Perl_signbit(NV x) {
1075 return (x < 0.0) ? 1 : 0;
1081 * c-indentation-style: bsd
1083 * indent-tabs-mode: t
1086 * ex: set ts=8 sts=4 sw=4 noet: