|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33581 abs returns zero for a (string) number between 0 and -1
Submitted: 2005-07-06 01:10 UTC Modified: 2005-07-06 01:18 UTC
From: todd at magnifisites dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.0.3 OS: Any
Private report: No CVE-ID: None
 [2005-07-06 01:10 UTC] todd at magnifisites dot com
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

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:
// 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";

Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-06 01:18 UTC]
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).
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 26 07:01:30 2024 UTC