strtod32, strtod64, or strtod128 Subroutine

Purpose

Converts a string to a decimal floating-point number.

Library

Standard C Library (libc.a)

Syntax

#include <stdlib.h>

_Decimal32 strtod32 (nptr, endptr)
const char *nptr;
char **endptr;

_Decimal64 strtod64 (nptr, endptr)
const char *nptr;
char **endptr;

_Decimal128 strtod128 (nptr, endptr)
const char *nptr;
char **endptr;

Description

The strtod32, strtod64, and strtod128 subroutines convert the initial portion of the string pointed to by the nptr parameter to _Decimal32, _Decimal64, and _Decimal128 representation, respectively. First, these subroutines decompose the input string into three parts:
  • An initial and possibly empty sequence of white-space characters (as specified by the isspace subroutine)
  • A subject sequence that is interpreted as a floating-point constant or represents infinity or NaN
  • A final string of one or more unrecognized characters, including the terminating null byte of the input string
Then, the strtod32, strtod64, and strtod128 subroutines attempt to convert the subject sequence to a floating-point number and return the result.
The expected form of the subject sequence is an optional plus or minus sign and one of the following:
  • A non-empty sequence of decimal digits that might contain a radix character and an exponent part
  • INF, INFINITY, or any other string equivalent except for case
  • NAN or NAN (n-char-sequence opt), ignoring case in the NAN, where:
     n-char-sequence:
                   digit
                   n-char-sequence digit

The subject sequence is defined as the longest initial subsequence of the input string, starting with the first non-white-space character that is of the expected form. The subject sequence contains no characters if the input wide string is not of the expected form.

If the subject sequence has the expected form for a floating-point number, the sequence of characters starting with the first digit or the radix character (whichever occurs first) are interpreted as a floating constant according to the rules of the C language, except that the sequence is not a hexadecimal floating number or the radix character is used in place of a period. If neither an exponent part nor a radix character appears in a decimal floating-point number, an exponent part of the appropriate type with a value of 0 is assumed to follow the last digit in the string.

If the subject sequence begins with a minus sign, the sequence is interpreted as negated. A character sequence INF or INFINITY is interpreted as infinity. A character sequence NAN or NAN (n-char-sequence opt) is interpreted as a quiet NaN. The meaning of the n-char sequences is implementation-defined. A pointer to the final string is stored in the object pointed to by the endptr parameter, provided that the endptr parameter is not a null pointer.

The radix character is defined in the locale of the program (category LC_NUMERIC). In the POSIX locale, or in a locale where the radix character is not defined, the radix character defaults to a period.

In locales other than the C or POSIX locale, other implementation-defined subject sequences can be accepted.

If the subject sequence is empty or does not have the expected form, no conversion is performed. The value of the nptr parameter is stored in the object pointed to by the endptr parameter , provided that the endptr parameter is not a null pointer.

The strtod32, strtod64, and strtod128 subroutines do not change the setting of the errno global variable if successful.

The value of 0 is returned on error and it is also a valid return value on success. Therefore, an application checking for error situations must set the value of the errno global variable to 0, call the strtod32, strtod64, or strto128 subroutine, and check the errno global variable.

Parameters

Item Description
nptr Contains a pointer to the string to be converted to a decimal floating point value.
endpr Contains a pointer to the position in the string specified by the nptr parameter where a character is found that is not a valid character for the conversion.

Return Values

Upon successful completion, the strtod32, strtod64, and strtod128 subroutines return the converted value. If no conversion can be performed, the value of 0 is returned and the errno global variable might be set to EINVAL.

If the correct value is outside the range of representable values, ±HUGE_VAL_D32, ±HUGE_VAL_D64, or ±HUGE_VAL_D128 is returned (according to the return type and sign of the value), and the errno global variable is set to ERANGE.

If the correct value causes underflow, a value whose magnitude is no greater than the smallest normalized positive number in the return type is returned, and the errno global variable is set to ERANGE.