Search code examples
phpbcmath

Getting the modulo of two real numbers with BCMath


I would like to find out whether one real number is a multitude of the other one. I'm using BC-Math because the code is part of a larger system doing price calculations and using floats results in wrong results on multiple calculations due to the behaviour of floating point math.

BC-Math has bcmod() to get the modulus of an arbitrary precision number. However, before PHP 7.2 this function does not work with non-integer numeric strings. E.g:

echo bcmod('10', '9.2'); // 1, because '9.2' is truncated to '9'

Converting the numbers back to floats and using fmod() isn't an option since fmod function returning wrong result

I couldn't find any solution to this problem but I can't image that there is no solution.

EDIT:

Hello reader in the future who encounters the same problem! Based on @chiliNUT's response I created a pull-request for the bcmath-extended library.


Solution

  • I think it can be done using some math:

    You can relate mod(a,b) to a and b with this equation:

    a = b * floor(a/b) + mod(a,b)
    

    (explanation)

    and then solve that equation for mod to yield

    mod(a,b) = a - b * floor(a/b)
    

    Substituting your numbers you get

    mod(10,9.2) = 10 - 9.2 * floor(10/9.2) = 0.8
    

    Also see here for a bcmath floor implementation: How to ceil, floor and round bcmath numbers?