Source of: /manual/en/language.types.float.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.types.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.types.float.php',
1 => 'Floating point numbers',
),
'up' =>
array (
0 => 'language.types.php',
1 => 'Types',
),
'prev' =>
array (
0 => 'language.types.integer.php',
1 => 'Integers',
),
'next' =>
array (
0 => 'language.types.string.php',
1 => 'Strings',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<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 programlisting">
<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 programlisting">
<div class="phpcode"><code><span style="color: #000000">
LNUM [0-9]+<br />DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)<br />EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})</span>
</code></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 id="warn.float-precision" class="warning"><b class="warning">Warning</b>
<h1 class="title">Floating point precision</h1>
<p class="para">
It is typical that simple decimal fractions like <i>0.1</i> or
<i>0.7</i> cannot be converted into their internal binary
counterparts without a small loss of precision. This can lead to confusing
results: for example, <i>floor((0.1+0.7)*10)</i> will usually
return <i>7</i> instead of the expected <i>8</i>,
since the internal representation will be something like
<i>7.<span style="text-decoration: overline;">9</span></i>.
</p>
<p class="para">
This is due to the fact that it is impossible to express some
fractions in decimal notation with a finite number of digits. For instance,
<i>1/3</i> in decimal form becomes
<i>0.<span style="text-decoration: overline;">3</span></i>.
</p>
<p class="para">
So never trust floating number results to the last digit, and never compare
floating point numbers for equality. If higher precision is necessary,
the <a href="ref.bc.php" class="link">arbitrary precision math functions</a> and
<a href="ref.gmp.php" class="link">gmp</a> functions are available.
</p>
</div>
<div id="language.types.float.casting" class="sect2">
<h3 class="title">Converting to float</h3>
<p class="para">
For information on converting <a href="language.types.string.php" class="type string">string</a>s to <a href="language.types.float.php" class="type float">float</a>, see
<a href="language.types.string.php#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 <a href="language.types.integer.php" class="type integer">integer</a> first and then to
<a href="language.types.float.php" class="type float">float</a>. See
<a href="language.types.integer.php#language.types.integer.casting" class="link">Converting to integer</a>
for more information. As of PHP 5, a notice is thrown if an
<a href="language.types.object.php" class="type object">object</a> is converted to <a href="language.types.float.php" class="type float">float</a>.
</p>
</div>
</div><?php manual_footer(); ?>