📄 strtod.c
字号:
#endif /* _STRTOD_HEXADECIMAL_FLOATS */#endif /* _STRTOD_RESTRICT_DIGITS */ ++pos; }#ifdef __UCLIBC_HAS_LOCALE__#if defined(L___wcstofpmax) || defined(L___wcstofpmax) if (!pos0 && (*pos == decpt_wc)) { /* First decimal point? */ pos0 = ++pos; goto LOOP; }#else if (!pos0 && !memcmp(pos, decpt, decpt_len)) { /* First decimal point? */ pos0 = (pos += decpt_len); goto LOOP; }#endif#else /* __UCLIBC_HAS_LOCALE__ */ if ((*pos == '.') && !pos0) { /* First decimal point? */ pos0 = ++pos; /* Save position of decimal point */ goto LOOP; /* and process rest of digits. */ }#endif /* __UCLIBC_HAS_LOCALE__ */#ifdef _STRTOD_NEED_NUM_DIGITS if (num_digits<0) { /* Must have at least one digit. */#ifdef _STRTOD_HEXADECIMAL_FLOATS if (poshex) { /* Back up to '0' in '0x' prefix. */ pos = poshex; goto DONE; }#endif /* _STRTOD_HEXADECIMAL_FLOATS */#ifdef _STRTOD_NAN_INF_STRINGS if (!pos0) { /* No decimal point, so check for inf/nan. */ /* Note: nan is the first string so 'number = i/0.;' works. */ static const char nan_inf_str[] = "\05nan\0\012infinity\0\05inf\0"; int i = 0;#ifdef __UCLIBC_HAS_LOCALE__ /* Avoid tolower problems for INFINITY in the tr_TR locale. (yuk)*/#undef _tolower#define _tolower(C) ((C)|0x20)#endif /* __UCLIBC_HAS_LOCALE__ */ do { /* Unfortunately, we have no memcasecmp(). */ int j = 0; while (_tolower(pos[j]) == nan_inf_str[i+1+j]) { ++j; if (!nan_inf_str[i+1+j]) { number = i / 0.; if (negative) { /* Correct for sign. */ number = -number; } pos += nan_inf_str[i] - 2; goto DONE; } } i += nan_inf_str[i]; } while (nan_inf_str[i]); }#endif /* STRTOD_NAN_INF_STRINGS */#ifdef _STRTOD_ENDPTR pos = (Wchar *) str;#endif goto DONE; }#endif /* _STRTOD_NEED_NUM_DIGITS */#ifdef _STRTOD_RESTRICT_DIGITS if (num_digits > DECIMAL_DIG) { /* Adjust exponent for skipped digits. */ exponent_power += num_digits - DECIMAL_DIG; }#endif if (pos0) { exponent_power += pos0 - pos; /* Adjust exponent for decimal point. */ }#ifdef _STRTOD_HEXADECIMAL_FLOATS if (poshex) { exponent_power *= 4; /* Above is 2**4, but below is 2. */ p_base = 2; }#endif /* _STRTOD_HEXADECIMAL_FLOATS */ if (negative) { /* Correct for sign. */ number = -number; } /* process an exponent string */ if (((*pos)|0x20) == EXPCHAR) {#ifdef _STRTOD_ENDPTR pos1 = pos;#endif negative = 1; switch(*++pos) { /* Handle optional sign. */ case '-': negative = -1; /* Fall through to increment pos. */ case '+': ++pos; } pos0 = pos; exponent_temp = 0; while (isdigit(*pos)) { /* Process string of digits. */#ifdef _STRTOD_RESTRICT_EXP if (exponent_temp < MAX_ALLOWED_EXP) { /* Avoid overflow. */ exponent_temp = exponent_temp * 10 + (*pos - '0'); }#else exponent_temp = exponent_temp * 10 + (*pos - '0');#endif ++pos; }#ifdef _STRTOD_ENDPTR if (pos == pos0) { /* No digits? */ pos = pos1; /* Back up to {e|E}/{p|P}. */ } /* else */#endif exponent_power += negative * exponent_temp; }#ifdef _STRTOD_ZERO_CHECK if (number == 0.) { goto DONE; }#endif /* scale the result */#ifdef _STRTOD_LOG_SCALING exponent_temp = exponent_power; if (exponent_temp < 0) { exponent_temp = -exponent_temp; } while (exponent_temp) { if (exponent_temp & 1) { if (exponent_power < 0) { /* Warning... caluclating a factor for the exponent and * then dividing could easily be faster. But doing so * might cause problems when dealing with denormals. */ number /= p_base; } else { number *= p_base; } } exponent_temp >>= 1; p_base *= p_base; }#else /* _STRTOD_LOG_SCALING */ while (exponent_power) { if (exponent_power < 0) { number /= p_base; exponent_power++; } else { number *= p_base; exponent_power--; } }#endif /* _STRTOD_LOG_SCALING */#ifdef _STRTOD_ERRNO if (__FPMAX_ZERO_OR_INF_CHECK(number)) { __set_errno(ERANGE); }#endif DONE:#ifdef _STRTOD_ENDPTR if (endptr) { *endptr = pos; }#endif return number;}#endif /* defined(__UCLIBC_HAS_XLOCALE__) && !defined(__UCLIBC_DO_XLOCALE) */#endif/**********************************************************************/#ifdef L___fp_range_check#if defined(NEED_STRTOF_WRAPPER) || defined(NEED_STRTOD_WRAPPER)extern void __fp_range_check(__fpmax_t y, __fpmax_t x){ if (__FPMAX_ZERO_OR_INF_CHECK(y) /* y is 0 or +/- infinity */ && (y != 0) /* y is not 0 (could have x>0, y==0 if underflow) */ && !__FPMAX_ZERO_OR_INF_CHECK(x) /* x is not 0 or +/- infinity */ ) { __set_errno(ERANGE); /* Then x is not in y's range. */ }}#endif#endif/**********************************************************************/#if defined(L_strtof) || defined(L_strtof_l) || defined(L_wcstof) || defined(L_wcstof_l)#if defined(NEED_STRTOF_WRAPPER)#if defined(L_wcstof) || defined(L_wcstof_l)#define strtof wcstof#define strtof_l wcstof_l#define __strtof __wcstof#define __strtof_l __wcstof_l#define __strtofpmax __wcstofpmax#define __strtofpmax_l __wcstofpmax_l#define Wchar wchar_t#else#define Wchar char#endiffloat __XL(strtof)(const Wchar *str, Wchar **endptr __LOCALE_PARAM ){#if FPMAX_TYPE == 1 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );#else __fpmax_t x; float y; x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG ); y = (float) x; __fp_range_check(y, x); return y;#endif}__XL_ALIAS(strtof)#endif#endif/**********************************************************************/#if defined(L_strtod) || defined(L_strtod_l) || defined(L_wcstod) || defined(L_wcstod_l)#if defined(NEED_STRTOD_WRAPPER)#if defined(L_wcstod) || defined(L_wcstod_l)#define strtod wcstod#define strtod_l wcstod_l#define __strtod __wcstod#define __strtod_l __wcstod_l#define __strtofpmax __wcstofpmax#define __strtofpmax_l __wcstofpmax_l#define Wchar wchar_t#else#define Wchar char#endifdouble __XL(strtod)(const Wchar *__restrict str, Wchar **__restrict endptr __LOCALE_PARAM ){#if FPMAX_TYPE == 2 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );#else __fpmax_t x; double y; x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG ); y = (double) x; __fp_range_check(y, x); return y;#endif}__XL_ALIAS(strtod)#endif#endif/**********************************************************************/#if defined(L_strtold) || defined(L_strtold_l) || defined(L_wcstold) || defined(L_wcstold_l)#if defined(NEED_STRTOLD_WRAPPER)#if defined(L_wcstold) || defined(L_wcstold_l)#define strtold wcstold#define strtold_l wcstold_l#define __strtold __wcstold#define __strtold_l __wcstold_l#define __strtofpmax __wcstofpmax#define __strtofpmax_l __wcstofpmax_l#define Wchar wchar_t#else#define Wchar char#endiflong double __XL(strtold)(const Wchar *str, Wchar **endptr __LOCALE_PARAM ){#if FPMAX_TYPE == 3 return __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG );#else __fpmax_t x; long double y; x = __XL_NPP(__strtofpmax)(str, endptr, 0 __LOCALE_ARG ); y = (long double) x; __fp_range_check(y, x); return y;#endif}__XL_ALIAS(strtold)#endif#endif/**********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -