From:             todd at magnifisites dot com
Operating system: Any
PHP version:      5.0.3
PHP Bug Type:     Math related
Bug description:  abs returns zero for a (string) number between 0 and -1

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 bug report at http://bugs.php.net/?id=33581&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=33581&r=trysnapshot4
Try a CVS snapshot (php5.0): 
http://bugs.php.net/fix.php?id=33581&r=trysnapshot50
Try a CVS snapshot (php5.1): 
http://bugs.php.net/fix.php?id=33581&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=33581&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=33581&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=33581&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=33581&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=33581&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=33581&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=33581&r=notwrong
Not enough info:             
http://bugs.php.net/fix.php?id=33581&r=notenoughinfo
Submitted twice:             
http://bugs.php.net/fix.php?id=33581&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=33581&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=33581&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=33581&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=33581&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=33581&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=33581&r=float
No Zend Extensions:          http://bugs.php.net/fix.php?id=33581&r=nozend
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=33581&r=mysqlcfg

Reply via email to