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 if (ckWARN_d(WARN_OVERFLOW))
180 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
181 "Integer overflow in binary number");
183 value_nv = (NV) value;
186 /* If an NV has not enough bits in its mantissa to
187 * represent a UV this summing of small low-order numbers
188 * is a waste of time (because the NV cannot preserve
189 * the low-order bits anyway): we could just remember when
190 * did we overflow and in the end just multiply value_nv by the
192 value_nv += (NV)(bit - '0');
195 if (bit == '_' && len && allow_underscores && (bit = s[1])
196 && (bit == '0' || bit == '1'))
202 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
203 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
204 "Illegal binary digit '%c' ignored", *s);
208 if ( ( overflowed && value_nv > 4294967295.0)
210 || (!overflowed && value > 0xffffffff )
213 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
214 "Binary number > 0b11111111111111111111111111111111 non-portable");
221 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
230 converts a string representing a hex number to numeric form.
232 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
233 conversion flags, and I<result> should be NULL or a pointer to an NV.
234 The scan stops at the end of the string, or the first invalid character.
235 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
236 invalid character will also trigger a warning.
237 On return I<*len> is set to the length of the scanned string,
238 and I<*flags> gives output flags.
240 If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
241 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_hex>
242 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
243 and writes the value to I<*result> (or the value is discarded if I<result>
246 The hex number may optionally be prefixed with "0x" or "x" unless
247 C<PERL_SCAN_DISALLOW_PREFIX> is set in I<*flags> on entry. If
248 C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the hex
249 number may use '_' characters to separate digits.
255 Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result)
258 const char *s = start;
262 const UV max_div_16 = UV_MAX / 16;
263 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
264 bool overflowed = FALSE;
266 PERL_ARGS_ASSERT_GROK_HEX;
268 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
269 /* strip off leading x or 0x.
270 for compatibility silently suffer "x" and "0x" as valid hex numbers.
277 else if (len >= 2 && s[0] == '0' && s[1] == 'x') {
284 for (; len-- && *s; s++) {
285 const char *hexdigit = strchr(PL_hexdigit, *s);
287 /* Write it in this wonky order with a goto to attempt to get the
288 compiler to make the common case integer-only loop pretty tight.
289 With gcc seems to be much straighter code than old scan_hex. */
292 if (value <= max_div_16) {
293 value = (value << 4) | ((hexdigit - PL_hexdigit) & 15);
296 /* Bah. We're just overflowed. */
297 if (ckWARN_d(WARN_OVERFLOW))
298 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
299 "Integer overflow in hexadecimal number");
301 value_nv = (NV) value;
304 /* If an NV has not enough bits in its mantissa to
305 * represent a UV this summing of small low-order numbers
306 * is a waste of time (because the NV cannot preserve
307 * the low-order bits anyway): we could just remember when
308 * did we overflow and in the end just multiply value_nv by the
309 * right amount of 16-tuples. */
310 value_nv += (NV)((hexdigit - PL_hexdigit) & 15);
313 if (*s == '_' && len && allow_underscores && s[1]
314 && (hexdigit = strchr(PL_hexdigit, s[1])))
320 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT))
321 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
322 "Illegal hexadecimal digit '%c' ignored", *s);
326 if ( ( overflowed && value_nv > 4294967295.0)
328 || (!overflowed && value > 0xffffffff )
331 Perl_ck_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))
426 Perl_ck_warner(aTHX_ packWARN(WARN_DIGIT),
427 "Illegal octal digit '%c' ignored", *s);
432 if ( ( overflowed && value_nv > 4294967295.0)
434 || (!overflowed && value > 0xffffffff )
437 Perl_ck_warner(aTHX_ packWARN(WARN_PORTABLE),
438 "Octal number > 037777777777 non-portable");
445 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
454 For backwards compatibility. Use C<grok_bin> instead.
458 For backwards compatibility. Use C<grok_hex> instead.
462 For backwards compatibility. Use C<grok_oct> instead.
468 Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
471 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
472 const UV ruv = grok_bin (start, &len, &flags, &rnv);
474 PERL_ARGS_ASSERT_SCAN_BIN;
477 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
481 Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
484 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
485 const UV ruv = grok_oct (start, &len, &flags, &rnv);
487 PERL_ARGS_ASSERT_SCAN_OCT;
490 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
494 Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
497 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
498 const UV ruv = grok_hex (start, &len, &flags, &rnv);
500 PERL_ARGS_ASSERT_SCAN_HEX;
503 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
507 =for apidoc grok_numeric_radix
509 Scan and skip for a numeric decimal separator (radix).
514 Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send)
516 #ifdef USE_LOCALE_NUMERIC
519 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
521 if (PL_numeric_radix_sv && IN_LOCALE) {
523 const char * const radix = SvPV(PL_numeric_radix_sv, len);
524 if (*sp + len <= send && memEQ(*sp, radix, len)) {
529 /* always try "." if numeric radix didn't match because
530 * we may have data from different locales mixed */
533 PERL_ARGS_ASSERT_GROK_NUMERIC_RADIX;
535 if (*sp < send && **sp == '.') {
543 =for apidoc grok_number
545 Recognise (or not) a number. The type of the number is returned
546 (0 if unrecognised), otherwise it is a bit-ORed combination of
547 IS_NUMBER_IN_UV, IS_NUMBER_GREATER_THAN_UV_MAX, IS_NUMBER_NOT_INT,
548 IS_NUMBER_NEG, IS_NUMBER_INFINITY, IS_NUMBER_NAN (defined in perl.h).
550 If the value of the number can fit an in UV, it is returned in the *valuep
551 IS_NUMBER_IN_UV will be set to indicate that *valuep is valid, IS_NUMBER_IN_UV
552 will never be set unless *valuep is valid, but *valuep may have been assigned
553 to during processing even though IS_NUMBER_IN_UV is not set on return.
554 If valuep is NULL, IS_NUMBER_IN_UV will be set for the same cases as when
555 valuep is non-NULL, but no actual assignment (or SEGV) will occur.
557 IS_NUMBER_NOT_INT will be set with IS_NUMBER_IN_UV if trailing decimals were
558 seen (in which case *valuep gives the true value truncated to an integer), and
559 IS_NUMBER_NEG if the number is negative (in which case *valuep holds the
560 absolute value). IS_NUMBER_IN_UV is not set if e notation was used or the
561 number is larger than a UV.
566 Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep)
569 const char * const send = pv + len;
570 const UV max_div_10 = UV_MAX / 10;
571 const char max_mod_10 = UV_MAX % 10;
576 PERL_ARGS_ASSERT_GROK_NUMBER;
578 while (s < send && isSPACE(*s))
582 } else if (*s == '-') {
584 numtype = IS_NUMBER_NEG;
592 /* next must be digit or the radix separator or beginning of infinity */
594 /* UVs are at least 32 bits, so the first 9 decimal digits cannot
597 /* This construction seems to be more optimiser friendly.
598 (without it gcc does the isDIGIT test and the *s - '0' separately)
599 With it gcc on arm is managing 6 instructions (6 cycles) per digit.
600 In theory the optimiser could deduce how far to unroll the loop
601 before checking for overflow. */
603 int digit = *s - '0';
604 if (digit >= 0 && digit <= 9) {
605 value = value * 10 + digit;
608 if (digit >= 0 && digit <= 9) {
609 value = value * 10 + digit;
612 if (digit >= 0 && digit <= 9) {
613 value = value * 10 + digit;
616 if (digit >= 0 && digit <= 9) {
617 value = value * 10 + digit;
620 if (digit >= 0 && digit <= 9) {
621 value = value * 10 + digit;
624 if (digit >= 0 && digit <= 9) {
625 value = value * 10 + digit;
628 if (digit >= 0 && digit <= 9) {
629 value = value * 10 + digit;
632 if (digit >= 0 && digit <= 9) {
633 value = value * 10 + digit;
635 /* Now got 9 digits, so need to check
636 each time for overflow. */
638 while (digit >= 0 && digit <= 9
639 && (value < max_div_10
640 || (value == max_div_10
641 && digit <= max_mod_10))) {
642 value = value * 10 + digit;
648 if (digit >= 0 && digit <= 9
651 skip the remaining digits, don't
652 worry about setting *valuep. */
655 } while (s < send && isDIGIT(*s));
657 IS_NUMBER_GREATER_THAN_UV_MAX;
677 numtype |= IS_NUMBER_IN_UV;
682 if (GROK_NUMERIC_RADIX(&s, send)) {
683 numtype |= IS_NUMBER_NOT_INT;
684 while (s < send && isDIGIT(*s)) /* optional digits after the radix */
688 else if (GROK_NUMERIC_RADIX(&s, send)) {
689 numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */
690 /* no digits before the radix means we need digits after it */
691 if (s < send && isDIGIT(*s)) {
694 } while (s < send && isDIGIT(*s));
696 /* integer approximation is valid - it's 0. */
702 } else if (*s == 'I' || *s == 'i') {
703 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
704 s++; if (s == send || (*s != 'F' && *s != 'f')) return 0;
705 s++; if (s < send && (*s == 'I' || *s == 'i')) {
706 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
707 s++; if (s == send || (*s != 'I' && *s != 'i')) return 0;
708 s++; if (s == send || (*s != 'T' && *s != 't')) return 0;
709 s++; if (s == send || (*s != 'Y' && *s != 'y')) return 0;
713 } else if (*s == 'N' || *s == 'n') {
714 /* XXX TODO: There are signaling NaNs and quiet NaNs. */
715 s++; if (s == send || (*s != 'A' && *s != 'a')) return 0;
716 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
723 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
724 numtype |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
726 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
727 numtype |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
728 } else if (s < send) {
729 /* we can have an optional exponent part */
730 if (*s == 'e' || *s == 'E') {
731 /* The only flag we keep is sign. Blow away any "it's UV" */
732 numtype &= IS_NUMBER_NEG;
733 numtype |= IS_NUMBER_NOT_INT;
735 if (s < send && (*s == '-' || *s == '+'))
737 if (s < send && isDIGIT(*s)) {
740 } while (s < send && isDIGIT(*s));
746 while (s < send && isSPACE(*s))
750 if (len == 10 && memEQ(pv, "0 but true", 10)) {
753 return IS_NUMBER_IN_UV;
759 S_mulexp10(NV value, I32 exponent)
771 /* On OpenVMS VAX we by default use the D_FLOAT double format,
772 * and that format does not have *easy* capabilities [1] for
773 * overflowing doubles 'silently' as IEEE fp does. We also need
774 * to support G_FLOAT on both VAX and Alpha, and though the exponent
775 * range is much larger than D_FLOAT it still doesn't do silent
776 * overflow. Therefore we need to detect early whether we would
777 * overflow (this is the behaviour of the native string-to-float
778 * conversion routines, and therefore of native applications, too).
780 * [1] Trying to establish a condition handler to trap floating point
781 * exceptions is not a good idea. */
783 /* In UNICOS and in certain Cray models (such as T90) there is no
784 * IEEE fp, and no way at all from C to catch fp overflows gracefully.
785 * There is something you can do if you are willing to use some
786 * inline assembler: the instruction is called DFI-- but that will
787 * disable *all* floating point interrupts, a little bit too large
788 * a hammer. Therefore we need to catch potential overflows before
791 #if ((defined(VMS) && !defined(__IEEE_FP)) || defined(_UNICOS)) && defined(NV_MAX_10_EXP)
793 const NV exp_v = log10(value);
794 if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP)
797 if (-(exponent + exp_v) >= NV_MAX_10_EXP)
799 while (-exponent >= NV_MAX_10_EXP) {
800 /* combination does not overflow, but 10^(-exponent) does */
810 exponent = -exponent;
812 for (bit = 1; exponent; bit <<= 1) {
813 if (exponent & bit) {
816 /* Floating point exceptions are supposed to be turned off,
817 * but if we're obviously done, don't risk another iteration.
819 if (exponent == 0) break;
823 return negative ? value / result : value * result;
827 Perl_my_atof(pTHX_ const char* s)
830 #ifdef USE_LOCALE_NUMERIC
833 PERL_ARGS_ASSERT_MY_ATOF;
835 if (PL_numeric_local && IN_LOCALE) {
838 /* Scan the number twice; once using locale and once without;
839 * choose the larger result (in absolute value). */
841 SET_NUMERIC_STANDARD();
844 if ((y < 0.0 && y < x) || (y > 0.0 && y > x))
856 Perl_my_atof2(pTHX_ const char* orig, NV* value)
858 NV result[3] = {0.0, 0.0, 0.0};
859 const char* s = orig;
861 UV accumulator[2] = {0,0}; /* before/after dp */
863 const char* send = s + strlen(orig) - 1;
865 I32 exp_adjust[2] = {0,0};
866 I32 exp_acc[2] = {-1, -1};
867 /* the current exponent adjust for the accumulators */
872 I32 sig_digits = 0; /* noof significant digits seen so far */
874 PERL_ARGS_ASSERT_MY_ATOF2;
876 /* There is no point in processing more significant digits
877 * than the NV can hold. Note that NV_DIG is a lower-bound value,
878 * while we need an upper-bound value. We add 2 to account for this;
879 * since it will have been conservative on both the first and last digit.
880 * For example a 32-bit mantissa with an exponent of 4 would have
881 * exact values in the set
889 * where for the purposes of calculating NV_DIG we would have to discount
890 * both the first and last digit, since neither can hold all values from
891 * 0..9; but for calculating the value we must examine those two digits.
893 #define MAX_SIG_DIGITS (NV_DIG+2)
895 /* the max number we can accumulate in a UV, and still safely do 10*N+9 */
896 #define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10))
898 /* leading whitespace */
911 /* punt to strtod for NaN/Inf; if no support for it there, tough luck */
914 if (*s == 'n' || *s == 'N' || *s == 'i' || *s == 'I') {
915 const char *p = negative ? s - 1 : s;
918 rslt = strtod(p, &endp);
926 /* we accumulate digits into an integer; when this becomes too
927 * large, we add the total to NV and start again */
937 /* don't start counting until we see the first significant
938 * digit, eg the 5 in 0.00005... */
939 if (!sig_digits && digit == 0)
942 if (++sig_digits > MAX_SIG_DIGITS) {
943 /* limits of precision reached */
945 ++accumulator[seen_dp];
946 } else if (digit == 5) {
947 if (old_digit % 2) { /* round to even - Allen */
948 ++accumulator[seen_dp];
956 /* skip remaining digits */
957 while (isDIGIT(*s)) {
963 /* warn of loss of precision? */
966 if (accumulator[seen_dp] > MAX_ACCUMULATE) {
967 /* add accumulator to result and start again */
968 result[seen_dp] = S_mulexp10(result[seen_dp],
970 + (NV)accumulator[seen_dp];
971 accumulator[seen_dp] = 0;
972 exp_acc[seen_dp] = 0;
974 accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit;
978 else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) {
980 if (sig_digits > MAX_SIG_DIGITS) {
983 } while (isDIGIT(*s));
992 result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0];
994 result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1];
997 if (seen_digit && (*s == 'e' || *s == 'E')) {
998 bool expnegative = 0;
1009 exponent = exponent * 10 + (*s++ - '0');
1011 exponent = -exponent;
1016 /* now apply the exponent */
1019 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0])
1020 + S_mulexp10(result[1],exponent-exp_adjust[1]);
1022 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]);
1025 /* now apply the sign */
1027 result[2] = -result[2];
1028 #endif /* USE_PERL_ATOF */
1033 #if ! defined(HAS_MODFL) && defined(HAS_AINTL) && defined(HAS_COPYSIGNL)
1035 Perl_my_modfl(long double x, long double *ip)
1038 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
1042 #if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
1044 Perl_my_frexpl(long double x, int *e) {
1045 *e = x == 0.0L ? 0 : ilogbl(x) + 1;
1046 return (scalbnl(x, -*e));
1051 =for apidoc Perl_signbit
1053 Return a non-zero integer if the sign bit on an NV is set, and 0 if
1056 If Configure detects this system has a signbit() that will work with
1057 our NVs, then we just use it via the #define in perl.h. Otherwise,
1058 fall back on this implementation. As a first pass, this gets everything
1059 right except -0.0. Alas, catching -0.0 is the main use for this function,
1060 so this is not too helpful yet. Still, at least we have the scaffolding
1061 in place to support other systems, should that prove useful.
1064 Configure notes: This function is called 'Perl_signbit' instead of a
1065 plain 'signbit' because it is easy to imagine a system having a signbit()
1066 function or macro that doesn't happen to work with our particular choice
1067 of NVs. We shouldn't just re-#define signbit as Perl_signbit and expect
1068 the standard system headers to be happy. Also, this is a no-context
1069 function (no pTHX_) because Perl_signbit() is usually re-#defined in
1070 perl.h as a simple macro call to the system's signbit().
1071 Users should just always call Perl_signbit().
1075 #if !defined(HAS_SIGNBIT)
1077 Perl_signbit(NV x) {
1078 return (x < 0.0) ? 1 : 0;
1084 * c-indentation-style: bsd
1086 * indent-tabs-mode: t
1089 * ex: set ts=8 sts=4 sw=4 noet: