【问题标题】:How to rectify code from zero division error:如何从零除错误中纠正代码:
【发布时间】:2020-02-17 19:29:58
【问题描述】:

我正在编写一个程序来查找数字的立方根。我得到除以零错误。我已将所有值更改为浮动,但仍然出现错误。

这里是代码。

def cube_root(num):
    import random
    g = random.randint(1,num+1)
    new_g = float(num/(g*g))
    check_ifclose(g,new_g,num)

def check_ifclose(g,new_g,num):
    from math import isclose
    if isclose(g, new_g, abs_tol=0.0000001):
        print("The cube root of ",num,"is ",float(new_g))
    else:
        g = float(new_g)
        new_g = float(num/(g*g))
        check_ifclose(g,new_g,num) 

这是我遇到的错误

ZeroDivisionError                         Traceback (most recent call last)
<ipython-input-53-7ad3ad630bd7> in <module>
----> 1 cube_root(16)

<ipython-input-52-46121e93a109> in cube_root(num)
      3     g = random.randint(1,num+1)
      4     new_g = float(num/(g*g))
----> 5     check_ifclose(g,new_g,num)
      6 
      7 def check_ifclose(g,new_g,num):

<ipython-input-52-46121e93a109> in check_ifclose(g, new_g, num)
     12         g = float(new_g)
     13         new_g = float(num/(g*g))
---> 14         check_ifclose(g,new_g,num)

ZeroDivisionError: float division by zero

【问题讨论】:

  • 请在此处添加错误跟踪。
  • 为什么不在每次迭代中打印这些值?你会看到会发生什么,然后你可以尝试调试它并重新考虑你的解决方案

标签: python data-structures divide-by-zero


【解决方案1】:

为什么不在每次迭代中打印这些值?你会看到会发生什么,然后你可以尝试调试它并重新考虑你的解决方案

只要做:

def cube_root(num):
    import random
    g = random.randint(1,num+1)
    new_g = float(num/(g*g))
    print("g", g, "new_g", new_g, "num", num)
    check_ifclose(g,new_g,num)

def check_ifclose(g,new_g,num):
    from math import isclose
    print("g", g, "new_g", new_g, "num", num)
    if isclose(g, new_g, abs_tol=0.0000001):
        print("The cube root of ",num,"is ",float(new_g))
    else:
        g = float(new_g)
        new_g = float(num/(g*g))
        check_ifclose(g,new_g,num) 

那么对于cube_root(16),你会得到输出:

>>> cube_root(16)
g 2 new_g 4.0 num 16
g 2 new_g 4.0 num 16
g 4.0 new_g 1.0 num 16
g 1.0 new_g 16.0 num 16
g 16.0 new_g 0.0625 num 16
g 0.0625 new_g 4096.0 num 16
g 4096.0 new_g 9.5367431640625e-07 num 16
g 9.5367431640625e-07 new_g 17592186044416.0 num 16
g 17592186044416.0 new_g 5.169878828456423e-26 num 16
g 5.169878828456423e-26 new_g 5.986310706507379e+51 num 16
g 5.986310706507379e+51 new_g 4.464794497196387e-103 num 16
g 4.464794497196387e-103 new_g 8.02633041618099e+205 num 16
g 8.02633041618099e+205 new_g 0.0 num 16
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 6, in cube_root
  File "<stdin>", line 9, in check_ifclose
  File "<stdin>", line 9, in check_ifclose
  File "<stdin>", line 9, in check_ifclose
  [Previous line repeated 8 more times]
  File "<stdin>", line 8, in check_ifclose
ZeroDivisionError: float division by zero

如您所见,这就像 Razmik 所说的小数字。

但是为什么呢?

因为您的解决方案有缺陷。你确定你应该这样计算new_g吗?如果你看这里,它会变得混乱 - 非常小,然后非常大,然后又小......你为什么不试试例如将 new_g 设置为旧的和当前新的平均值以平衡它?


