note 110766 deleted from language.types.float by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Feb 2014 02:23:57 +0000 |
| Subject: | note 110766 deleted from language.types.float by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198484@lists.php.net to get a copy of this message | ||
Note Submitter: deminy at deminy dot net
----
It seems that this hasn't yet been documented any well, but please be aware that
displayed/printed values of floating point numbers are dependent on PHP configuration
"precision". You might need to keep this in mind if you are doing tests with floating
point numbers and want you test scripts always work under different situations.
Following code tested with PHP 5.3.18 on a 64-bit machine:
<?php
for ($i = 15; $i < 19; $i++) {
ini_set('precision', $i);
echo sprintf('When PHP configuration "precision" is set to %2d, floating point
number 1.12 is displayed as:', $i), "\n\t", 1.12, "\n\t", (string)1.12,
"\n\t", round(1.12, 2), "\n";
echo "\tString ", (string)1.12, ' ((string)1.12) and ',
(string)'1.12', " ((string)'1.12') are ", (string)1.12 ==
(string)'1.12' ? 'equal' : 'different', " when compared with
'=='.\n";
// When PHP configuration 'precision' is set to different values, following expression
prints different results!
echo "\tString ", (string)1.12, ' ((string)1.12) and ',
(string)'1.12', " ((string)'1.12') are ", (string)1.12 ===
(string)'1.12' ? 'equal' : 'different', " when compared with
'==='.\n";
}
?>
Output:
When PHP configuration "precision" is set to 15, floating point number 1.12 is printed as:
1.12
1.12
1.12
String 1.12 ((string)1.12) and 1.12 ((string)'1.12') are equal when compared with
'=='.
String 1.12 ((string)1.12) and 1.12 ((string)'1.12') are equal when compared with
'==='.
When PHP configuration "precision" is set to 16, floating point number 1.12 is printed as:
1.12
1.12
1.12
String 1.12 ((string)1.12) and 1.12 ((string)'1.12') are equal when compared with
'=='.
String 1.12 ((string)1.12) and 1.12 ((string)'1.12') are equal when compared with
'==='.
When PHP configuration "precision" is set to 17, floating point number 1.12 is printed as:
1.1200000000000001
1.1200000000000001
1.1200000000000001
String 1.1200000000000001 ((string)1.12) and 1.12 ((string)'1.12') are equal when
compared with '=='.
String 1.1200000000000001 ((string)1.12) and 1.12 ((string)'1.12') are different when
compared with '==='.
When PHP configuration "precision" is set to 18, floating point number 1.12 is printed as:
1.12000000000000011
1.12000000000000011
1.12000000000000011
String 1.12000000000000011 ((string)1.12) and 1.12 ((string)'1.12') are equal when
compared with '=='.
String 1.12000000000000011 ((string)1.12) and 1.12 ((string)'1.12') are different when
compared with '==='.