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?
Related
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.
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?
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
I observed the following and would be thankful for an explanation.
$amount = 4.56;
echo ($amount * 100) % 5;
outputs : 0
However,
$amount = 456;
echo $amount % 5;
outputs : 1
I tried this code on two separate PHP installations, with the same result. Thanks for your help!
I strongly suspect this is because 4.56 can't be exactly represented as a binary floating point number, so a value very close to it is used instead. When multiplied by 100, that comes to 455.999(something), and then the modulo operator truncates down to 455 before performing the operation.
I don't know the exact details of PHP floating point numbers, but the closest IEEE-754 double to 4.56 is 4.55999999999999960920149533194489777088165283203125.
So here's something to try:
$amount = 455.999999999;
echo $amount % 5;
I strongly suspect that will print 0 too. From some PHP arithmetic documentation:
Operands of modulus are converted to integers (by stripping the decimal part) before processing.
Use fmod to avoid this problem.
I have two numbers in PHP. 81.0000 and 81. While they are equal in reality I cannot get them to be equal in PHP.
I have tried casting both numbers to float and they still won't come thought as equal.
Anyone have any idea how I can have these two numbers be the same?
Check out the awesome WARNING on php.net:
never trust floating number results to the last digit, and never compare floating point numbers for equality.
The very best you can to is type cast to (int), or use PHP rounding functions like round(), floor(), or ceil().
UPDATE
Check out the Arbitrary Precision Math Functions such as the one #Jose Vega pointed out in his answer. They should get you where you need to go.
bccomp — Compare two arbitrary precision numbers
bccomp ( string $left_operand , string $right_operand [, int $scale ] )
<?php
echo bccomp('1', '2') . "\n"; // -1
echo bccomp('1.00001', '1', 3); // 0
echo bccomp('1.00001', '1', 5); // 1
?>