编辑:决定检查我的最后一个建议,现在它工作正常。

def cube_root(num):
    import random
    g = random.randint(1,num+1)
    new_g = float(num/(g*g))
    print("g", g, "new_g", new_g, "num", num)
    check_ifclose(g,new_g,num)

def check_ifclose(g,new_g,num):
    from math import isclose
    print("g", g, "new_g", new_g, "num", num)
    if isclose(g, new_g, abs_tol=0.0000001):
        print("The cube root of ",num,"is ",float(new_g))
    else:
        g = float(new_g)
        new_g = (g+float(num/(g*g)))/2
        check_ifclose(g,new_g,num) 
>>> cube_root(16)
g 3 new_g 1.7777777777777777 num 16
g 3 new_g 1.7777777777777777 num 16
g 1.7777777777777777 new_g 3.420138888888889 num 16
g 3.420138888888889 new_g 2.3939850310351836 num 16
g 2.3939850310351836 new_g 2.592869433541053 num 16
g 2.592869433541053 new_g 2.4863846555355664 num 16
g 2.4863846555355664 new_g 2.537249169224923 num 16
g 2.537249169224923 new_g 2.51131729000868 num 16
g 2.51131729000868 new_g 2.524147960725603 num 16
g 2.524147960725603 new_g 2.517700180896523 num 16
g 2.517700180896523 new_g 2.52091579334918 num 16
g 2.52091579334918 new_g 2.519305938864469 num 16
g 2.519305938864469 new_g 2.520110351423893 num 16
g 2.520110351423893 new_g 2.51970801680198 num 16
g 2.51970801680198 new_g 2.5199091519863974 num 16
g 2.5199091519863974 new_g 2.5198085763676823 num 16
g 2.5198085763676823 new_g 2.519858862169772 num 16
g 2.519858862169772 new_g 2.519833718766991 num 16
g 2.519833718766991 new_g 2.5198462903429375 num 16
g 2.5198462903429375 new_g 2.519840004523604 num 16
g 2.519840004523604 new_g 2.519843147425431 num 16
g 2.519843147425431 new_g 2.519841575972557 num 16
g 2.519841575972557 new_g 2.519842361698504 num 16
g 2.519842361698504 new_g 2.519841968835408 num 16
g 2.519841968835408 new_g 2.5198421652669256 num 16
g 2.5198421652669256 new_g 2.519842067051159 num 16
The cube root of  16 is  2.519842067051159

【讨论】:

  • 没问题。数学说你的数列应该是收敛的,它的极限应该是根。让我们看看它在现实中的样子:g1 = n/(g0*g0), g2 = n/(g1*g1) = (g0^4)/n... 等等。如果g0>1(就像那样),奇数g趋向于0,偶数g趋向于无穷大。 |我的建议甚至不是基于数学(我之前没有写下来),而是基于数值计算中最常用的方法:你想调整当前值,而不是使用完全不同的东西。 ^_^
  • 是的。在我的逻辑中,我只是倾向于零和无穷大。
  • 我没有想通逻辑。我有点愚蠢地认为,如果我将我的第一个“g”设置为大于 1,我确信在整个迭代过程中“g”值高于 1!
【解决方案2】:

问题是,如果您的数字非常小,例如 g(小于 1e-200),那么 g * g python “对待”为 0。请在此处查看 https://docs.python.org/3.7/tutorial/floatingpoint.html。

【讨论】:

  • 但是我做了 g = random.rantint(1,num) 使 g 的值至少为 1。那么,我该如何解决呢?
【解决方案3】:

在new_g = float(num/(g*g)) 中,g*g 是浮点数。小数点多于 308,导致值变为 0.0。

求立方根可以试试num**1/3

【讨论】:

  • 我正在尝试在不使用内置方法计算功率的情况下执行程序
  • 可以使用一种方法,只取几个小数点。
猜你喜欢
  • 2017-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
相关资源
最近更新 更多