| <html lang="en"> |
| <head> |
| <title>Floating-Point Conversions - The GNU C Library</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="The GNU C Library"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Formatted-Output.html#Formatted-Output" title="Formatted Output"> |
| <link rel="prev" href="Integer-Conversions.html#Integer-Conversions" title="Integer Conversions"> |
| <link rel="next" href="Other-Output-Conversions.html#Other-Output-Conversions" title="Other Output Conversions"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| This file documents the GNU C library. |
| |
| This is Edition 0.12, last updated 2007-10-27, |
| of `The GNU C Library Reference Manual', for version |
| 2.8 (Sourcery G++ Lite 2011.03-41). |
| |
| Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2001, 2002, |
| 2003, 2007, 2008, 2010 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software Needs Free Documentation'' |
| and ``GNU Lesser General Public License'', the Front-Cover texts being |
| ``A GNU Manual'', and with the Back-Cover Texts as in (a) below. A |
| copy of the license is included in the section entitled "GNU Free |
| Documentation License". |
| |
| (a) The FSF's Back-Cover Text is: ``You have the freedom to |
| copy and modify this GNU manual. Buying copies from the FSF |
| supports it in developing GNU and promoting software freedom.''--> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| <link rel="stylesheet" type="text/css" href="../cs.css"> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Floating-Point-Conversions"></a> |
| <a name="Floating_002dPoint-Conversions"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Other-Output-Conversions.html#Other-Output-Conversions">Other Output Conversions</a>, |
| Previous: <a rel="previous" accesskey="p" href="Integer-Conversions.html#Integer-Conversions">Integer Conversions</a>, |
| Up: <a rel="up" accesskey="u" href="Formatted-Output.html#Formatted-Output">Formatted Output</a> |
| <hr> |
| </div> |
| |
| <h4 class="subsection">12.12.5 Floating-Point Conversions</h4> |
| |
| <p>This section discusses the conversion specifications for floating-point |
| numbers: the ‘<samp><span class="samp">%f</span></samp>’, ‘<samp><span class="samp">%e</span></samp>’, ‘<samp><span class="samp">%E</span></samp>’, ‘<samp><span class="samp">%g</span></samp>’, and ‘<samp><span class="samp">%G</span></samp>’ |
| conversions. |
| |
| <p>The ‘<samp><span class="samp">%f</span></samp>’ conversion prints its argument in fixed-point notation, |
| producing output of the form |
| [<code>-</code>]<var>ddd</var><code>.</code><var>ddd</var><!-- /@w -->, |
| where the number of digits following the decimal point is controlled |
| by the precision you specify. |
| |
| <p>The ‘<samp><span class="samp">%e</span></samp>’ conversion prints its argument in exponential notation, |
| producing output of the form |
| [<code>-</code>]<var>d</var><code>.</code><var>ddd</var><code>e</code>[<code>+</code>|<code>-</code>]<var>dd</var><!-- /@w -->. |
| Again, the number of digits following the decimal point is controlled by |
| the precision. The exponent always contains at least two digits. The |
| ‘<samp><span class="samp">%E</span></samp>’ conversion is similar but the exponent is marked with the letter |
| ‘<samp><span class="samp">E</span></samp>’ instead of ‘<samp><span class="samp">e</span></samp>’. |
| |
| <p>The ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions print the argument in the style |
| of ‘<samp><span class="samp">%e</span></samp>’ or ‘<samp><span class="samp">%E</span></samp>’ (respectively) if the exponent would be less |
| than -4 or greater than or equal to the precision; otherwise they use |
| the ‘<samp><span class="samp">%f</span></samp>’ style. A precision of <code>0</code>, is taken as 1. |
| Trailing zeros are removed from the fractional portion of the result and |
| a decimal-point character appears only if it is followed by a digit. |
| |
| <p>The ‘<samp><span class="samp">%a</span></samp>’ and ‘<samp><span class="samp">%A</span></samp>’ conversions are meant for representing |
| floating-point numbers exactly in textual form so that they can be |
| exchanged as texts between different programs and/or machines. The |
| numbers are represented is the form |
| [<code>-</code>]<code>0x</code><var>h</var><code>.</code><var>hhh</var><code>p</code>[<code>+</code>|<code>-</code>]<var>dd</var><!-- /@w -->. |
| At the left of the decimal-point character exactly one digit is print. |
| This character is only <code>0</code> if the number is denormalized. |
| Otherwise the value is unspecified; it is implementation dependent how many |
| bits are used. The number of hexadecimal digits on the right side of |
| the decimal-point character is equal to the precision. If the precision |
| is zero it is determined to be large enough to provide an exact |
| representation of the number (or it is large enough to distinguish two |
| adjacent values if the <code>FLT_RADIX</code> is not a power of 2, |
| see <a href="Floating-Point-Parameters.html#Floating-Point-Parameters">Floating Point Parameters</a>). For the ‘<samp><span class="samp">%a</span></samp>’ conversion |
| lower-case characters are used to represent the hexadecimal number and |
| the prefix and exponent sign are printed as <code>0x</code> and <code>p</code> |
| respectively. Otherwise upper-case characters are used and <code>0X</code> |
| and <code>P</code> are used for the representation of prefix and exponent |
| string. The exponent to the base of two is printed as a decimal number |
| using at least one digit but at most as many digits as necessary to |
| represent the value exactly. |
| |
| <p>If the value to be printed represents infinity or a NaN, the output is |
| [<code>-</code>]<code>inf</code><!-- /@w --> or <code>nan</code> respectively if the conversion |
| specifier is ‘<samp><span class="samp">%a</span></samp>’, ‘<samp><span class="samp">%e</span></samp>’, ‘<samp><span class="samp">%f</span></samp>’, or ‘<samp><span class="samp">%g</span></samp>’ and it is |
| [<code>-</code>]<code>INF</code><!-- /@w --> or <code>NAN</code> respectively if the conversion is |
| ‘<samp><span class="samp">%A</span></samp>’, ‘<samp><span class="samp">%E</span></samp>’, or ‘<samp><span class="samp">%G</span></samp>’. |
| |
| <p>The following flags can be used to modify the behavior: |
| |
| <!-- We use @asis instead of @samp so we can have ` ' as an item. --> |
| <dl> |
| <dt>‘<samp><span class="samp">-</span></samp>’<dd>Left-justify the result in the field. Normally the result is |
| right-justified. |
| |
| <br><dt>‘<samp><span class="samp">+</span></samp>’<dd>Always include a plus or minus sign in the result. |
| |
| <br><dt>‘<samp> </samp>’<dd>If the result doesn't start with a plus or minus sign, prefix it with a |
| space instead. Since the ‘<samp><span class="samp">+</span></samp>’ flag ensures that the result includes |
| a sign, this flag is ignored if you supply both of them. |
| |
| <br><dt>‘<samp><span class="samp">#</span></samp>’<dd>Specifies that the result should always include a decimal point, even |
| if no digits follow it. For the ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions, |
| this also forces trailing zeros after the decimal point to be left |
| in place where they would otherwise be removed. |
| |
| <br><dt>‘<samp><span class="samp">'</span></samp>’<dd>Separate the digits of the integer part of the result into groups as |
| specified by the locale specified for the <code>LC_NUMERIC</code> category; |
| see <a href="General-Numeric.html#General-Numeric">General Numeric</a>. This flag is a GNU extension. |
| |
| <br><dt>‘<samp><span class="samp">0</span></samp>’<dd>Pad the field with zeros instead of spaces; the zeros are placed |
| after any sign. This flag is ignored if the ‘<samp><span class="samp">-</span></samp>’ flag is also |
| specified. |
| </dl> |
| |
| <p>The precision specifies how many digits follow the decimal-point |
| character for the ‘<samp><span class="samp">%f</span></samp>’, ‘<samp><span class="samp">%e</span></samp>’, and ‘<samp><span class="samp">%E</span></samp>’ conversions. For |
| these conversions, the default precision is <code>6</code>. If the precision |
| is explicitly <code>0</code>, this suppresses the decimal point character |
| entirely. For the ‘<samp><span class="samp">%g</span></samp>’ and ‘<samp><span class="samp">%G</span></samp>’ conversions, the precision |
| specifies how many significant digits to print. Significant digits are |
| the first digit before the decimal point, and all the digits after it. |
| If the precision is <code>0</code> or not specified for ‘<samp><span class="samp">%g</span></samp>’ or ‘<samp><span class="samp">%G</span></samp>’, |
| it is treated like a value of <code>1</code>. If the value being printed |
| cannot be expressed accurately in the specified number of digits, the |
| value is rounded to the nearest number that fits. |
| |
| <p>Without a type modifier, the floating-point conversions use an argument |
| of type <code>double</code>. (By the default argument promotions, any |
| <code>float</code> arguments are automatically converted to <code>double</code>.) |
| The following type modifier is supported: |
| |
| <dl> |
| <dt>‘<samp><span class="samp">L</span></samp>’<dd>An uppercase ‘<samp><span class="samp">L</span></samp>’ specifies that the argument is a <code>long |
| double</code>. |
| </dl> |
| |
| <p>Here are some examples showing how numbers print using the various |
| floating-point conversions. All of the numbers were printed using |
| this template string: |
| |
| <pre class="smallexample"> "|%13.4a|%13.4f|%13.4e|%13.4g|\n" |
| </pre> |
| <p>Here is the output: |
| |
| <pre class="smallexample"> | 0x0.0000p+0| 0.0000| 0.0000e+00| 0| |
| | 0x1.0000p-1| 0.5000| 5.0000e-01| 0.5| |
| | 0x1.0000p+0| 1.0000| 1.0000e+00| 1| |
| | -0x1.0000p+0| -1.0000| -1.0000e+00| -1| |
| | 0x1.9000p+6| 100.0000| 1.0000e+02| 100| |
| | 0x1.f400p+9| 1000.0000| 1.0000e+03| 1000| |
| | 0x1.3880p+13| 10000.0000| 1.0000e+04| 1e+04| |
| | 0x1.81c8p+13| 12345.0000| 1.2345e+04| 1.234e+04| |
| | 0x1.86a0p+16| 100000.0000| 1.0000e+05| 1e+05| |
| | 0x1.e240p+16| 123456.0000| 1.2346e+05| 1.235e+05| |
| </pre> |
| <p>Notice how the ‘<samp><span class="samp">%g</span></samp>’ conversion drops trailing zeros. |
| |
| </body></html> |
| |