note 114204 added to function.floor

From: Date: Fri, 24 Jan 2014 00:55:22 +0000
Subject: note 114204 added to function.floor
Groups: php.notes 
Request: Send a blank email to php-notes+get-198327@lists.php.net to get a copy of this message
Note that the use of floor may (intended or not) uncover false integers: <?php $arrRoots=array(); $arrRoots[]=pow(27,(1/3)); // pow(3,3)=27 $arrRoots[]=pow(64,(1/3)); // pow(4,3)=64 for($i=0;$i<count($arrRoots);$i++) { if($arrRoots[$i]!=floor($arrRoots[$i])) echo '<div style="background-color:#ffff99">'; echo 'Unfloored value: '.$arrRoots[$i].' (Format float? '.is_float($arrRoots[$i]).')<br />'; echo 'Floored value: '.floor($arrRoots[$i]).' (Format float? '.is_float(floor($arrRoots[$i])).')'; if($arrRoots[$i]!=floor($arrRoots[$i])) echo '</div>'; echo '<br /><br />'; } ?> The directly retrieved cubic root of 64 (by pow with a fraction exp) equals 4; an outcome any maths teacher will reckon correct. Yet bringing the same number - the cubic root of 64 retrieved in the very same manner - to the floor will result in a value of 3 only. This behaviour is in no way exceptional. It is pivotal to check whether you are dealing with genuine integers or floats in disguise. floor should be used carefully. ---- Server IP: 213.172.110.14 Probable Submitter: 46.5.0.177 ---- Manual Page -- http://php.net/manual/en/function.floor.php Edit -- https://master.php.net/note/edit/114204 Del: integrated -- https://master.php.net/note/delete/114204/integrated Del: useless -- https://master.php.net/note/delete/114204/useless Del: bad code -- https://master.php.net/note/delete/114204/bad+code Del: spam -- https://master.php.net/note/delete/114204/spam Del: non-english -- https://master.php.net/note/delete/114204/non-english Del: in docs -- https://master.php.net/note/delete/114204/in+docs Del: other reasons-- https://master.php.net/note/delete/114204 Reject -- https://master.php.net/note/reject/114204 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#198327) next »