note 59648 deleted from function.abs by hholzgra
| From: | hholzgra@php.net | Date: | Sun, 15 Apr 2012 19:28:03 +0000 |
| Subject: | note 59648 deleted from function.abs by hholzgra | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-187145@lists.php.net to get a copy of this message | ||
Note Submitter: jeremys at hang dash wire dot com
----
I'm unable to replicate concordia's problem with the $n = $n - $n * 2 code. I agree with
the simplification to $n *= -1. But there's no reason that concordia's code should return
6 for the value -2, and it doesn't appear to. When I tried it, PHP returned 2, as it should.
If PHP were somehow flipping the sign of integers randomly, that would be a *major* bug!
There doesn't seem to be a sgn() function yet. Here's some quick code to do it:
function sgn($x) {
return $x ? ($x>0 ? 1 : -1) : 0;
}
You could use $x ? abs($x)/$x : 0 too, but might as well avoid the float division.