From aa3b2dd31a47926c1f9a7d4c11a1aee528959939 Mon Sep 17 00:00:00 2001 From: Hartmut Holzgraefe Date: Thu, 17 Aug 2000 19:24:36 +0000 Subject: [PATCH] some notes on float precision git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@30424 c90b9560-bf6c-de11-be94-00142212c4b1 --- functions/math.xml | 8 ++++---- language/types.xml | 30 ++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/functions/math.xml b/functions/math.xml index 3532eee2e3..355c2e9eee 100644 --- a/functions/math.xml +++ b/functions/math.xml @@ -307,8 +307,8 @@ $binary = base_convert ($hexadecimal, 16, 2); get the old behaviour. - See also floor and - round. + See also floor and round + and the float precision note. @@ -478,8 +478,8 @@ $binary = base_convert ($hexadecimal, 16, 2); get the old behaviour. - See also ceil and - round. + See also ceil and round + and the float precision note. diff --git a/language/types.xml b/language/types.xml index b29f6c09ae..c5ccb45c7a 100644 --- a/language/types.xml +++ b/language/types.xml @@ -62,6 +62,9 @@ $a = 0123; # octal number (equivalent to 83 decimal) $a = 0x12; # hexadecimal number (equivalent to 18 decimal) + The size of an integer is platform-dependant, although a + maximum value of about 2 billion is the usual value + (thats 32 bits signed). @@ -75,7 +78,34 @@ $a = 0x12; # hexadecimal number (equivalent to 18 decimal) $a = 1.234; $a = 1.2e3; + The size of a floating point number is platform-dependant, + although a maximum of ~1.8e308 with a precision of roughly 14 + decimal digits is a common value (thats 64 bit IEEE format). + + + It is quite usual that simple decimal fractions like 0.1 or 0.7 + cannot be converted into their internal binary counterparts + without a little lack of precision. This can lead to confusing + results as for example floor((0.1+0.7)*10) + will usualy return 7 instead of the expexted + 8 as the result of the inner expression + really something like 7.9999999999.... + + + This is similar to the impossibility of writing down the result + of 1/3 exactly in decimal form with a limit + numer of digits as it is 0.3333333.... + + + So never trust floating number results to the last digit and + never compare floating point numbers for equalness. If you + really need higher precision for decimal calculations or + in general you should use the + arbitrary precision math functions + instead. + +