Search code examples
pythonmathscipycomb

Why is there a difference between scipy.special.comb and math.comb?


Can someone explain why this is not equal please?

import scipy
import math
sum(math.comb(250, i) for i in range(0, 251)) == sum(scipy.special.comb(250, i) for i in range(0, 251))

But that's, for example, yes?

sum(math.comb(25, i) for i in range(0, 26)) == sum(scipy.special.comb(25, i) for i in range(0, 26))

Thank you :)


Solution

  • From the documentation you find, that you have to set the 'exact' flag to True like this:

    scipy.special.comb(250, i, exact=True)
    

    your code will then read

    import scipy.special as ssp
    import math
    print(sum(math.comb(250, i) for i in range(0, 251)) == sum(ssp.comb(250, i, exact=True) for i in range(0, 251)))
    

    and output 'True'.

    The documentation says

    exactbool, optional

    If exact is False, then floating point precision is used, otherwise exact long integer is computed.