|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15946 Math output errors
Submitted: 2002-03-07 22:55 UTC Modified: 2002-06-07 01:00 UTC
From: aubieweb at rogers dot com Assigned:
Status: No Feedback Package: Math related
PHP Version: 4.1.1 OS: Windows 2000
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
40 - 6 = ?
Subscribe to this entry?

 [2002-03-07 22:55 UTC] aubieweb at rogers dot com
I wrote a script which output Pascal's triangle and then showed the numbers which were divisible by a certain number in a different colour.  At first, this works fine.  Then after the 20th row or so, the % operator starts outputting incorrect numbers.  For example, 37 % 4 returns 1.  24 % 4 returns 3.  When those numbers are checked on their own, it works fine.  In the loops though, it starts giving incorrect information after 20 rows.  You can see this at



for ($r = 0; $r <= $rows; $r++)
	if ($hidenum) print "<table cellpadding='0' cellspacing='0' border='1' bordercolor='#000088' borderstyle='solid'><tr>\n";
	if (!$hidenum) print "<table cellpadding='2' cellspacing='1' border='0'><tr>\n";
	for ($i = 0; $i <= $r; $i++)
		$num = factorial($r)/(factorial($r - $i) * factorial($i));
		if ($divisor > 0 && $num % $divisor == 0)
	if (!$hidenum) print "<td bgcolor='$bgcolor'><span class='text'>$num</span></td>\n";
	$check = $num % $divisor;
	if ($hidenum && $divisor > 0 && $num % $divisor == 0) print "<td><img src='red.gif' width='$size' height='$size' alt='$num % $divisor = $check'></td>\n";
	if ($hidenum && $divisor > 0 && $num % $divisor != 0) print "<td><img src='grey.gif' width='$size' height='$size' alt='$num % $divisor = $check'></td>\n";
	if ($hidenum && $divisor == 0) print "<td><img src='grey.gif' width='$size' height='$size' alt='$num % $divisor = $check'></td>\n";
	print "</tr></table>";

function factorial($x)
	$total = 1;
	for ($i = $x; $i > 1; $i--)
		$total = $total * $i;
	return $total;



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-03-08 06:20 UTC]
Are you completely sure that it's an error in PHP and not in your algorithm?
 [2002-03-08 06:27 UTC]
 [2002-03-08 07:19 UTC] aubieweb at rogers dot com
Yes.  Here is the exact code that shows the calculuation.

$check = $num % $divisor;
	if ($hidenum && $divisor > 0 && $num % $divisor == 0) print "<td><img
src='red.gif' width='$size' height='$size' alt='$num % $divisor =

As yuo can see, the calculation is very simple (top line) and the alt of the image shows both numbers exactly and they haven't been changed.  In that piece of code I get the bad results but only AFTER the loop has been running for some time.
 [2002-03-08 16:52 UTC] aubieweb at rogers dot com
OK.  I added a bunch of round()'s around the variables and the problem seems to have been fixed up to 48 rows.  This just seems to be numbers start loosing accuracy after a while.  Even smaller numbers.
 [2002-06-07 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a month, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Thu Jan 20 10:03:34 2022 UTC