summaryrefslogtreecommitdiff
path: root/devdocs/c/language%2Ffloating_constant.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/c/language%2Ffloating_constant.html
new repository
Diffstat (limited to 'devdocs/c/language%2Ffloating_constant.html')
-rw-r--r--devdocs/c/language%2Ffloating_constant.html87
1 files changed, 87 insertions, 0 deletions
diff --git a/devdocs/c/language%2Ffloating_constant.html b/devdocs/c/language%2Ffloating_constant.html
new file mode 100644
index 00000000..112ca10b
--- /dev/null
+++ b/devdocs/c/language%2Ffloating_constant.html
@@ -0,0 +1,87 @@
+ <h1 id="firstHeading" class="firstHeading">Floating constant</h1> <p>Allows values of floating type to be used directly in expressions.</p>
+<h3 id="Syntax"> Syntax</h3> <p>A floating constant is a <a href="value_category" title="c/language/value category">non-lvalue</a> expression having the form:</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">significand</span> <span class="t-spar">exponent</span> <span class="t-mark">(optional)</span> <span class="t-spar">suffix</span> <span class="t-mark">(optional)</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <p>Where the <span class="t-spar">significand</span> has the form</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td class="t-sdsc-nopad"> <span class="t-spar">whole-number</span> <span class="t-mark">(optional)</span> <code>.</code><span class="t-mark">(optional)</span> <span class="t-spar">fraction</span> <span class="t-mark">(optional)</span> </td> <td class="t-sdsc-nopad"> </td> <td class="t-sdsc-nopad"> </td>
+</tr>
+</table> <p>The <span class="t-spar">exponent</span> has the form</p>
+<table class="t-sdsc-begin"> <tr class="t-sdsc"> <td> <code>e</code> | <code>E</code> <span class="t-spar">exponent-sign</span> <span class="t-mark">(optional)</span> <span class="t-spar">digit-sequence</span> </td> <td> (1) </td> <td class="t-sdsc-nopad"> </td>
+</tr> <tr class="t-sdsc"> <td> <code>p</code> | <code>P</code> <span class="t-spar">exponent-sign</span> <span class="t-mark">(optional)</span> <span class="t-spar">digit-sequence</span> </td> <td> (2) </td> <td> <span class="t-mark-rev t-since-c99">(since C99)</span> </td>
+</tr>
+</table> <div class="t-li1">
+<span class="t-li">1)</span> The exponent syntax for a decimal floating-point constant</div> <div class="t-li1">
+<span class="t-li">2)</span> The exponent syntax for hexadecimal floating-point constant</div> <table class="t-rev-begin"> <tr class="t-rev t-since-c23">
+<td> <p>Optional single quotes (<code>'</code>) can be inserted between the digits as a separator, they are ignored when compiling.</p>
+</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
+</tr> </table> <h3 id="Explanation"> Explanation</h3> <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
+<td> <p>If the <span class="t-spar">significand</span> begins with the character sequence <code>0x</code> or <code>0X</code>, the floating constant is a <i>hexadecimal floating constant</i>. Otherwise, it is a <i>decimal floating constant</i>.</p>
+<p>For a <i>hexadecimal floating constant</i>, the <span class="t-spar">significand</span> is interpreted as a hexadecimal rational number, and the <span class="t-spar">digit-sequence</span> of the exponent is interpreted as the integer power of 2 to which the significand has to be scaled.</p>
+<div class="c source-c"><pre data-language="c">double d = 0x1.2p3; // hex fraction 1.2 (decimal 1.125) scaled by 2^3, that is 9.0</pre></div> </td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
+</tr> </table> <p>For a <i>decimal floating constant</i>, the <span class="t-spar">significand</span> is interpreted as a decimal rational number, and the <span class="t-spar">digit-sequence</span> of the exponent is interpreted as the integer power of 10 to which the significand has to be scaled.</p>
+<div class="c source-c"><pre data-language="c">double d = 1.2e3; // decimal fraction 1.2 scaled by 10^3, that is 1200.0</pre></div> <h4 id="Suffixes"> Suffixes</h4> <p>An unsuffixed floating constant has type <code>double</code>. If <span class="t-spar">suffix</span> is the letter <code>f</code> or <code>F</code>, the floating constant has type <code>float</code>. If <span class="t-spar">suffix</span> is the letter <code>l</code> or <code>L</code>, the floating constant has type <code>long double</code>.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-since-c23">
+<td> <p>If the implementation predefines macro <code>__STDC_IEC_60559_BFP__</code>, the following suffixes and corresponding floating constants are additionally supported:</p>
+<ul>
+<li> if <span class="t-spar">suffix</span> is <code>df</code> or <code>DF</code>, the floating constant has type <code>_Decimal32</code>; </li>
+<li> if <span class="t-spar">suffix</span> is <code>dd</code> or <code>DD</code>, the floating constant has type <code>_Decimal64</code>; </li>
+<li> if <span class="t-spar">suffix</span> is <code>dl</code> or <code>DL</code>, the floating constant has type <code>_Decimal128</code>. </li>
+</ul> <p>Suffixes for decimal floating-point types are not allowed in hexadecimal floating constants.</p>
+</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
+</tr> </table> <h4 id="Optional_parts"> Optional parts</h4> <p>If the exponent is present and fractional part is not used, the decimal separator may be omitted:</p>
+<div class="c source-c"><pre data-language="c">double x = 1e0; // floating-point 1.0 (period not used)</pre></div> <p>For decimal floating constants, the <span class="t-spar">exponent</span> part is optional. If it is omitted, the period is not optional, and either the <span class="t-spar">whole-number</span> or the <span class="t-spar">fraction</span> must be present.</p>
+<div class="c source-c"><pre data-language="c">double x = 1.; // floating-point 1.0 (fractional part optional)
+double y = .1; // floating-point 0.1 (whole-number part optional)</pre></div> <table class="t-rev-begin"> <tr class="t-rev t-since-c99">
+<td> <p>For hexadecimal floating constants, the exponent is not optional to avoid ambiguity resulting from an <code>f</code> suffix being mistaken as a hexadecimal digit.</p>
+</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
+</tr> </table> <h4 id="Representable_values"> Representable values</h4> <p>The result of evaluating a floating constant is either the nearest representable value or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner (in other words, <a href="../numeric/fenv/fe_round" title="c/numeric/fenv/FE round">default rounding direction</a> during translation is implementation-defined).</p>
+<p>All floating constants of the same source form convert to the same internal format with the same value. Floating constants of different source forms, e.g. <code>1.23</code> and <code>1.230</code>, need not to convert to the same internal format and value.</p>
+<table class="t-rev-begin"> <tr class="t-rev t-since-c99">
+<td> <p>Floating-point constants may convert to more range and precision than is indicated by their type, if indicated by <code><a href="../types/limits/flt_eval_method" title="c/types/limits/FLT EVAL METHOD">FLT_EVAL_METHOD</a></code>. For example, the constant <code>0.1f</code> may act as if it were <code>0.1L</code> in an expression.</p>
+<p>The result of evaluating a hexadecimal floating constant, if <code><a href="../types/limits" title="c/types/limits">FLT_RADIX</a></code> is 2, is the exact value represented by the floating constant, correctly rounded to the target type.</p>
+</td> <td><span class="t-mark-rev t-since-c99">(since C99)</span></td>
+</tr> </table> <table class="t-rev-begin"> <tr class="t-rev t-since-c23">
+<td> <p>Floating constants of decimal floating-point type that have the same numerical value \(\small x\)x but different quantum exponents, e.g. <code>1230.dd</code>, <code>1230.0dd</code>, and <code>1.23e3dd</code>, have distinguishable internal representations.</p>
+<p>The quantum exponent \(\small q\)q of a floating constant of a decimal floating-point type is determined in the manner that \(\small 10^q\)10<sup class="t-su">q</sup> represents 1 at the place of last digit of the <span class="t-spar">significand</span> when possible. If the quantum exponent \(\small q\)q and the coefficient \(\small c = x\cdot 10^{-q}\)c=x·10<sup class="t-su">-q</sup> determined above cannot represented exactly in the type of the floating constant, \(\small q\)q is increased as needed within the limit of the type, and \(\small c\)c is reduced correspondingly, with needed rounding. The rounding may result in zero or infinity. If (the possibly rounded) \(\small c\)c is still out of the permitted range after \(\small q\)q reaches the maximum value, the resulted floating constant has value positive infinity.</p>
+</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
+</tr> </table> <h3 id="Notes"> Notes</h3> <p>Default <a href="../numeric/fenv/fe_round" title="c/numeric/fenv/FE round">rounding direction</a> and <a href="../types/limits/flt_eval_method" title="c/types/limits/FLT EVAL METHOD">precision</a> are in effect when the floating constants are converted into internal representations, and <a href="../numeric/fenv/fe_exceptions" title="c/numeric/fenv/FE exceptions">floating-point exceptions</a> are not raised even if <a href="../preprocessor/impl" title="c/preprocessor/impl"><code> #pragma STDC FENV_ACCESS</code></a> is in effect (for execution-time conversion of character strings, <code><a href="../string/byte/strtof" title="c/string/byte/strtof">strtod</a></code> can be used). Note that this differs from <a href="constant_expression" title="c/language/constant expression">arithmetic constant expressions</a> of floating type.</p>
+<p>Letters in the floating constants are case-insensitive<span class="t-rev-inl t-since-c23"><span>, except that uppercase and lowercase cannot be both used in suffixes for decimal floating-point types</span><span><span class="t-mark-rev t-since-c23">(since C23)</span></span></span>: <code>0x1.ep+3</code> and <code>0X1.EP+3</code> represent the same floating-point value <code>15.0</code>.</p>
+<p>The decimal point specified by <code><a href="../locale/setlocale" title="c/locale/setlocale">setlocale</a></code> has no effect on the syntax of floating constants: the decimal point character is always the period.</p>
+<p>Unlike integers, not every floating value can be represented directly by decimal <span class="t-rev-inl t-since-c99"><span>or even hexadecimal</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span> constant syntax<span class="t-rev-inl t-since-c99"><span>: macros <a href="../numeric/math/nan" title="c/numeric/math/NAN"><code>NAN</code></a> and <a href="../numeric/math/infinity" title="c/numeric/math/INFINITY"><code>INFINITY</code></a> as well as functions such as <code><a href="../numeric/math/nan" title="c/numeric/math/nan">nan</a></code> offer ways to generate those special values</span><span><span class="t-mark-rev t-since-c99">(since C99)</span></span></span>. Note that <code>0x1.FFFFFEp128f</code>, which might appear to be an IEEE float NaN, in fact overflows to an infinity in that format.</p>
+<p>There are no negative floating constants; an expression such as <code>-1.2</code> is the <a href="operator_arithmetic" title="c/language/operator arithmetic">arithmetic operator</a> unary minus applied to the floating constant <code>1.2</code>. Note that the special value negative zero may be constructed with <code>-0.0</code>.</p>
+<h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;stdio.h&gt;
+
+int main(void)
+{
+ printf("15.0 = %a\n", 15.0);
+ printf("0x1.ep+3 = %f\n", 0x1.ep+3);
+
+ // Constants outside the range of type double.
+ printf("+2.0e+308 --&gt; %g\n", 2.0e+308);
+ printf("+1.0e-324 --&gt; %g\n", 1.0e-324);
+ printf("-1.0e-324 --&gt; %g\n", -1.0e-324);
+ printf("-2.0e+308 --&gt; %g\n", -2.0e+308);
+}</pre></div> <p>Output:</p>
+<div class="text source-text"><pre data-language="c">15.0 = 0x1.ep+3
+0x1.ep+3 = 15.000000
++2.0e+308 --&gt; inf
++1.0e-324 --&gt; 0
+-1.0e-324 --&gt; -0
+-2.0e+308 --&gt; -inf</pre></div> </div> <h3 id="References"> References</h3> <ul>
+<li> C23 standard (ISO/IEC 9899:2023): </li>
+<ul><li> 6.4.4.2 Floating constants (p: TBD) </li></ul>
+<li> C17 standard (ISO/IEC 9899:2018): </li>
+<ul><li> 6.4.4.2 Floating constants (p: 47-48) </li></ul>
+<li> C11 standard (ISO/IEC 9899:2011): </li>
+<ul><li> 6.4.4.2 Floating constants (p: 65-66) </li></ul>
+<li> C99 standard (ISO/IEC 9899:1999): </li>
+<ul><li> 6.4.4.2 Floating constants (p: 57-58) </li></ul>
+<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
+<ul><li> 3.1.3.1 Floating constants </li></ul>
+</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/language/floating_literal" title="cpp/language/floating literal">C++ documentation</a></span> for <span class=""><span>Floating-point literal</span></span> </td>
+</tr> </table> <div class="_attribution">
+ <p class="_attribution-p">
+ &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
+ <a href="https://en.cppreference.com/w/c/language/floating_constant" class="_attribution-link">https://en.cppreference.com/w/c/language/floating_constant</a>
+ </p>
+</div>