Opposite of Modulus?

Silver-0-surfer

Well-Known Member
Joined
Jan 5, 2008
Messages
317
Reaction score
7
Location
CPT
Hi, Is there a way to do something like the opposite of Modulus in PHP?

I want to go 7 (SPECIAL OPERATOR) 3 = 2

I have googled but can't find anything positive...
 
You mean, you want the integer not the remainder? Try using round or ceil or floor.
 
Last edited:
Erm just divide?

7/3 = 2

If it's an integer the remainder is ignored. Round will be different, it takes a number then gets the double value and rounds up/down based on the result (hence the name round).

Technically the opposite of modulus is the modular multiplicative inverse, which can be performed if and only if the numbers used for the mod operation have a greatest common denominator of 1. (IE. relatively prime)
 
$x = 11/3;
echo $x; // = 3.6666666666667



$x = (int)(11/3);
echo $x; // = 3

or

$x = 11/3;
echo (int)$x; // = 3
 
Last edited:
I think you mean weakly typed languages...

Python, Ruby etc. 11/3 = 3
 
I think you mean weakly typed languages...

Python, Ruby etc. 11/3 = 3

Code:
Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 11/3
3.6666666666666665
>>>

In Python, if you want to do integer division, where the fractional part of the answer is dropped (also known as floor division), you can use the // operator instead of /. The remainder is obtainable by using the modulo % operator.
>>> print(13 // 5)
2
>>> print(13 % 5)
3
 
Top
Sign up to the MyBroadband newsletter
X