1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Floating Constants (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Floating Constants (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Floating Constants (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Constants.html" rel="up" title="Constants">
|
---|
33 | <link href="Imaginary-Constants.html" rel="next" title="Imaginary Constants">
|
---|
34 | <link href="Integer-Const-Type.html" rel="prev" title="Integer Const Type">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Floating-Constants"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Imaginary-Constants.html" accesskey="n" rel="next">Imaginary Constants</a>, Previous: <a href="Integer-Const-Type.html" accesskey="p" rel="prev">Integer Const Type</a>, Up: <a href="Constants.html" accesskey="u" rel="up">Constants</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Floating_002dPoint-Constants"></span><h3 class="section">12.3 Floating-Point Constants</h3>
|
---|
64 | <span id="index-floating_002dpoint-constants"></span>
|
---|
65 | <span id="index-constants_002c-floating_002dpoint"></span>
|
---|
66 |
|
---|
67 | <p>A floating-point constant must have either a decimal point, an
|
---|
68 | exponent-of-ten, or both; they distinguish it from an integer
|
---|
69 | constant.
|
---|
70 | </p>
|
---|
71 | <p>To indicate an exponent, write ‘<samp>e</samp>’ or ‘<samp>E</samp>’. The exponent
|
---|
72 | value follows. It is always written as a decimal number; it can
|
---|
73 | optionally start with a sign. The exponent <var>n</var> means to multiply
|
---|
74 | the constant’s value by ten to the <var>n</var>th power.
|
---|
75 | </p>
|
---|
76 | <p>Thus, ‘<samp>1500.0</samp>’, ‘<samp>15e2</samp>’, ‘<samp>15e+2</samp>’, ‘<samp>15.0e2</samp>’,
|
---|
77 | ‘<samp>1.5e+3</samp>’, ‘<samp>.15e4</samp>’, and ‘<samp>15000e-1</samp>’ are six ways of
|
---|
78 | writing a floating-point number whose value is 1500. They are all
|
---|
79 | equivalent.
|
---|
80 | </p>
|
---|
81 | <p>Here are more examples with decimal points:
|
---|
82 | </p>
|
---|
83 | <div class="example">
|
---|
84 | <pre class="example">1.0
|
---|
85 | 1000.
|
---|
86 | 3.14159
|
---|
87 | .05
|
---|
88 | .0005
|
---|
89 | </pre></div>
|
---|
90 |
|
---|
91 | <p>For each of them, here are some equivalent constants written with
|
---|
92 | exponents:
|
---|
93 | </p>
|
---|
94 | <div class="example">
|
---|
95 | <pre class="example">1e0, 1.0000e0
|
---|
96 | 100e1, 100e+1, 100E+1, 1e3, 10000e-1
|
---|
97 | 3.14159e0
|
---|
98 | 5e-2, .0005e+2, 5E-2, .0005E2
|
---|
99 | .05e-2
|
---|
100 | </pre></div>
|
---|
101 |
|
---|
102 | <p>A floating-point constant normally has type <code>double</code>. You can
|
---|
103 | force it to type <code>float</code> by adding ‘<samp>f</samp>’ or ‘<samp>F</samp>’
|
---|
104 | at the end. For example,
|
---|
105 | </p>
|
---|
106 | <div class="example">
|
---|
107 | <pre class="example">3.14159f
|
---|
108 | 3.14159e0f
|
---|
109 | 1000.f
|
---|
110 | 100E1F
|
---|
111 | .0005f
|
---|
112 | .05e-2f
|
---|
113 | </pre></div>
|
---|
114 |
|
---|
115 | <p>Likewise, ‘<samp>l</samp>’ or ‘<samp>L</samp>’ at the end forces the constant
|
---|
116 | to type <code>long double</code>.
|
---|
117 | </p>
|
---|
118 | <p>You can use exponents in hexadecimal floating constants, but since
|
---|
119 | ‘<samp>e</samp>’ would be interpreted as a hexadecimal digit, the character
|
---|
120 | ‘<samp>p</samp>’ or ‘<samp>P</samp>’ (for “power”) indicates an exponent.
|
---|
121 | </p>
|
---|
122 | <p>The exponent in a hexadecimal floating constant is a possibly-signed
|
---|
123 | decimal integer that specifies a power of 2 (<em>not</em> 10 or 16) to
|
---|
124 | multiply into the number.
|
---|
125 | </p>
|
---|
126 | <p>Here are some examples:
|
---|
127 | </p>
|
---|
128 | <div class="example">
|
---|
129 | <pre class="example">0xAp2 // <span class="roman">40 in decimal</span>
|
---|
130 | 0xAp-1 // <span class="roman">5 in decimal</span>
|
---|
131 | 0x2.0Bp4 // <span class="roman">16.75 decimal</span>
|
---|
132 | 0xE.2p3 // <span class="roman">121 decimal</span>
|
---|
133 | 0x123.ABCp0 // <span class="roman">291.6708984375 in decimal</span>
|
---|
134 | 0x123.ABCp4 // <span class="roman">4666.734375 in decimal</span>
|
---|
135 | 0x100p-8 // <span class="roman">1</span>
|
---|
136 | 0x10p-4 // <span class="roman">1</span>
|
---|
137 | 0x1p+4 // <span class="roman">16</span>
|
---|
138 | 0x1p+8 // <span class="roman">256</span>
|
---|
139 | </pre></div>
|
---|
140 |
|
---|
141 | <p>See <a href="Floating_002dPoint-Data-Types.html">Floating-Point Data Types</a>.
|
---|
142 | </p>
|
---|
143 | <hr>
|
---|
144 | <div class="header">
|
---|
145 | <p>
|
---|
146 | Next: <a href="Imaginary-Constants.html" accesskey="n" rel="next">Imaginary Constants</a>, Previous: <a href="Integer-Const-Type.html" accesskey="p" rel="prev">Integer Const Type</a>, Up: <a href="Constants.html" accesskey="u" rel="up">Constants</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
147 | </div>
|
---|
148 |
|
---|
149 |
|
---|
150 |
|
---|
151 | </body>
|
---|
152 | </html>
|
---|