strtod32(), strtod64(), strtod128() — Convert character string to decimal floating point

Standards

Standards / Extensions C or C++ Dependencies
C/C++ DFP both z/OS® V1.8

Format

#define __STDC_WANT_DEC_FP__
#include <stdlib.h>

_Decimal32 strtod32(const char * __restrict__ nptr, char ** __restrict__ endptr);
_Decimal64 strtod64(const char * __restrict__ nptr, char ** __restrict__ endptr);
_Decimal128 strtod128(const char * __restrict__ nptr, char ** __restrict__ endptr);

General description

The strtod32() strtod64(), and strtod128() functions convert the initial portion of the string pointed to by nptr to _Decimal32, _Decimal64, and _Decimal128 representation, respectively.

First, they decompose the input string into three parts:

  1. An initial, possibly empty, sequence of white-space characters (as specified by the isspace() function) .
  2. A subject sequence resembling a floating-point constant or representing an infinity or NaN.
  3. A final string of one or more unrecognized characters, including the terminating null character of the input string.

Then, they 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, then one of the following:

Note: If the input string is not one of these forms (for example "INFINITE"), the output results are undefined.

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 string is not of the expected form.

A character sequence NAN, NANQ, NAN(n-char-sequence), or NANQ(n-char-sequence) is interpreted as a quiet NAN. A character sequence of NANS, or NANS(n-char-sequence), is interpreted as a signalling NaN. A character sequence INF or INFINITY is interpreted as an infinity. A character sequence NAN, NAN(),or NAN(n-char-sequence) is interpreted as a quiet NAN. A character sequence of NANS, NANS(),or NANS(n-char-sequence), is interpreted as a signalling NaN.

A pointer to the final string is stored in the object pointed to by endptr, provided that endptr is not a null pointer.

The converted value keeps the same precision as the input if possible, and the value may be denormalized. Otherwise, rounding may occur. Rounding happens after any negation.

In other than the "C" locale, additional locale-specific subject sequence forms are accepted.

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

Argument Description
nptr Input pointer to start of the string to be converted
endptr NULL, or a pointer to a output pointer field that is filled in with the address of the first character in the input string that is not used in the conversion.
Note: To use IEEE decimal floating-point, the hardware must have the Decimal Floating-Point Facility installed.

Returned value

These functions return the converted value, if any. If no conversion could be performed, the value +0.E0DF, +0.E0DD,or +0.E0DL is returned. If the correct value is outside the range of representable values, plus or minus HUGE_VAL_D32, HUGE_VAL_D64, or HUGE_VAL_D128 is returned (according to the return type and sign of the value), and errno is set to ERANGE. If the result underflows, these functions return a value whose magnitude is no greater than the smallest normalized positive number in the return type. No signal is raised at the point of returning a signaling NaN.

errno Description
ERANGE The input string represents a value too large to fit in the output Decimal Floating Point type.

Example

See strtod() — Convert character string to double for an example.

Related information