Get remainder only from a division using PHP - php

I am dividing 19/5 where by I have used 19/5 but I am unable to get the remainder only.
How do I get it.
Thanks
Jean

echo 19 % 5;
should return 4, which is the remainder of 19/5 (3 rem 4)
There is no need to use floor, because the result of a modulus operation will always be an integer value.
If you want the remainder when working with floating point values, then PHP also has the fmod() function:
echo fmod(19,5.5);
EDIT
If you want the remainder as a decimal:
either
echo 19/5 - floor(19/5);
or
echo (19 % 5) / 5
will both return 0.8

Please try it-
$tempMod = (float)($x / $y);
$tempMod = ($tempMod - (int)$tempMod)*$y;

Depending on what language you're using, % may not be the modulus operator. I'll assume you're using PHP, in which case it is %.
From what I can see, there is no need to use floor() with integer modulus, it will always return an integer. You can safely remove it.
To me, it looks like it isn't the math that's giving you hell, it's the code around it. You'll need to post more code; the code you have listed is fine.
Edit:
You're not looking for the remainder, you're looking for the left over decimal value. It has no name.
$leftover = 19 / 5;
$leftover = $leftover - floor($leftover);
This should be what you're looking for.

Related

Why does 2e3 return 2000? Isn't 2 to the 3rd power equal to 8?

This code:
$g = 2e3;
echo $g;
echo <br>;
echo "var_dump g gives:<br>";
Displays:
2000
float(2000)
I don't understand why it wouldn't display "8" and "float(8)"? Isn't 2 to the 3rd power equal to 8 ??
I tried looking for this question on this website already. Any help would be greatly appreciated. I did figure out that for 2e2 it displays 200. So it sounds like it just adds that many zeros to the end of the number instead of finding the 3rd power of 2. When I search for how to write exponential in php, the answers I've found said to use 'e' or 'E', but that doesn't seem to work or I've forgotten basic math. I'm sure somebody on here has a very simple answer for me.
Ok so why does $g = 2^3; give me 1? How can I write 2 to the 3rd power in php?
It's called scientific notation (or in this case "E notation").
2e3 is the same as 2 x 10^3, which is 2000.
If you want 2^3, you can use
$g = pow(2, 3);
Or in PHP 5.6+:
$g = 2**3;
Note: You need to use pow() (or **) because in PHP, when you do $g = 2^3;, you are doing 2 XOR 3.
When you read something like xey that is called scientific notation. It represents x times 10 to the power of y, or essentilly, add y zeroes to x (for positive values of y). So in this case, it's 2 * 10^3 = 2,000
2e3 is scientific notation for 2000.0. It is the same as 2.0e03. Use the pow() function:
echo pow(2, 3); // 8

Division by zero error while using modulus

Modulus operator is supposed to show the remainder. Like for echo(34%100) outputs 34. But why do i get a "Division by zero" error for this code echo(34%4294967296)
4294967296 is 2^32 and cannot be represented as 32 bit number - it wraps back to 0. If you use 64-bit version of PHP, it may work.
You might be able to use floating point modulus fmod to get what you want without overflowing.
https://bugs.php.net/bug.php?id=51731
2^31 is the largest integer you can get on Windows.
If you still want to mod large numbers, use bcmod.
I found this question searching for "division by zero error when using modulus", but the reason why was different.
Modulus (the % operator) will not work when the denomenator is is less than 1. using fmod() solves the problem.
Example:
$num = 5.1;
$den = .25;
echo ($num % $den);
// Outputs Warning: Division by zero
echo fmod($num, $den);
// Outputs 0.1
$num = 5.1;
$den = 1;
echo ($num % $den);
// Outputs 0, which is incorrect
echo fmod($num, $den);
// Outputs 0.1, which is correct
There's a lot of reports of mod getting wonky with large integers in php. Might be an overflow in the calculation or even in that number itself which is going to give you bugs. Best to use a large number library for that. Check out gmp or bcmath.

Modulus division returns an integer?

