php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: todd at magnifisites dot com
New email:
PHP Version: OS:

 

 [2005-07-06 01:10 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-06 01:18 UTC] sniper@php.net
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: Tue Mar 19 11:01:28 2024 UTC