Math problem with modulus

From: Date: Tue, 05 Oct 2004 10:03:58 +0000
Subject: Math problem with modulus
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-198651@lists.php.net to get a copy of this message
Hello. I have some problems with basic math in the function below. function Custom_Round($Digit, $Divisor) { return (($Digit / $Divisor) - (($Digit % $Divisor) / $Divisor)); } Custom_Round(23, 10) returns float(2) as expected. Same for Custom_Round(22, 10). BUT when you compare the both results like below Custom_Round(23, 10)!=Custom_Round(22, 10) I always get bool(true) whereas the result should be bool(false) When I have debuged this small piece of code I found out that intval(Custom_Round(23, 10)) = 1 whats definitly not expected. But intval(Custom_Round(22, 10)) = 2 as expected. Replacing Custom_Round() by Floor() works fine, but why does the custom function doing the same not work? Is this a bug in PHP? This problem occurse in PHP 4.3.3 and PHP 5.0.1. Thanks for help, Christoph

« previous php.general (#198651) next »