note 110766 added to language.types.float
| From: | deminy at deminy dot net | Date: | Wed, 05 Dec 2012 20:39:52 +0000 |
| Subject: | note 110766 added to language.types.float | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-192276@lists.php.net to get a copy of this message | ||
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 '==='.
----
Server IP: 69.147.83.199
Probable Submitter: 72.52.91.2 (proxied: 216.113.168.148)
----
Manual Page -- http://php.net/manual/en/language.types.float.php
Edit -- https://master.php.net/note/edit/110766
Del: integrated -- https://master.php.net/note/delete/110766/integrated
Del: useless -- https://master.php.net/note/delete/110766/useless
Del: bad code -- https://master.php.net/note/delete/110766/bad+code
Del: spam -- https://master.php.net/note/delete/110766/spam
Del: non-english -- https://master.php.net/note/delete/110766/non-english
Del: in docs -- https://master.php.net/note/delete/110766/in+docs
Del: other reasons-- https://master.php.net/note/delete/110766
Reject -- https://master.php.net/note/reject/110766
Search -- https://master.php.net/manage/user-notes.php