Does modulus division only return integers? I need a float return. See the following:
var_dump(12 % 10); // returns 2, as expected
var_dump(11.5 % 10); // returns 1 instead of 1.5?
Yes. the % operator returns an integer.
If you want a floating point result, use the fmod() function instead.
See the manual.
Operands of modulus are converted to integers (by stripping the
decimal part) before processing.
11.5 becomes 11.
11 % 10 = 1 remainder **1**
Your solution: fmod(), as tom_yes_tom suggests.
Quoting the documentation page:
"Operands of modulus are converted to integers (by stripping the
decimal part) before processing."
Is there any workaround for this?
mathematics...
11.5 - floor(11.5 / 10) * 10 == 1.5

How to divide numbers without remainder in PHP?

How does one divide numbers but exclude the remainder in PHP?
Just cast the resulting value to an int.
$n = (int) ($i / $m);
Interesting functions (depending on what you want to achieve and if you expect negative integers to get devided) are floor(), ceil() and round().
PHP 7 has a new built-in function for this named intdiv.
Example:
$result = intdiv(13, 2);
The value of $result in this example will be 6.
You can find the full documentation for this function in the PHP documentation.
For most practical purposes, the accepted answer is correct.
However, for builds where integers are 64 bits wide, not all possible integer values are representable as a double-precision float; See my comment on the accepted answer for details.
A variation of
$n = ($i - $i % $m) / $m;
(code taken from KingCrunch's comment under another answer) will avoid this problem depending on the desired rounding behavior (bear in mind that the result of the modulus operator may be negative).
In addition to decisions above like $result = intval($a / $b) there is one particular case:
If you need an integer division (without remainder) by some power of two ($b is 2 ^ N) you can use bitwise right shift operation:
$result = $a >> $N;
where $N is number from 1 to 32 on 32-bit operating system or 64 for 64-bit ones.
Sometimes it's useful because it's fastest decision for case of $b is some power of two.
And there's a backward (be careful due to overflow!) operation for multiplying by power of two:
$result = $a << $N;
Hope this helps for someone too.
or you could just do intval(13 / 2)
gives 6
use modulo in php:
$n = $i % $m;

How is -13 % 64 = -13 in PHP?

Derived from this question : (Java) How does java do modulus calculations with negative numbers?
Anywhere to force PHP to return positive 51?
update
Looking for a configuration setting to fix, instead hard-guessing
Or other math function like bcmath?
updated
Not entire convinced by that java answer, as it does not take account of negative modulus
-13+(-64) =?
Anyway, the post you referenced already gave the correct answer:
$r = $x % $n;
if ($r < 0)
{
$r += abs($n);
}
Where $x = -13 and $n = 64.
If GMP is available, you can use gmp_mod
Calculates n modulo d. The result is always non-negative, the sign of d is ignored.
Example:
echo gmp_strval(gmp_mod('-13', '64')); // 51
Note that n and d have to be GMP number resources or numeric strings. Anything else won't work¹
echo gmp_strval(gmp_mod(-13, 64));
echo gmp_mod(-13, 64);
will both return -51 instead (which is a bug).
¹ running the above in this codepad, will produce 51 in all three cases. It won't do that on my development machine.
The modulo operation should find the remainder of division of a number by another. But strictly speaking in most mainstream programming languages the modulo operation malfunctions if dividend or/and divisor are negative. This includes PHP, Perl, Python, Java, C, C++, etc.
Why I say malfunction? Because according to mathematic definition, a remainder must be zero or positive.
The simple solution is to handle the case yourself:
if r < 0 then r = r + |divisor|;
|divisor| is the absolute value of divisor.
Another solution is to use a library (as #Gordon pointed). However I wouldn't use a library to handle a simple case like this.
I hate using if in this case when you can calculate it right away.
$r = ($x % $n + $n) % $n;
when $n is positive.
The PHP manual says that
The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a. For example
so this is not configurable. Use the options suggested in the question you linked to

Categories