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, unless
13 * wizards count differently to other people."
17 =head1 Numeric functions
19 This file contains all the stuff needed by perl for manipulating numeric
20 values, including such things as replacements for the OS's atof() function
27 #define PERL_IN_NUMERIC_C
31 Perl_cast_ulong(pTHX_ NV f)
35 return f < I32_MIN ? (U32) I32_MIN : (U32)(I32) f;
38 if (f < U32_MAX_P1_HALF)
41 return ((U32) f) | (1 + U32_MAX >> 1);
46 return f > 0 ? U32_MAX : 0 /* NaN */;
50 Perl_cast_i32(pTHX_ NV f)
54 return f < I32_MIN ? I32_MIN : (I32) f;
57 if (f < U32_MAX_P1_HALF)
60 return (I32)(((U32) f) | (1 + U32_MAX >> 1));
65 return f > 0 ? (I32)U32_MAX : 0 /* NaN */;
69 Perl_cast_iv(pTHX_ NV f)
73 return f < IV_MIN ? IV_MIN : (IV) f;
76 /* For future flexibility allowing for sizeof(UV) >= sizeof(IV) */
77 if (f < UV_MAX_P1_HALF)
80 return (IV)(((UV) f) | (1 + UV_MAX >> 1));
85 return f > 0 ? (IV)UV_MAX : 0 /* NaN */;
89 Perl_cast_uv(pTHX_ NV f)
93 return f < IV_MIN ? (UV) IV_MIN : (UV)(IV) f;
96 if (f < UV_MAX_P1_HALF)
99 return ((UV) f) | (1 + UV_MAX >> 1);
104 return f > 0 ? UV_MAX : 0 /* NaN */;
110 converts a string representing a binary number to numeric form.
112 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
113 conversion flags, and I<result> should be NULL or a pointer to an NV.
114 The scan stops at the end of the string, or the first invalid character.
115 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
116 invalid character will also trigger a warning.
117 On return I<*len> is set to the length of the scanned string,
118 and I<*flags> gives output flags.
120 If the value is <= C<UV_MAX> it is returned as a UV, the output flags are clear,
121 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_bin>
122 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
123 and writes the value to I<*result> (or the value is discarded if I<result>
126 The binary number may optionally be prefixed with "0b" or "b" unless
127 C<PERL_SCAN_DISALLOW_PREFIX> is set in I<*flags> on entry. If
128 C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the binary
129 number may use '_' characters to separate digits.
135 Perl_grok_bin(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
137 const char *s = start;
142 const UV max_div_2 = UV_MAX / 2;
143 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
144 bool overflowed = FALSE;
147 PERL_ARGS_ASSERT_GROK_BIN;
149 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
150 /* strip off leading b or 0b.
151 for compatibility silently suffer "b" and "0b" as valid binary
158 else if (len >= 2 && s[0] == '0' && s[1] == 'b') {
165 for (; len-- && (bit = *s); s++) {
166 if (bit == '0' || bit == '1') {
167 /* Write it in this wonky order with a goto to attempt to get the
168 compiler to make the common case integer-only loop pretty tight.
169 With gcc seems to be much straighter code than old scan_bin. */
172 if (value <= max_div_2) {
173 value = (value << 1) | (bit - '0');
176 /* Bah. We're just overflowed. */
177 if (ckWARN_d(WARN_OVERFLOW))
178 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
179 "Integer overflow in binary number");
181 value_nv = (NV) value;
184 /* If an NV has not enough bits in its mantissa to
185 * represent a UV this summing of small low-order numbers
186 * is a waste of time (because the NV cannot preserve
187 * the low-order bits anyway): we could just remember when
188 * did we overflow and in the end just multiply value_nv by the
190 value_nv += (NV)(bit - '0');
193 if (bit == '_' && len && allow_underscores && (bit = s[1])
194 && (bit == '0' || bit == '1'))
200 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
201 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
202 "Illegal binary digit '%c' ignored", *s);
206 if ( ( overflowed && value_nv > 4294967295.0)
208 || (!overflowed && value > 0xffffffff )
211 if (ckWARN(WARN_PORTABLE))
212 Perl_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 if (ckWARN_d(WARN_OVERFLOW))
297 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
298 "Integer overflow in hexadecimal number");
300 value_nv = (NV) value;
303 /* If an NV has not enough bits in its mantissa to
304 * represent a UV this summing of small low-order numbers
305 * is a waste of time (because the NV cannot preserve
306 * the low-order bits anyway): we could just remember when
307 * did we overflow and in the end just multiply value_nv by the
308 * right amount of 16-tuples. */
309 value_nv += (NV)((hexdigit - PL_hexdigit) & 15);
312 if (*s == '_' && len && allow_underscores && s[1]
313 && (hexdigit = strchr(PL_hexdigit, s[1])))
319 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
320 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
321 "Illegal hexadecimal digit '%c' ignored", *s);
325 if ( ( overflowed && value_nv > 4294967295.0)
327 || (!overflowed && value > 0xffffffff )
330 if (ckWARN(WARN_PORTABLE))
331 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
332 "Hexadecimal number > 0xffffffff non-portable");
339 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
348 converts a string representing an octal number to numeric form.
350 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
351 conversion flags, and I<result> should be NULL or a pointer to an NV.
352 The scan stops at the end of the string, or the first invalid character.
353 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
354 invalid character will also trigger a warning.
355 On return I<*len> is set to the length of the scanned string,
356 and I<*flags> gives output flags.
358 If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
359 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_oct>
360 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
361 and writes the value to I<*result> (or the value is discarded if I<result>
364 If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the octal
365 number may use '_' characters to separate digits.
371 Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
373 const char *s = start;
377 const UV max_div_8 = UV_MAX / 8;
378 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
379 bool overflowed = FALSE;
381 PERL_ARGS_ASSERT_GROK_OCT;
383 for (; len-- && *s; s++) {
384 /* gcc 2.95 optimiser not smart enough to figure that this subtraction
385 out front allows slicker code. */
386 int digit = *s - '0';
387 if (digit >= 0 && digit <= 7) {
388 /* Write it in this wonky order with a goto to attempt to get the
389 compiler to make the common case integer-only loop pretty tight.
393 if (value <= max_div_8) {
394 value = (value << 3) | digit;
397 /* Bah. We're just overflowed. */
398 if (ckWARN_d(WARN_OVERFLOW))
399 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
400 "Integer overflow in octal number");
402 value_nv = (NV) value;
405 /* If an NV has not enough bits in its mantissa to
406 * represent a UV this summing of small low-order numbers
407 * is a waste of time (because the NV cannot preserve
408 * the low-order bits anyway): we could just remember when
409 * did we overflow and in the end just multiply value_nv by the
410 * right amount of 8-tuples. */
411 value_nv += (NV)digit;
414 if (digit == ('_' - '0') && len && allow_underscores
415 && (digit = s[1] - '0') && (digit >= 0 && digit <= 7))
421 /* Allow \octal to work the DWIM way (that is, stop scanning
422 * as soon as non-octal characters are seen, complain only if
423 * someone seems to want to use the digits eight and nine). */
424 if (digit == 8 || digit == 9) {
425 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
426 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
427 "Illegal octal digit '%c' ignored", *s);
432 if ( ( overflowed && value_nv > 4294967295.0)
434 || (!overflowed && value > 0xffffffff )
437 if (ckWARN(WARN_PORTABLE))
438 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
439 "Octal number > 037777777777 non-portable");
446 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
455 For backwards compatibility. Use C<grok_bin> instead.
459 For backwards compatibility. Use C<grok_hex> instead.
463 For backwards compatibility. Use C<grok_oct> instead.
469 Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
472 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
473 const UV ruv = grok_bin (start, &len, &flags, &rnv);
475 PERL_ARGS_ASSERT_SCAN_BIN;
478 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
482 Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
485 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
486 const UV ruv = grok_oct (start, &len, &flags, &rnv);
488 PERL_ARGS_ASSERT_SCAN_OCT;
491 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
495 Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
498 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
499 const UV ruv = grok_hex (start, &len, &flags, &rnv);
501 PERL_ARGS_ASSERT_SCAN_HEX;
504 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
508 =for apidoc grok_numeric_radix
510 Scan and skip for a numeric decimal separator (radix).
515 Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send)
517 #ifdef USE_LOCALE_NUMERIC
520 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
522 if (PL_numeric_radix_sv && IN_LOCALE) {
524 const char * const radix = SvPV(PL_numeric_radix_sv, len);
525 if (*sp + len <= send && memEQ(*sp, radix, len)) {
530 /* always try "." if numeric radix didn't match because
531 * we may have data from different locales mixed */
534 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
536 if (*sp < send && **sp == '.') {
544 =for apidoc grok_number
546 Recognise (or not) a number. The type of the number is returned
547 (0 if unrecognised), otherwise it is a bit-ORed combination of
548 IS_NUMBER_IN_UV, IS_NUMBER_GREATER_THAN_UV_MAX, IS_NUMBER_NOT_INT,
549 IS_NUMBER_NEG, IS_NUMBER_INFINITY, IS_NUMBER_NAN (defined in perl.h).
551 If the value of the number can fit an in UV, it is returned in the *valuep
552 IS_NUMBER_IN_UV will be set to indicate that *valuep is valid, IS_NUMBER_IN_UV
553 will never be set unless *valuep is valid, but *valuep may have been assigned
554 to during processing even though IS_NUMBER_IN_UV is not set on return.
555 If valuep is NULL, IS_NUMBER_IN_UV will be set for the same cases as when
556 valuep is non-NULL, but no actual assignment (or SEGV) will occur.
558 IS_NUMBER_NOT_INT will be set with IS_NUMBER_IN_UV if trailing decimals were
559 seen (in which case *valuep gives the true value truncated to an integer), and
560 IS_NUMBER_NEG if the number is negative (in which case *valuep holds the
561 absolute value). IS_NUMBER_IN_UV is not set if e notation was used or the
562 number is larger than a UV.
567 Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep)
570 const char * const send = pv + len;
571 const UV max_div_10 = UV_MAX / 10;
572 const char max_mod_10 = UV_MAX % 10;
577 PERL_ARGS_ASSERT_GROK_NUMBER;
579 while (s < send && isSPACE(*s))
583 } else if (*s == '-') {
585 numtype = IS_NUMBER_NEG;
593 /* next must be digit or the radix separator or beginning of infinity */
595 /* UVs are at least 32 bits, so the first 9 decimal digits cannot
598 /* This construction seems to be more optimiser friendly.
599 (without it gcc does the isDIGIT test and the *s - '0' separately)
600 With it gcc on arm is managing 6 instructions (6 cycles) per digit.
601 In theory the optimiser could deduce how far to unroll the loop
602 before checking for overflow. */
604 int digit = *s - '0';
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;
633 if (digit >= 0 && digit <= 9) {
634 value = value * 10 + digit;
636 /* Now got 9 digits, so need to check
637 each time for overflow. */
639 while (digit >= 0 && digit <= 9
640 && (value < max_div_10
641 || (value == max_div_10
642 && digit <= max_mod_10))) {
643 value = value * 10 + digit;
649 if (digit >= 0 && digit <= 9
652 skip the remaining digits, don't
653 worry about setting *valuep. */
656 } while (s < send && isDIGIT(*s));
658 IS_NUMBER_GREATER_THAN_UV_MAX;
678 numtype |= IS_NUMBER_IN_UV;
683 if (GROK_NUMERIC_RADIX(&s, send)) {
684 numtype |= IS_NUMBER_NOT_INT;
685 while (s < send && isDIGIT(*s)) /* optional digits after the radix */
689 else if (GROK_NUMERIC_RADIX(&s, send)) {
690 numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */
691 /* no digits before the radix means we need digits after it */
692 if (s < send && isDIGIT(*s)) {
695 } while (s < send && isDIGIT(*s));
697 /* integer approximation is valid - it's 0. */
703 } else if (*s == 'I' || *s == 'i') {
704 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
705 s++; if (s == send || (*s != 'F' && *s != 'f')) return 0;
706 s++; if (s < send && (*s == 'I' || *s == 'i')) {
707 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
708 s++; if (s == send || (*s != 'I' && *s != 'i')) return 0;
709 s++; if (s == send || (*s != 'T' && *s != 't')) return 0;
710 s++; if (s == send || (*s != 'Y' && *s != 'y')) return 0;
714 } else if (*s == 'N' || *s == 'n') {
715 /* XXX TODO: There are signaling NaNs and quiet NaNs. */
716 s++; if (s == send || (*s != 'A' && *s != 'a')) return 0;
717 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
724 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
725 numtype |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
727 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
728 numtype |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
729 } else if (s < send) {
730 /* we can have an optional exponent part */
731 if (*s == 'e' || *s == 'E') {
732 /* The only flag we keep is sign. Blow away any "it's UV" */
733 numtype &= IS_NUMBER_NEG;
734 numtype |= IS_NUMBER_NOT_INT;
736 if (s < send && (*s == '-' || *s == '+'))
738 if (s < send && isDIGIT(*s)) {
741 } while (s < send && isDIGIT(*s));
747 while (s < send && isSPACE(*s))
751 if (len == 10 && memEQ(pv, "0 but true", 10)) {
754 return IS_NUMBER_IN_UV;
760 S_mulexp10(NV value, I32 exponent)
772 /* On OpenVMS VAX we by default use the D_FLOAT double format,
773 * and that format does not have *easy* capabilities [1] for
774 * overflowing doubles 'silently' as IEEE fp does. We also need
775 * to support G_FLOAT on both VAX and Alpha, and though the exponent
776 * range is much larger than D_FLOAT it still doesn't do silent
777 * overflow. Therefore we need to detect early whether we would
778 * overflow (this is the behaviour of the native string-to-float
779 * conversion routines, and therefore of native applications, too).
781 * [1] Trying to establish a condition handler to trap floating point
782 * exceptions is not a good idea. */
784 /* In UNICOS and in certain Cray models (such as T90) there is no
785 * IEEE fp, and no way at all from C to catch fp overflows gracefully.
786 * There is something you can do if you are willing to use some
787 * inline assembler: the instruction is called DFI-- but that will
788 * disable *all* floating point interrupts, a little bit too large
789 * a hammer. Therefore we need to catch potential overflows before
792 #if ((defined(VMS) && !defined(__IEEE_FP)) || defined(_UNICOS)) && defined(NV_MAX_10_EXP)
794 const NV exp_v = log10(value);
795 if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP)
798 if (-(exponent + exp_v) >= NV_MAX_10_EXP)
800 while (-exponent >= NV_MAX_10_EXP) {
801 /* combination does not overflow, but 10^(-exponent) does */
811 exponent = -exponent;
813 for (bit = 1; exponent; bit <<= 1) {
814 if (exponent & bit) {
817 /* Floating point exceptions are supposed to be turned off,
818 * but if we're obviously done, don't risk another iteration.
820 if (exponent == 0) break;
824 return negative ? value / result : value * result;
828 Perl_my_atof(pTHX_ const char* s)
831 #ifdef USE_LOCALE_NUMERIC
834 PERL_ARGS_ASSERT_MY_ATOF;
836 if (PL_numeric_local && IN_LOCALE) {
839 /* Scan the number twice; once using locale and once without;
840 * choose the larger result (in absolute value). */
842 SET_NUMERIC_STANDARD();
845 if ((y < 0.0 && y < x) || (y > 0.0 && y > x))
857 Perl_my_atof2(pTHX_ const char* orig, NV* value)
859 NV result[3] = {0.0, 0.0, 0.0};
860 const char* s = orig;
862 UV accumulator[2] = {0,0}; /* before/after dp */
864 const char* send = s + strlen(orig) - 1;
866 I32 exp_adjust[2] = {0,0};
867 I32 exp_acc[2] = {-1, -1};
868 /* the current exponent adjust for the accumulators */
873 I32 sig_digits = 0; /* noof significant digits seen so far */
875 PERL_ARGS_ASSERT_MY_ATOF2;
877 /* There is no point in processing more significant digits
878 * than the NV can hold. Note that NV_DIG is a lower-bound value,
879 * while we need an upper-bound value. We add 2 to account for this;
880 * since it will have been conservative on both the first and last digit.
881 * For example a 32-bit mantissa with an exponent of 4 would have
882 * exact values in the set
890 * where for the purposes of calculating NV_DIG we would have to discount
891 * both the first and last digit, since neither can hold all values from
892 * 0..9; but for calculating the value we must examine those two digits.
894 #define MAX_SIG_DIGITS (NV_DIG+2)
896 /* the max number we can accumulate in a UV, and still safely do 10*N+9 */
897 #define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10))
899 /* leading whitespace */
912 /* punt to strtod for NaN/Inf; if no support for it there, tough luck */
915 if (*s == 'n' || *s == 'N' || *s == 'i' || *s == 'I') {
916 const char *p = negative ? s - 1 : s;
919 rslt = strtod(p, &endp);
927 /* we accumulate digits into an integer; when this becomes too
928 * large, we add the total to NV and start again */
938 /* don't start counting until we see the first significant
939 * digit, eg the 5 in 0.00005... */
940 if (!sig_digits && digit == 0)
943 if (++sig_digits > MAX_SIG_DIGITS) {
944 /* limits of precision reached */
946 ++accumulator[seen_dp];
947 } else if (digit == 5) {
948 if (old_digit % 2) { /* round to even - Allen */
949 ++accumulator[seen_dp];
957 /* skip remaining digits */
958 while (isDIGIT(*s)) {
964 /* warn of loss of precision? */
967 if (accumulator[seen_dp] > MAX_ACCUMULATE) {
968 /* add accumulator to result and start again */
969 result[seen_dp] = S_mulexp10(result[seen_dp],
971 + (NV)accumulator[seen_dp];
972 accumulator[seen_dp] = 0;
973 exp_acc[seen_dp] = 0;
975 accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit;
979 else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) {
981 if (sig_digits > MAX_SIG_DIGITS) {
984 } while (isDIGIT(*s));
993 result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0];
995 result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1];
998 if (seen_digit && (*s == 'e' || *s == 'E')) {
999 bool expnegative = 0;
1010 exponent = exponent * 10 + (*s++ - '0');
1012 exponent = -exponent;
1017 /* now apply the exponent */
1020 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0])
1021 + S_mulexp10(result[1],exponent-exp_adjust[1]);
1023 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]);
1026 /* now apply the sign */
1028 result[2] = -result[2];
1029 #endif /* USE_PERL_ATOF */
1034 #if ! defined(HAS_MODFL) && defined(HAS_AINTL) && defined(HAS_COPYSIGNL)
1036 Perl_my_modfl(long double x, long double *ip)
1039 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
1043 #if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
1045 Perl_my_frexpl(long double x, int *e) {
1046 *e = x == 0.0L ? 0 : ilogbl(x) + 1;
1047 return (scalbnl(x, -*e));
1052 =for apidoc Perl_signbit
1054 Return a non-zero integer if the sign bit on an NV is set, and 0 if
1057 If Configure detects this system has a signbit() that will work with
1058 our NVs, then we just use it via the #define in perl.h. Otherwise,
1059 fall back on this implementation. As a first pass, this gets everything
1060 right except -0.0. Alas, catching -0.0 is the main use for this function,
1061 so this is not too helpful yet. Still, at least we have the scaffolding
1062 in place to support other systems, should that prove useful.
1065 Configure notes: This function is called 'Perl_signbit' instead of a
1066 plain 'signbit' because it is easy to imagine a system having a signbit()
1067 function or macro that doesn't happen to work with our particular choice
1068 of NVs. We shouldn't just re-#define signbit as Perl_signbit and expect
1069 the standard system headers to be happy. Also, this is a no-context
1070 function (no pTHX_) because Perl_signbit() is usually re-#defined in
1071 perl.h as a simple macro call to the system's signbit().
1072 Users should just always call Perl_signbit().
1076 #if !defined(HAS_SIGNBIT)
1078 Perl_signbit(NV x) {
1079 return (x < 0.0) ? 1 : 0;
1085 * c-indentation-style: bsd
1087 * indent-tabs-mode: t
1090 * ex: set ts=8 sts=4 sw=4 noet: