PHP Modulo Decimal - php

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

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.

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

Weird modulo result

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.

Strange number conversion error in PHP

How come the result for
intval("19.90"*100)
is
1989
and not 1990 as one would expect (PHP 5.2.14)?
That's because 19.90 is not exactly representable in base 2 and the closest approximation is slightly lower than 19.90.
Namely, this closest approximation is exactly 2^-48 × 0x13E66666666666. You can see its exact value in decimal form here, if you're interested.
This rounding error is propagated when you multiply by 100. intval will force a cast of the float to an integer, and such casts always rounds towards 0, which is why you see 1989. Use round instead.
You can also use bc* function for working with float :
$var = bcmul("19.90", "100");
echo intval($var);
intval converts doubles to integers by truncating the fractional component of the number. When dealing with some values, this can give odd results. Consider the following:
print intval ((0.1 + 0.7) * 10);
This will most likely print out 7, instead of the expected value of 8.
For more information, see the section on floating point numbers in the PHP manual
Why are you using intval on a floating point number? I agree with you that the output is a little off but it has to do with the relative inprecision of floating point numbers.
Why not just use floatval("19.90"*100) which outputs 1990
I believe the php doc at http://de2.php.net/manual/en/function.intval.php is omitting the fact that intval will not deliver "the integer value" but the integer (that is non-fractional) part of the number. It does not round.

Categories