<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Floating point numbers</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.integer.html">Integers</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.string.html">Strings</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.types.float" class="sect1"> <h2 class="title">Floating point numbers</h2> <p class="para"> Floating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes: </p> <div class="informalexample"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1.234</span><span style="color: #007700">; <br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">1.2e3</span><span style="color: #007700">; <br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">7E-10</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> <p class="para"> Formally: </p> <div class="informalexample"> <div class="example-contents"> <div class="cdata"><pre> LNUM [0-9]+ DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*) EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM}) </pre></div> </div> </div> <p class="para"> The size of a float is platform-dependent, although a maximum of ~1.8e308 with a precision of roughly 14 decimal digits is a common value (the 64 bit IEEE format). </p> <div class="warning"><strong class="warning">Warning</strong> <h1 class="title">Floating point precision</h1> <p class="para"> Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded. </p> <p class="para"> Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like <em>0.1</em> or <em>0.7</em>, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, <em>floor((0.1+0.7)*10)</em> will usually return <em>7</em> instead of the expected <em>8</em>, since the internal representation will be something like <em>7.9999999999999991118...</em>. </p> <p class="para"> So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the <a href="ref.bc.html" class="link">arbitrary precision math functions</a> and <a href="ref.gmp.html" class="link">gmp</a> functions are available. </p> <p class="para"> For a "simple" explanation, see the <a href="http://floating-point-gui.de/" class="link external">» floating point guide</a> that's also titled "Why don’t my numbers add up?" </p> </div> <div class="sect2" id="language.types.float.casting"> <h3 class="title">Converting to float</h3> <p class="para"> For information on converting <span class="type"><a href="language.types.string.html" class="type string">string</a></span>s to <span class="type"><a href="language.types.float.html" class="type float">float</a></span>, see <a href="language.types.string.html#language.types.string.conversion" class="link">String conversion to numbers</a>. For values of other types, the conversion is performed by converting the value to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> first and then to <span class="type"><a href="language.types.float.html" class="type float">float</a></span>. See <a href="language.types.integer.html#language.types.integer.casting" class="link">Converting to integer</a> for more information. As of PHP 5, a notice is thrown if an <span class="type"><a href="language.types.object.html" class="type object">object</a></span> is converted to <span class="type"><a href="language.types.float.html" class="type float">float</a></span>. </p> </div> <div class="sect2" id="language.types.float.comparison"> <h3 class="title">Comparing floats</h3> <p class="para"> As noted in the warning above, testing floating point values for equality is problematic, due to the way that they are represented internally. However, there are ways to make comparisons of floating point values that work around these limitations. </p> <p class="para"> To test floating point values for equality, an upper bound on the relative error due to rounding is used. This value is known as the machine epsilon, or unit roundoff, and is the smallest acceptable difference in calculations. </p> <div class="informalexample"> <p class="simpara"> <var class="varname"><var class="varname">$a</var></var> and <var class="varname"><var class="varname">$b</var></var> are equal to 5 digits of precision. </p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= </span><span style="color: #0000BB">1.23456789</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= </span><span style="color: #0000BB">1.23456780</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$epsilon </span><span style="color: #007700">= </span><span style="color: #0000BB">0.00001</span><span style="color: #007700">;<br /><br />if(</span><span style="color: #0000BB">abs</span><span style="color: #007700">(</span><span style="color: #0000BB">$a</span><span style="color: #007700">-</span><span style="color: #0000BB">$b</span><span style="color: #007700">) < </span><span style="color: #0000BB">$epsilon</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"true"</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <div class="sect2" id="language.types.float.nan"> <h3 class="title">NaN</h3> <p class="para"> Some numeric operations can result in a value represented by the constant <strong><code>NAN</code></strong>. This result represents an undefined or unrepresentable value in floating-point calculations. Any loose or strict comparisons of this value against any other value, including itself, will have a result of <strong><code>FALSE</code></strong>. </p> <p class="para"> Because <strong><code>NAN</code></strong> represents any number of different values, <strong><code>NAN</code></strong> should not be compared to other values, including itself, and instead should be checked for using <span class="function"><a href="function.is-nan.html" class="function">is_nan()</a></span>. </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.types.integer.html">Integers</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.types.string.html">Strings</a></div> <div class="up"><a href="language.types.html">Types</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>