www.delorie.com/djgpp/doc/libc/libc_776.html   search  
libc.a reference

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

strtod

Syntax

 
#include <stdlib.h>

double strtod(const char *s, char **endp);

Description

This function converts as many characters of s as look like a floating point number into that number. It also recognises (case-insensitively) "Inf", "Infinity", "NaN", "NaN(optional decimal-number)", "NaN(optional octal-number)" and "NaN(optional hex-number)". If endp is not a null pointer, a pointer to the first unconverted character will be stored in the location pointed to by endp.

Return Value

The value represented by s.

If s is "Inf" or "Infinity", with any variations of case and optionally prefixed with "+" or "-", the return value is INFINITY (if no prefix or a "+" prefix) or -INFINITY (if the prefix is "-").

If s is "NaN" or "NaN()", with any variations of case and optionally prefixed with "+" or "-", the return value is (double)NAN. If the prefix is "-" the sign bit in the NaN will be set to 1.

If s is "NaN(decimal-number)", "NaN(octal-number)" or "NaN(hex-number)", with any variations of case and optionally prefixed with "+" or "-", the return value is a NaN with the mantissa bits set to the lower 52 bits of decimal-number, octal-number or hex-number (the mantissa for doubles consists of 52 bits). Use at most 16 hexadecimal digits in hex-number or the internal conversion will overflow, which results in a mantissa with all bits set. If the bit pattern given is 0 (which won't work as a representation of a NaN) (double)NAN will be returned. If the prefix is "-" the sign bit in the NaN will be set to 1. Testing shows that SNaNs might be converted into QNaNs (most significant bit will be set in the mantissa).

If a number represented by s doesn't fit into the range of values representable by the type double, the function returns either -HUGE_VAL (if s begins with the character -) or +HUGE_VAL, and sets errno to ERANGE.

Portability

ANSI/ISO C C89; C99 (see note 1)
POSIX 1003.2-1992; 1003.1-2001

Notes:

  1. Support for "Inf", "Infinity", "NaN" and "NaN(...)" was standardised in ANSI C99.

Example

 
char buf[] = "123ret";
char buf2[] = "0x123ret";
char buf3[] = "NAN(123)";
char buf4[] = "NAN(0x123)";
char *bp;
double x, x2, x3, x4;

x = strtod(buf, &bp);
x2 = strtod(buf2, &bp);
x3 = strtod(buf3, &bp);
x4 = strtod(buf4, &bp);


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2004   by DJ Delorie     Updated Apr 2004