3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 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) {
136 const char *s = start;
141 const UV max_div_2 = UV_MAX / 2;
142 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
143 bool overflowed = FALSE;
146 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
147 /* strip off leading b or 0b.
148 for compatibility silently suffer "b" and "0b" as valid binary
155 else if (len >= 2 && s[0] == '0' && s[1] == 'b') {
162 for (; len-- && (bit = *s); s++) {
163 if (bit == '0' || bit == '1') {
164 /* Write it in this wonky order with a goto to attempt to get the
165 compiler to make the common case integer-only loop pretty tight.
166 With gcc seems to be much straighter code than old scan_bin. */
169 if (value <= max_div_2) {
170 value = (value << 1) | (bit - '0');
173 /* Bah. We're just overflowed. */
174 if (ckWARN_d(WARN_OVERFLOW))
175 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
176 "Integer overflow in binary number");
178 value_nv = (NV) value;
181 /* If an NV has not enough bits in its mantissa to
182 * represent a UV this summing of small low-order numbers
183 * is a waste of time (because the NV cannot preserve
184 * the low-order bits anyway): we could just remember when
185 * did we overflow and in the end just multiply value_nv by the
187 value_nv += (NV)(bit - '0');
190 if (bit == '_' && len && allow_underscores && (bit = s[1])
191 && (bit == '0' || bit == '1'))
197 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
198 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
199 "Illegal binary digit '%c' ignored", *s);
203 if ( ( overflowed && value_nv > 4294967295.0)
205 || (!overflowed && value > 0xffffffff )
208 if (ckWARN(WARN_PORTABLE))
209 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
210 "Binary number > 0b11111111111111111111111111111111 non-portable");
217 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
226 converts a string representing a hex number to numeric form.
228 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
229 conversion flags, and I<result> should be NULL or a pointer to an NV.
230 The scan stops at the end of the string, or the first invalid character.
231 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
232 invalid character will also trigger a warning.
233 On return I<*len> is set to the length of the scanned string,
234 and I<*flags> gives output flags.
236 If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
237 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_hex>
238 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
239 and writes the value to I<*result> (or the value is discarded if I<result>
242 The hex number may optionally be prefixed with "0x" or "x" unless
243 C<PERL_SCAN_DISALLOW_PREFIX> is set in I<*flags> on entry. If
244 C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the hex
245 number may use '_' characters to separate digits.
251 Perl_grok_hex(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) {
253 const char *s = start;
258 const UV max_div_16 = UV_MAX / 16;
259 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
260 bool overflowed = FALSE;
262 if (!(*flags & PERL_SCAN_DISALLOW_PREFIX)) {
263 /* strip off leading x or 0x.
264 for compatibility silently suffer "x" and "0x" as valid hex numbers.
271 else if (len >= 2 && s[0] == '0' && s[1] == 'x') {
278 for (; len-- && *s; s++) {
279 const char *hexdigit = strchr(PL_hexdigit, *s);
281 /* Write it in this wonky order with a goto to attempt to get the
282 compiler to make the common case integer-only loop pretty tight.
283 With gcc seems to be much straighter code than old scan_hex. */
286 if (value <= max_div_16) {
287 value = (value << 4) | ((hexdigit - PL_hexdigit) & 15);
290 /* Bah. We're just overflowed. */
291 if (ckWARN_d(WARN_OVERFLOW))
292 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
293 "Integer overflow in hexadecimal number");
295 value_nv = (NV) value;
298 /* If an NV has not enough bits in its mantissa to
299 * represent a UV this summing of small low-order numbers
300 * is a waste of time (because the NV cannot preserve
301 * the low-order bits anyway): we could just remember when
302 * did we overflow and in the end just multiply value_nv by the
303 * right amount of 16-tuples. */
304 value_nv += (NV)((hexdigit - PL_hexdigit) & 15);
307 if (*s == '_' && len && allow_underscores && s[1]
308 && (hexdigit = strchr(PL_hexdigit, s[1])))
314 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
315 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
316 "Illegal hexadecimal digit '%c' ignored", *s);
320 if ( ( overflowed && value_nv > 4294967295.0)
322 || (!overflowed && value > 0xffffffff )
325 if (ckWARN(WARN_PORTABLE))
326 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
327 "Hexadecimal number > 0xffffffff non-portable");
334 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
343 converts a string representing an octal number to numeric form.
345 On entry I<start> and I<*len> give the string to scan, I<*flags> gives
346 conversion flags, and I<result> should be NULL or a pointer to an NV.
347 The scan stops at the end of the string, or the first invalid character.
348 Unless C<PERL_SCAN_SILENT_ILLDIGIT> is set in I<*flags>, encountering an
349 invalid character will also trigger a warning.
350 On return I<*len> is set to the length of the scanned string,
351 and I<*flags> gives output flags.
353 If the value is <= UV_MAX it is returned as a UV, the output flags are clear,
354 and nothing is written to I<*result>. If the value is > UV_MAX C<grok_oct>
355 returns UV_MAX, sets C<PERL_SCAN_GREATER_THAN_UV_MAX> in the output flags,
356 and writes the value to I<*result> (or the value is discarded if I<result>
359 If C<PERL_SCAN_ALLOW_UNDERSCORES> is set in I<*flags> then the octal
360 number may use '_' characters to separate digits.
366 Perl_grok_oct(pTHX_ const char *start, STRLEN *len_p, I32 *flags, NV *result) {
367 const char *s = start;
372 const UV max_div_8 = UV_MAX / 8;
373 const bool allow_underscores = (bool)(*flags & PERL_SCAN_ALLOW_UNDERSCORES);
374 bool overflowed = FALSE;
376 for (; len-- && *s; s++) {
377 /* gcc 2.95 optimiser not smart enough to figure that this subtraction
378 out front allows slicker code. */
379 int digit = *s - '0';
380 if (digit >= 0 && digit <= 7) {
381 /* Write it in this wonky order with a goto to attempt to get the
382 compiler to make the common case integer-only loop pretty tight.
386 if (value <= max_div_8) {
387 value = (value << 3) | digit;
390 /* Bah. We're just overflowed. */
391 if (ckWARN_d(WARN_OVERFLOW))
392 Perl_warner(aTHX_ packWARN(WARN_OVERFLOW),
393 "Integer overflow in octal number");
395 value_nv = (NV) value;
398 /* If an NV has not enough bits in its mantissa to
399 * represent a UV this summing of small low-order numbers
400 * is a waste of time (because the NV cannot preserve
401 * the low-order bits anyway): we could just remember when
402 * did we overflow and in the end just multiply value_nv by the
403 * right amount of 8-tuples. */
404 value_nv += (NV)digit;
407 if (digit == ('_' - '0') && len && allow_underscores
408 && (digit = s[1] - '0') && (digit >= 0 && digit <= 7))
414 /* Allow \octal to work the DWIM way (that is, stop scanning
415 * as soon as non-octal characters are seen, complain only if
416 * someone seems to want to use the digits eight and nine). */
417 if (digit == 8 || digit == 9) {
418 if (!(*flags & PERL_SCAN_SILENT_ILLDIGIT) && ckWARN(WARN_DIGIT))
419 Perl_warner(aTHX_ packWARN(WARN_DIGIT),
420 "Illegal octal digit '%c' ignored", *s);
425 if ( ( overflowed && value_nv > 4294967295.0)
427 || (!overflowed && value > 0xffffffff )
430 if (ckWARN(WARN_PORTABLE))
431 Perl_warner(aTHX_ packWARN(WARN_PORTABLE),
432 "Octal number > 037777777777 non-portable");
439 *flags = PERL_SCAN_GREATER_THAN_UV_MAX;
448 For backwards compatibility. Use C<grok_bin> instead.
452 For backwards compatibility. Use C<grok_hex> instead.
456 For backwards compatibility. Use C<grok_oct> instead.
462 Perl_scan_bin(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
465 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
466 const UV ruv = grok_bin (start, &len, &flags, &rnv);
469 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
473 Perl_scan_oct(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
476 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
477 const UV ruv = grok_oct (start, &len, &flags, &rnv);
480 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
484 Perl_scan_hex(pTHX_ const char *start, STRLEN len, STRLEN *retlen)
487 I32 flags = *retlen ? PERL_SCAN_ALLOW_UNDERSCORES : 0;
488 const UV ruv = grok_hex (start, &len, &flags, &rnv);
491 return (flags & PERL_SCAN_GREATER_THAN_UV_MAX) ? rnv : (NV)ruv;
495 =for apidoc grok_numeric_radix
497 Scan and skip for a numeric decimal separator (radix).
502 Perl_grok_numeric_radix(pTHX_ const char **sp, const char *send)
504 #ifdef USE_LOCALE_NUMERIC
506 if (PL_numeric_radix_sv && IN_LOCALE) {
508 const char * const radix = SvPV(PL_numeric_radix_sv, len);
509 if (*sp + len <= send && memEQ(*sp, radix, len)) {
514 /* always try "." if numeric radix didn't match because
515 * we may have data from different locales mixed */
517 if (*sp < send && **sp == '.') {
525 =for apidoc grok_number
527 Recognise (or not) a number. The type of the number is returned
528 (0 if unrecognised), otherwise it is a bit-ORed combination of
529 IS_NUMBER_IN_UV, IS_NUMBER_GREATER_THAN_UV_MAX, IS_NUMBER_NOT_INT,
530 IS_NUMBER_NEG, IS_NUMBER_INFINITY, IS_NUMBER_NAN (defined in perl.h).
532 If the value of the number can fit an in UV, it is returned in the *valuep
533 IS_NUMBER_IN_UV will be set to indicate that *valuep is valid, IS_NUMBER_IN_UV
534 will never be set unless *valuep is valid, but *valuep may have been assigned
535 to during processing even though IS_NUMBER_IN_UV is not set on return.
536 If valuep is NULL, IS_NUMBER_IN_UV will be set for the same cases as when
537 valuep is non-NULL, but no actual assignment (or SEGV) will occur.
539 IS_NUMBER_NOT_INT will be set with IS_NUMBER_IN_UV if trailing decimals were
540 seen (in which case *valuep gives the true value truncated to an integer), and
541 IS_NUMBER_NEG if the number is negative (in which case *valuep holds the
542 absolute value). IS_NUMBER_IN_UV is not set if e notation was used or the
543 number is larger than a UV.
548 Perl_grok_number(pTHX_ const char *pv, STRLEN len, UV *valuep)
551 const char * const send = pv + len;
552 const UV max_div_10 = UV_MAX / 10;
553 const char max_mod_10 = UV_MAX % 10;
558 while (s < send && isSPACE(*s))
562 } else if (*s == '-') {
564 numtype = IS_NUMBER_NEG;
572 /* next must be digit or the radix separator or beginning of infinity */
574 /* UVs are at least 32 bits, so the first 9 decimal digits cannot
577 /* This construction seems to be more optimiser friendly.
578 (without it gcc does the isDIGIT test and the *s - '0' separately)
579 With it gcc on arm is managing 6 instructions (6 cycles) per digit.
580 In theory the optimiser could deduce how far to unroll the loop
581 before checking for overflow. */
583 int digit = *s - '0';
584 if (digit >= 0 && digit <= 9) {
585 value = value * 10 + digit;
588 if (digit >= 0 && digit <= 9) {
589 value = value * 10 + digit;
592 if (digit >= 0 && digit <= 9) {
593 value = value * 10 + digit;
596 if (digit >= 0 && digit <= 9) {
597 value = value * 10 + digit;
600 if (digit >= 0 && digit <= 9) {
601 value = value * 10 + digit;
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;
615 /* Now got 9 digits, so need to check
616 each time for overflow. */
618 while (digit >= 0 && digit <= 9
619 && (value < max_div_10
620 || (value == max_div_10
621 && digit <= max_mod_10))) {
622 value = value * 10 + digit;
628 if (digit >= 0 && digit <= 9
631 skip the remaining digits, don't
632 worry about setting *valuep. */
635 } while (s < send && isDIGIT(*s));
637 IS_NUMBER_GREATER_THAN_UV_MAX;
657 numtype |= IS_NUMBER_IN_UV;
662 if (GROK_NUMERIC_RADIX(&s, send)) {
663 numtype |= IS_NUMBER_NOT_INT;
664 while (s < send && isDIGIT(*s)) /* optional digits after the radix */
668 else if (GROK_NUMERIC_RADIX(&s, send)) {
669 numtype |= IS_NUMBER_NOT_INT | IS_NUMBER_IN_UV; /* valuep assigned below */
670 /* no digits before the radix means we need digits after it */
671 if (s < send && isDIGIT(*s)) {
674 } while (s < send && isDIGIT(*s));
676 /* integer approximation is valid - it's 0. */
682 } else if (*s == 'I' || *s == 'i') {
683 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
684 s++; if (s == send || (*s != 'F' && *s != 'f')) return 0;
685 s++; if (s < send && (*s == 'I' || *s == 'i')) {
686 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
687 s++; if (s == send || (*s != 'I' && *s != 'i')) return 0;
688 s++; if (s == send || (*s != 'T' && *s != 't')) return 0;
689 s++; if (s == send || (*s != 'Y' && *s != 'y')) return 0;
693 } else if (*s == 'N' || *s == 'n') {
694 /* XXX TODO: There are signaling NaNs and quiet NaNs. */
695 s++; if (s == send || (*s != 'A' && *s != 'a')) return 0;
696 s++; if (s == send || (*s != 'N' && *s != 'n')) return 0;
703 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
704 numtype |= IS_NUMBER_INFINITY | IS_NUMBER_NOT_INT;
706 numtype &= IS_NUMBER_NEG; /* Keep track of sign */
707 numtype |= IS_NUMBER_NAN | IS_NUMBER_NOT_INT;
708 } else if (s < send) {
709 /* we can have an optional exponent part */
710 if (*s == 'e' || *s == 'E') {
711 /* The only flag we keep is sign. Blow away any "it's UV" */
712 numtype &= IS_NUMBER_NEG;
713 numtype |= IS_NUMBER_NOT_INT;
715 if (s < send && (*s == '-' || *s == '+'))
717 if (s < send && isDIGIT(*s)) {
720 } while (s < send && isDIGIT(*s));
726 while (s < send && isSPACE(*s))
730 if (len == 10 && memEQ(pv, "0 but true", 10)) {
733 return IS_NUMBER_IN_UV;
739 S_mulexp10(NV value, I32 exponent)
751 /* On OpenVMS VAX we by default use the D_FLOAT double format,
752 * and that format does not have *easy* capabilities [1] for
753 * overflowing doubles 'silently' as IEEE fp does. We also need
754 * to support G_FLOAT on both VAX and Alpha, and though the exponent
755 * range is much larger than D_FLOAT it still doesn't do silent
756 * overflow. Therefore we need to detect early whether we would
757 * overflow (this is the behaviour of the native string-to-float
758 * conversion routines, and therefore of native applications, too).
760 * [1] Trying to establish a condition handler to trap floating point
761 * exceptions is not a good idea. */
763 /* In UNICOS and in certain Cray models (such as T90) there is no
764 * IEEE fp, and no way at all from C to catch fp overflows gracefully.
765 * There is something you can do if you are willing to use some
766 * inline assembler: the instruction is called DFI-- but that will
767 * disable *all* floating point interrupts, a little bit too large
768 * a hammer. Therefore we need to catch potential overflows before
771 #if ((defined(VMS) && !defined(__IEEE_FP)) || defined(_UNICOS)) && defined(NV_MAX_10_EXP)
773 const NV exp_v = log10(value);
774 if (exponent >= NV_MAX_10_EXP || exponent + exp_v >= NV_MAX_10_EXP)
777 if (-(exponent + exp_v) >= NV_MAX_10_EXP)
779 while (-exponent >= NV_MAX_10_EXP) {
780 /* combination does not overflow, but 10^(-exponent) does */
790 exponent = -exponent;
792 for (bit = 1; exponent; bit <<= 1) {
793 if (exponent & bit) {
796 /* Floating point exceptions are supposed to be turned off,
797 * but if we're obviously done, don't risk another iteration.
799 if (exponent == 0) break;
803 return negative ? value / result : value * result;
807 Perl_my_atof(pTHX_ const char* s)
810 #ifdef USE_LOCALE_NUMERIC
812 if (PL_numeric_local && IN_LOCALE) {
815 /* Scan the number twice; once using locale and once without;
816 * choose the larger result (in absolute value). */
818 SET_NUMERIC_STANDARD();
821 if ((y < 0.0 && y < x) || (y > 0.0 && y > x))
833 Perl_my_atof2(pTHX_ const char* orig, NV* value)
835 NV result[3] = {0.0, 0.0, 0.0};
836 const char* s = orig;
838 UV accumulator[2] = {0,0}; /* before/after dp */
840 const char* send = s + strlen(orig) - 1;
842 I32 exp_adjust[2] = {0,0};
843 I32 exp_acc[2] = {-1, -1};
844 /* the current exponent adjust for the accumulators */
849 I32 sig_digits = 0; /* noof significant digits seen so far */
851 /* There is no point in processing more significant digits
852 * than the NV can hold. Note that NV_DIG is a lower-bound value,
853 * while we need an upper-bound value. We add 2 to account for this;
854 * since it will have been conservative on both the first and last digit.
855 * For example a 32-bit mantissa with an exponent of 4 would have
856 * exact values in the set
864 * where for the purposes of calculating NV_DIG we would have to discount
865 * both the first and last digit, since neither can hold all values from
866 * 0..9; but for calculating the value we must examine those two digits.
868 #define MAX_SIG_DIGITS (NV_DIG+2)
870 /* the max number we can accumulate in a UV, and still safely do 10*N+9 */
871 #define MAX_ACCUMULATE ( (UV) ((UV_MAX - 9)/10))
873 /* leading whitespace */
886 /* punt to strtod for NaN/Inf; if no support for it there, tough luck */
889 if (*s == 'n' || *s == 'N' || *s == 'i' || *s == 'I') {
890 const char *p = negative ? s - 1 : s;
893 rslt = strtod(p, &endp);
901 /* we accumulate digits into an integer; when this becomes too
902 * large, we add the total to NV and start again */
912 /* don't start counting until we see the first significant
913 * digit, eg the 5 in 0.00005... */
914 if (!sig_digits && digit == 0)
917 if (++sig_digits > MAX_SIG_DIGITS) {
918 /* limits of precision reached */
920 ++accumulator[seen_dp];
921 } else if (digit == 5) {
922 if (old_digit % 2) { /* round to even - Allen */
923 ++accumulator[seen_dp];
931 /* skip remaining digits */
932 while (isDIGIT(*s)) {
938 /* warn of loss of precision? */
941 if (accumulator[seen_dp] > MAX_ACCUMULATE) {
942 /* add accumulator to result and start again */
943 result[seen_dp] = S_mulexp10(result[seen_dp],
945 + (NV)accumulator[seen_dp];
946 accumulator[seen_dp] = 0;
947 exp_acc[seen_dp] = 0;
949 accumulator[seen_dp] = accumulator[seen_dp] * 10 + digit;
953 else if (!seen_dp && GROK_NUMERIC_RADIX(&s, send)) {
955 if (sig_digits > MAX_SIG_DIGITS) {
958 } while (isDIGIT(*s));
967 result[0] = S_mulexp10(result[0], exp_acc[0]) + (NV)accumulator[0];
969 result[1] = S_mulexp10(result[1], exp_acc[1]) + (NV)accumulator[1];
972 if (seen_digit && (*s == 'e' || *s == 'E')) {
973 bool expnegative = 0;
984 exponent = exponent * 10 + (*s++ - '0');
986 exponent = -exponent;
991 /* now apply the exponent */
994 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0])
995 + S_mulexp10(result[1],exponent-exp_adjust[1]);
997 result[2] = S_mulexp10(result[0],exponent+exp_adjust[0]);
1000 /* now apply the sign */
1002 result[2] = -result[2];
1003 #endif /* USE_PERL_ATOF */
1008 #if ! defined(HAS_MODFL) && defined(HAS_AINTL) && defined(HAS_COPYSIGNL)
1010 Perl_my_modfl(long double x, long double *ip)
1013 return (x == *ip ? copysignl(0.0L, x) : x - *ip);
1017 #if ! defined(HAS_FREXPL) && defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
1019 Perl_my_frexpl(long double x, int *e) {
1020 *e = x == 0.0L ? 0 : ilogbl(x) + 1;
1021 return (scalbnl(x, -*e));
1026 =for apidoc Perl_signbit
1028 Return a non-zero integer if the sign bit on an NV is set, and 0 if
1031 If Configure detects this system has a signbit() that will work with
1032 our NVs, then we just use it via the #define in perl.h. Otherwise,
1033 fall back on this implementation. As a first pass, this gets everything
1034 right except -0.0. Alas, catching -0.0 is the main use for this function,
1035 so this is not too helpful yet. Still, at least we have the scaffolding
1036 in place to support other systems, should that prove useful.
1039 Configure notes: This function is called 'Perl_signbit' instead of a
1040 plain 'signbit' because it is easy to imagine a system having a signbit()
1041 function or macro that doesn't happen to work with our particular choice
1042 of NVs. We shouldn't just re-#define signbit as Perl_signbit and expect
1043 the standard system headers to be happy. Also, this is a no-context
1044 function (no pTHX_) because Perl_signbit() is usually re-#defined in
1045 perl.h as a simple macro call to the system's signbit().
1046 Users should just always call Perl_signbit().
1050 #if !defined(HAS_SIGNBIT)
1052 Perl_signbit(NV x) {
1053 return (x < 0.0) ? 1 : 0;
1059 * c-indentation-style: bsd
1061 * indent-tabs-mode: t
1064 * ex: set ts=8 sts=4 sw=4 noet: