php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42418 Strange behaviour of modulus function
Submitted: 2007-08-24 19:45 UTC Modified: 2007-08-27 05:56 UTC
From: onyjgyjz at trashmail dot net Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.2.3 OS: WinXP
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: onyjgyjz at trashmail dot net
New email:
PHP Version: OS:

 

 [2007-08-24 19:45 UTC] onyjgyjz at trashmail dot net
Description:
------------
Somehow the modulus operator does not behave as expected on some special numbers.(maybe integer overflow or something like that?)

Php-version i used:
PHP 5.2.3 (cli) (built: May 31 2007 09:37:22)
Copyright (c) 1997-2007 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2007 Zend Technologies

Reproduce code:
---------------
$a=11213214321;
echo $a % 2;

Expected result:
----------------
Output of: 1

Actual result:
--------------
Output of: -1

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-08-26 16:49 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

11213214321 overflows an integer and is converted into a negative 
number. The modulus operation is then performed on a negative value, 
hence the -1 return value.
 [2007-08-27 05:56 UTC] onyjgyjz at trashmail dot net
Shouldn't it return an error, if an integer was overflowed?
(like this "Division through Zero" error)
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Mar 12 11:01:32 2025 UTC