Modulus division returns an integer? - php

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

Related

Check if one number is a multiple of another in php [duplicate]

How can i do modulo arithmetic with decimal value since PHP only can do modulo with integer?
Example case:
echo 1.92 % 1000; // (int) 1, expected result should be 1.92
is there any library to do this correctly?
More info about this modulo arithmetic problem: https://bugs.php.net/bug.php?id=34399
There is also a floating point fmod() function
echo fmod(1.92, 1000)
http://php.net/manual/en/function.fmod.php
I think you wanted this one, this returns float result?

difference between php fmod function and arithmetic % operator

What is difference between PHP fmod function and arithmetic % operator ?
Here its give diff output
fmod(1438090136928 , 268435456)
and
1438090136928 % 268435456
When to use these functions?
Edit
Which one is more faster ?
As doc says:
Operands of modulus are converted to integers (by stripping the
decimal part) before processing.
But fmod accepts float values and return a float value also.
I do not know the differences in detail but the following information might help :-
The modulus can only handle numbers upto 4294967296 i.e 2^32
Both fmod and % are native
You should also consider using php math functions, as they can handle larger numbers and comparatively faster
Thanks, hope it helps.

Getting Remainder/% operator Throwing Error in php

I am new to php and was doing some mathematics and found this weird thing happening.
$numOfDiffChars = 62;
$id = 285355773910;
$remainder = $id % $numOfDiffChars;
echo "Remainder: ".$remainder." ID: ".$id." NumOfElems: ".$numOfDiffChars." ",($id - floor($id/$numOfDiffChars)*$numOfDiffChars);
The answer is as follows:
Remainder: 10 ID: 285355773910 NumOfElems: 62 26
which states that % operator gives the remainder 10 whereas mathematically its 26. What could be the reason for this? Is it just some error that I committed or is there a logic?
I am not quite sure, but if your using a 32-bit machine the problem could be that the integer $id is out of bound and therefore interpreted as a floating point.
http://php.net/manual/en/language.types.integer.php
have you tried fmod function find example on --->
http://www.php.net/manual/en/function.fmod.php
From php.net
Note that operator % (modulus) works just with integers (between -214748348 and 2147483647) while fmod() works with short and large numbers.

PHP Modulo Decimal

How can i do modulo arithmetic with decimal value since PHP only can do modulo with integer?
Example case:
echo 1.92 % 1000; // (int) 1, expected result should be 1.92
is there any library to do this correctly?
More info about this modulo arithmetic problem: https://bugs.php.net/bug.php?id=34399
There is also a floating point fmod() function
echo fmod(1.92, 1000)
http://php.net/manual/en/function.fmod.php
I think you wanted this one, this returns float result?

Get remainder only from a division using 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.

Categories