#33581 [Opn->Bgs]: abs returns zero for a (string) number between 0 and -1
| From: | sniper@php.net | Date: | Tue, 05 Jul 2005 23:19:00 +0000 |
| Subject: | #33581 [Opn->Bgs]: abs returns zero for a (string) number between 0 and -1 | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-81435@lists.php.net to get a copy of this message | ||
ID: 33581
Updated by: sniper@php.net
Reported By: todd at magnifisites dot com
-Status: Open
+Status: Bogus
Bug Type: Math related
Operating System: Any
PHP Version: 5.0.3
New Comment:
Works fine with latest CVS. Please don't report bugs if you can't
reproduce them with the latest releases (including BETAs) and
preferrably also with latest CVS snapshot(s).
Previous Comments:
------------------------------------------------------------------------
[2005-07-06 01:10:00] todd at magnifisites dot com
Description:
------------
abs() is returning zero (0) for a string representation of numbers
between (but not including) 0 and -1.
I realize that abs() operates on numeric values as opposed to strings
but this value is coming from an ODBC result set and yes, I tried
casting it to a numeric value first and it still returned the
unexpected result of zero (0). Besides, the docs say it should work
this way
http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion
This is tested and confirmed for PHP version 4.3.9, 4.3.10, 5.0.3 but
seems fixed in 5.0.4. However, I could not find any documentation
noting so in the ChangeLogs nor in any bug reports.
Originally, the $row['AMOUNT'] variable was coming from an ODBC result
set which will return the value as type string according to a
var_dump(). We are able to simulate the same by casting the variable
as a number, then as a string to note the difference:
Reproduce code:
---------------
<pre>
<?php
// numeric value; this works:
$row['AMOUNT'] = -.94;
print abs($row['AMOUNT']) . "\n";
// string; these do not work:
$row['AMOUNT'] = '-.94';
print abs($row['AMOUNT']) . "\n";
print abs($row['AMOUNT'] + 0) . "\n";
$amount = $row['AMOUNT'] + 0;
print abs($amount) . "\n";
// string; this works:
$amount = (float) $row['AMOUNT'];
print abs($amount) . "\n";
?>
</pre>
Expected result:
----------------
0.94
0.94
0.94
0.94
0.94
Actual result:
--------------
0.94
0
0
0
0.94
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=33581&edit=1