svn: /phpdoc/en/trunk/language/types/ float.xml
| From: | Gustavo André dos Santos Lopes | Date: | Sat, 20 Nov 2010 16:10:04 +0000 |
| Subject: | svn: /phpdoc/en/trunk/language/types/ float.xml | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-7443@lists.php.net to get a copy of this message | ||
cataphract Sat, 20 Nov 2010 16:10:04 +0000
Revision: http://svn.php.net/viewvc?view=revision&revision=305581
Log:
- Fixed inaccurate information on floats. Closes bug #53365.
Bug: http://bugs.php.net/53365 (Open) Inaccurate information
on floats.
Changed paths:
U phpdoc/en/trunk/language/types/float.xml
Modified: phpdoc/en/trunk/language/types/float.xml
===================================================================
--- phpdoc/en/trunk/language/types/float.xml 2010-11-20 02:09:04 UTC (rev 305580)
+++ phpdoc/en/trunk/language/types/float.xml 2010-11-20 16:10:04 UTC (rev 305581)
@@ -44,23 +44,28 @@
<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
+ 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 progragation must be considered when several operations are
+ compounded.
+ </para>
+
+ <para>
+ Additionally, rational numbers that are exactly representable as floating
+ point numbers in base 10, like <literal>0.1</literal> or
+ <literal>0.7</literal>, 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, <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>.
+ <literal>7.9999999999999991118...</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