Bug #16551 Updated: ceil results -0

From: Date: Wed, 24 Apr 2002 13:26:24 +0000
Subject: Bug #16551 Updated: ceil results -0
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-5818@lists.php.net to get a copy of this message
 ID:               16551
 Updated by:       alberty@neptunelabs.de
 Reported By:      alberty@neptunelabs.de
 Status:           Open
 Bug Type:         Variables related
 Operating System: i686-pc-linux-gnu
 PHP Version:      4.0CVS-2002-04-11
 New Comment:

Hi derick,

here is the proof of my statement.

try this code:

<?php
echo "<tt>(machine code representation)<br><br>";

// correct result
$b1=0;
$p=pack('f',$b1);
echo " $b1 = ";
for ($l=0,$o1='';$l<strlen($p);$l++){
	$o1.=sprintf('%02X',ord($p[$l]));
}
echo $o1.'h';
echo '<hr size=1>';

//wrong result
$a=-0.1;
$b2=round($a);
$p=pack('f',$b2);
echo "$b2 = ";
for ($l=0,$o2='';$l<strlen($p);$l++){
	$o2.=sprintf('%02X',ord($p[$l]));
}
echo $o2.'h';

echo "<hr>the question is, is $b1 == $b2<br>";
echo "php mean:<br><font color=#000088>";
if ($b1==$b2) echo "yes, this is true.<br>";
else echo "no, this is false.<br>";
echo "</font>but $o1 != $o2<br>";
echo "and also $o2 != 0";

echo "</tt>";
?>


The big problem with this behavior is, if you using round() or ceil()
to output results
of mathematical function, you can't be sure if the result what you
expected or not.

This can have fatal consequences for programs they output e.g.
statitics in text files and
other programs interpret the php results wrong!

This is not a trivial problem!


Previous Comments:
------------------------------------------------------------------------

[2002-04-24 08:34:50] alberty@neptunelabs.de

Bogus?

It is true that ceil() and round() return floating point numbers, but
-0 is not a number.

------------------------------------------------------------------------

[2002-04-24 08:27:16] derick@php.net

This is expected behavior. ceil() and floor() return a float because of
the large range of supported numbers.

Derick

------------------------------------------------------------------------

[2002-04-24 08:24:51] alberty@neptunelabs.de

The problem is also affected on round().

<?php
$a=-0.1
echo round($a);
// results: -0
?>

Regards,

Steve

------------------------------------------------------------------------

[2002-04-11 09:38:05] alberty@neptunelabs.de

Hi,

the follow code results -0,

<?php
$a=ceil((1/16)-1);
echo $a;
?>

maybe, this is a convertion problem with float types.

also, why the ceil() command results a floating point number and not an
integer?
It makes sense when the ceil command accept a precision like
round()...

Regards,

Steve

------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=16551&edit=1



Thread (6 messages)

« previous php.bugs (#5818) next »