ZeroDivsionError with factorial in python

huangapple go评论68阅读模式
英文:

ZeroDivsionError with factorial in python

问题

I get a zero when I'm running the code

factorial(n-x)*factorial(x)

It's my first time asking on stackoverflow.

below are my codes

def factorial(x):
    res = 1

    if x==0:
        return 1
    
    while x > 0:
        res *= x
        x -= 1
    return res

def combination(n,x):
    return factorial(n)/(factorial(n-x)*factorial(x))

def binomial(n,x,p):
    return combination(n,x)*(p**x)*((1-p)**(n-x))

pmf_values = []

n = 200
x_list = np.arange(0, 200+1)

for x in x_list:
    #print(x)
    #print((factorial(n-x)*factorial(x)))
    pmf_values.append(binomial(n,x,p))

sample = np.random.choice(
    x_list, size=10000, p=pmf_values)
plt.hist(sample)

I found that the error occurs when x is 0.
when I run it in a separate cell,
it returns the correct answer 1.0

It's strange that it works fine in a separate cell
but returns an error in a loop

Thank you for helping

英文:

I get a zero when I'm running the code

factorial(n-x)*factorial(x)

It's my first time asking on stackoverflow.

below are my codes

def factorial(x):
    res = 1

    if x==0:
        return 1
    
    while x>0:
        res*=x
        x-=1
    return res

def combination(n,x):
    return factorial(n)/(factorial(n-x)*factorial(x))

def binomial(n,x,p):
    return combination(n,x)*(p**x)*((1-p)**(n-x))

pmf_values = []

n=200
x_list = np.arange(0,200+1)

for x in x_list:
    #print(x)
    #print((factorial(n-x)*factorial(x)))
    pmf_values.append(binomial(n,x,p))

sample = np.random.choice(
    x_list,size=10000,p=pmf_values)
plt.hist(sample)

I found that the error occurs when x is 0.
when I run it in a seperate cell,
it returns the correct answer 1.0

It's strange that it works fine in a seperate cell
but returns an error in a loop

Thank you for helping

答案1

得分: 0

I also got zeroes using your factorial function. This is strange. (Python 3.10.10).

I printed outputs:

阶乘(135)阶乘(65)
0
-9223372036854775808=0
阶乘(134)阶乘(66)
0
0=0
...
阶乘(66)阶乘(134)
0
0=0
阶乘(65)阶乘(135)
-9223372036854775808
0=0
...
阶乘(23)阶乘(177)
8128291617894825984
0=0
阶乘(22)阶乘(178)
-1250660718674968576
0=0
阶乘(21)阶乘(179)
-4249290049419214848
0=0
阶乘(20)阶乘(180)
2432902008176640000
0=0

The return of the factorial began to oscillate negative/positive above factorial(20), and went to zero beginning at factorial(66)

Try this, this worked for me and looks nicer anyway:

试试这个,这对我有效,而且看起来更好:

def factorial(x):
  res = 1 
  for i in range(2,x+1):
    res *= i
  return res 

Edit:

编辑:

I got it to work using your original factorial function:

我用你原始的阶乘函数让它正常工作了:

def factorial(x):
  x = int(x)
  res = 1
  while x > 1:
    res *= x
    x -= 1
  return res

So, this is an overflow issue with numpy. The x being passed in comes from np.arange()

所以,这是一个与numpy相关的溢出问题。传入的x来自于np.arange()

The reason my shorter function worked is because there is never math between numpy int64s, and if you convert x into a Python int before you do the math it will also work. Otherwise, the numpy int64 type will overflow

我较短的函数之所以有效是因为numpy int64之间从不进行数学运算,如果在进行数学运算之前将x转换为Python int,它也会有效。否则,numpy int64类型会溢出

Link to Numpy Overflow Documentation

查看Numpy溢出文档

$ pip freeze | grep numpy
numpy==1.24.3
英文:

I also got zeroes using your factorial function. This is strange. (Python 3.10.10).

I printed outputs:

factorial(135)*factorial(65)
0*-9223372036854775808=0
factorial(134)*factorial(66)
0*0=0
...
factorial(66)*factorial(134)
0*0=0
factorial(65)*factorial(135)
-9223372036854775808*0=0
...
factorial(23)*factorial(177)
8128291617894825984*0=0
factorial(22)*factorial(178)
-1250660718674968576*0=0
factorial(21)*factorial(179)
-4249290049419214848*0=0
factorial(20)*factorial(180)
2432902008176640000*0=0

The return of the factorial began to oscillate negative/positive above factorial(20), and went to zero beginning at factorial(66)

Try this, this worked for me and looks nicer anyway:

def factorial(x):
  res = 1 
  for i in range(2,x+1):
    res *= i
  return res 

Edit:

I got it to work using your original factorial function:

def factorial(x):
  x = int(x)
  res = 1
  while x > 1:
    res *= x
    x -= 1
  return res

So, this is an overflow issue with numpy. The x being passed in comes from np.arange()

The reason my shorter function worked is because there is never math between numpy int64s, and if you convert x into a Python int before you do the math it will also work. Otherwise, the numpy int64 type will overflow

https://numpy.org/doc/stable/user/basics.types.html#overflow-errors

$ pip freeze | grep numpy
numpy==1.24.3

huangapple
  • 本文由 发表于 2023年5月14日 08:59:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定