1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision: 288721 $ -->
<sect1 xml:id="language.types.float">
<title>Floating point numbers</title>
<para>
Floating point numbers (also known as "floats", "doubles", or "real numbers")
can be specified using any of the following syntaxes:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
]]>
</programlisting>
</informalexample>
<para>
Formally:
</para>
<informalexample>
<programlisting role="php">
<![CDATA[
LNUM [0-9]+
DNUM ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)
EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})
]]>
</programlisting>
</informalexample>
<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).
</para>
<warning xml:id="warn.float-precision">
<title>Floating point precision</title>
<para>
It is typical that simple decimal fractions like <literal>0.1</literal> or
<literal>0.7</literal> cannot be converted into their internal binary
counterparts without a small loss of precision. This can lead to confusing
results: for example, <literal>floor((0.1+0.7)*10)</literal> will usually
return <literal>7</literal> instead of the expected <literal>8</literal>,
since the internal representation will be something like
<literal role="infdec">7.9</literal>.
</para>
<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,
<literal>1/3</literal> in decimal form becomes
<literal role="infdec">0.3</literal>.
</para>
<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 <link linkend="ref.bc">arbitrary precision math functions</link> and
<link linkend="ref.gmp">gmp</link> functions are available.
</para>
</warning>
<sect2 xml:id="language.types.float.casting">
<title>Converting to float</title>
<para>
For information on converting <type>string</type>s to <type>float</type>, see
<link linkend="language.types.string.conversion">String conversion to
numbers</link>. For values of other types, the conversion is performed by
converting the value to <type>integer</type> first and then to
<type>float</type>. See
<link linkend="language.types.integer.casting">Converting to integer</link>
for more information. As of PHP 5, a notice is thrown if an
<type>object</type> is converted to <type>float</type>.
</para>
</sect2>
</sect1>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
|