328 lines
13 KiB
HTML
328 lines
13 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>strtod</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="strtod"></a> <a name="tag_03_740"></a><!-- strtod -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_03_740_01"></a>NAME</h4>
|
|
|
|
<blockquote>strtod, strtof, strtold - convert a string to a double-precision number</blockquote>
|
|
|
|
<h4><a name="tag_03_740_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<p><code><tt>#include <<a href="../basedefs/stdlib.h.html">stdlib.h</a>><br>
|
|
<br>
|
|
double strtod(const char *restrict</tt> <i>nptr</i><tt>, char **restrict</tt> <i>endptr</i><tt>);<br>
|
|
float strtof(const char *restrict</tt> <i>nptr</i><tt>, char **restrict</tt> <i>endptr</i><tt>);<br>
|
|
long double strtold(const char *restrict</tt> <i>nptr</i><tt>, char **restrict</tt> <i>endptr</i><tt>);<br>
|
|
</tt></code></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<div class="box"><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]"
|
|
border="0"> The functionality described on this reference page is aligned with the ISO C standard. Any conflict between the
|
|
requirements described here and the ISO C standard is unintentional. This volume of IEEE Std 1003.1-2001 defers to
|
|
the ISO C standard. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></div>
|
|
|
|
<p>These functions shall convert the initial portion of the string pointed to by <i>nptr</i> to <b>double</b>, <b>float</b>, and
|
|
<b>long double</b> representation, respectively. First, they decompose the input string into three parts:</p>
|
|
|
|
<ol>
|
|
<li>
|
|
<p>An initial, possibly empty, sequence of white-space characters (as specified by <a href=
|
|
"../functions/isspace.html"><i>isspace</i>()</a>)</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>A subject sequence interpreted as a floating-point constant or representing infinity or NaN</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>A final string of one or more unrecognized characters, including the terminating null byte of the input string</p>
|
|
</li>
|
|
</ol>
|
|
|
|
<p>Then they shall attempt to convert the subject sequence to a floating-point number, and return the result.</p>
|
|
|
|
<p>The expected form of the subject sequence is an optional plus or minus sign, then one of the following:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>A non-empty sequence of decimal digits optionally containing a radix character, then an optional exponent part</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>A 0x or 0X, then a non-empty sequence of hexadecimal digits optionally containing a radix character, then an optional binary
|
|
exponent part</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>One of INF or INFINITY, ignoring case</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>One of NAN or NAN(<i>n-char-sequence<small><sub>opt</sub></small></i>), ignoring case in the NAN part, where:</p>
|
|
|
|
<pre>
|
|
<tt>n-char-sequence:
|
|
digit
|
|
nondigit
|
|
n-char-sequence digit
|
|
n-char-sequence nondigit
|
|
</tt>
|
|
</pre>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>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.</p>
|
|
|
|
<p>If the subject sequence has the expected form for a floating-point number, the sequence of characters starting with the first
|
|
digit or the decimal-point character (whichever occurs first) shall be interpreted as a floating constant of the C language, except
|
|
that the radix character shall be used in place of a period, and that if neither an exponent part nor a radix character appears in
|
|
a decimal floating-point number, or if a binary exponent part does not appear in a hexadecimal floating-point number, an exponent
|
|
part of the appropriate type with value zero is assumed to follow the last digit in the string. If the subject sequence begins with
|
|
a minus sign, the sequence shall be interpreted as negated. A character sequence INF or INFINITY shall be interpreted as an
|
|
infinity, if representable in the return type, else as if it were a floating constant that is too large for the range of the return
|
|
type. A character sequence NAN or NAN(<i>n-char-sequence<small><sub>opt</sub></small></i>) shall be interpreted as a quiet NaN, if
|
|
supported in the return type, else as if it were a subject sequence part that does not have the expected form; the meaning of the
|
|
<i>n</i>-char sequences is implementation-defined. A pointer to the final string is stored in the object pointed to by
|
|
<i>endptr</i>, provided that <i>endptr</i> is not a null pointer.</p>
|
|
|
|
<p>If the subject sequence has the hexadecimal form and FLT_RADIX is a power of 2, the value resulting from the conversion is
|
|
correctly rounded.</p>
|
|
|
|
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The
|
|
radix character is defined in the program's locale (category <i>LC_NUMERIC ).</i> In the POSIX locale, or in a locale where the
|
|
radix character is not defined, the radix character shall default to a period ( <tt>'.'</tt> ). <img src="../images/opt-end.gif"
|
|
alt="[Option End]" border="0"></p>
|
|
|
|
<p>In other than the C <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt=
|
|
"[Option Start]" border="0"> or POSIX <img src="../images/opt-end.gif" alt="[Option End]" border="0"> locales, other
|
|
implementation-defined subject sequences may be accepted.</p>
|
|
|
|
<p>If the subject sequence is empty or does not have the expected form, no conversion shall be performed; the value of <i>str</i>
|
|
is stored in the object pointed to by <i>endptr</i>, provided that <i>endptr</i> is not a null pointer.</p>
|
|
|
|
<p><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> The
|
|
<i>strtod</i>() function shall not change the setting of <i>errno</i> if successful.</p>
|
|
|
|
<p>Since 0 is returned on error and is also a valid return on success, an application wishing to check for error situations should
|
|
set <i>errno</i> to 0, then call <i>strtod</i>(), <i>strtof</i>(), or <i>strtold</i>(), then check <i>errno</i>. <img src=
|
|
"../images/opt-end.gif" alt="[Option End]" border="0"></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_04"></a>RETURN VALUE</h4>
|
|
|
|
<blockquote>
|
|
<p>Upon successful completion, these functions shall return the converted value. If no conversion could be performed, 0 shall be
|
|
returned, and <i>errno</i> may be set to [EINVAL].</p>
|
|
|
|
<p>If the correct value is outside the range of representable values, HUGE_VAL, HUGE_VALF, or HUGE_VALL shall be returned
|
|
(according to the sign of the value), and <i>errno</i> shall be set to [ERANGE].</p>
|
|
|
|
<p>If the correct value would cause an underflow, a value whose magnitude is no greater than the smallest normalized positive
|
|
number in the return type shall be returned and <i>errno</i> set to [ERANGE].</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_05"></a>ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>These functions shall fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[ERANGE]</dt>
|
|
|
|
<dd>The value to be returned would cause overflow <sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src=
|
|
"../images/opt-start.gif" alt="[Option Start]" border="0"> or underflow. <img src="../images/opt-end.gif" alt="[Option End]"
|
|
border="0"></dd>
|
|
</dl>
|
|
|
|
<p>These functions may fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EINVAL]</dt>
|
|
|
|
<dd><sup>[<a href="javascript:open_code('CX')">CX</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> No
|
|
conversion could be performed. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_03_740_06"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_07"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>If the subject sequence has the hexadecimal form and FLT_RADIX is not a power of 2, and the result is not exactly representable,
|
|
the result should be one of the two numbers in the appropriate internal format that are adjacent to the hexadecimal floating source
|
|
value, with the extra stipulation that the error should have a correct sign for the current rounding direction.</p>
|
|
|
|
<p>If the subject sequence has the decimal form and at most DECIMAL_DIG (defined in <a href=
|
|
"../basedefs/float.h.html"><i><float.h></i></a>) significant digits, the result should be correctly rounded. If the subject
|
|
sequence <i>D</i> has the decimal form and more than DECIMAL_DIG significant digits, consider the two bounding, adjacent decimal
|
|
strings <i>L</i> and <i>U</i>, both having DECIMAL_DIG significant digits, such that the values of <i>L</i>, <i>D</i>, and <i>U</i>
|
|
satisfy <i>L</i> <= <i>D</i> <= <i>U</i>. The result should be one of the (equal or adjacent) values that would be obtained
|
|
by correctly rounding <i>L</i> and <i>U</i> according to the current rounding direction, with the extra stipulation that the error
|
|
with respect to <i>D</i> should have a correct sign for the current rounding direction.</p>
|
|
|
|
<p>The changes to <i>strtod</i>() introduced by the ISO/IEC 9899:1999 standard can alter the behavior of well-formed
|
|
applications complying with the ISO/IEC 9899:1990 standard and thus earlier versions of the base documents. One such example
|
|
would be:</p>
|
|
|
|
<pre>
|
|
<tt>int
|
|
what_kind_of_number (char *s)
|
|
{
|
|
char *endp;
|
|
double d;
|
|
long l;
|
|
<br>
|
|
d = strtod(s, &endp);
|
|
if (s != endp && *endp == `\0')
|
|
printf("It's a float with value %g\n", d);
|
|
else
|
|
{
|
|
l = strtol(s, &endp, 0);
|
|
if (s != endp && *endp == `\0')
|
|
printf("It's an integer with value %ld\n", 1);
|
|
else
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>If the function is called with:</p>
|
|
|
|
<pre>
|
|
<tt>what_kind_of_number ("0x10")
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>an ISO/IEC 9899:1990 standard-compliant library will result in the function printing:</p>
|
|
|
|
<pre>
|
|
<tt>It's an integer with value 16
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>With the ISO/IEC 9899:1999 standard, the result is:</p>
|
|
|
|
<pre>
|
|
<tt>It's a float with value 16
|
|
</tt>
|
|
</pre>
|
|
|
|
<p>The change in behavior is due to the inclusion of floating-point numbers in hexadecimal notation without requiring that either a
|
|
decimal point or the binary exponent be present.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_08"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_09"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_10"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="isspace.html"><i>isspace</i>()</a> , <a href="localeconv.html"><i>localeconv</i>()</a> , <a href=
|
|
"scanf.html"><i>scanf</i>()</a> , <a href="setlocale.html"><i>setlocale</i>()</a> , <a href="strtol.html"><i>strtol</i>()</a> , the
|
|
Base Definitions volume of IEEE Std 1003.1-2001, <a href="../basedefs/xbd_chap07.html">Chapter 7, Locale</a>, <a href=
|
|
"../basedefs/float.h.html"><i><float.h></i></a>, <a href="../basedefs/stdlib.h.html"><i><stdlib.h></i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_11"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 1. Derived from Issue 1 of the SVID.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_12"></a>Issue 5</h4>
|
|
|
|
<blockquote>
|
|
<p>The DESCRIPTION is updated to indicate that <i>errno</i> is not changed if the function is successful.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_740_13"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>Extensions beyond the ISO C standard are marked.</p>
|
|
|
|
<p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>In the RETURN VALUE and ERRORS sections, the [EINVAL] optional error condition is added if no conversion could be performed.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>The following changes are made for alignment with the ISO/IEC 9899:1999 standard:</p>
|
|
|
|
<ul>
|
|
<li>
|
|
<p>The <i>strtod</i>() function is updated.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The <i>strtof</i>() and <i>strtold</i>() functions are added.</p>
|
|
</li>
|
|
|
|
<li>
|
|
<p>The DESCRIPTION is extensively revised.</p>
|
|
</li>
|
|
</ul>
|
|
|
|
<p>ISO/IEC 9899:1999 standard, Technical Corrigendum No. 1 is incorporated.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|