Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么这个递归函数在基本情况得到满足后仍然继续_Python_Recursion - Fatal编程技术网

Python 为什么这个递归函数在基本情况得到满足后仍然继续

Python 为什么这个递归函数在基本情况得到满足后仍然继续,python,recursion,Python,Recursion,早些时候,我在玩轻松挑战;在这种情况下,您将面临发现Dottie编号(~0.739085)的挑战。虽然挑战需要弧度,但我决定暂时保持弧度。下面是一些快速代码: from math import cos def func(n): prev = n cur = cos(n) if cur == prev: print 'Dottie number: ' + str(cur) else: func(cur) print 'Pr

早些时候,我在玩轻松挑战;在这种情况下,您将面临发现Dottie编号(~0.739085)的挑战。虽然挑战需要弧度,但我决定暂时保持弧度。下面是一些快速代码:

from math import cos

def func(n):
    prev = n
    cur = cos(n)

    if cur == prev:
        print 'Dottie number: ' + str(cur)
    else:
        func(cur)
    print 'Previous = ' + str(prev) + '\tCurrent = ' + str(cur)

func(1)
但是,我从输出中注意到以下示例:

Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133215       Current = 0.739085133215
Previous = 0.739085133216       Current = 0.739085133215
Previous = 0.739085133214       Current = 0.739085133216
Previous = 0.739085133216       Current = 0.739085133214
Previous = 0.739085133213       Current = 0.739085133216
Previous = 0.739085133218       Current = 0.739085133213
Previous = 0.739085133211       Current = 0.739085133218
Previous = 0.739085133221       Current = 0.739085133211
Previous = 0.739085133206       Current = 0.739085133221
Previous = 0.739085133229       Current = 0.739085133206
Previous = 0.739085133195       Current = 0.739085133229
Previous = 0.739085133245       Current = 0.739085133195
Previous = 0.739085133171       Current = 0.739085133245
Previous = 0.739085133281       Current = 0.739085133171
Previous = 0.739085133117       Current = 0.739085133281
Previous = 0.739085133361       Current = 0.739085133117
Previous = 0.739085132999       Current = 0.739085133361
Previous = 0.739085133536       Current = 0.739085132999
Previous = 0.739085132739       Current = 0.739085133536
Previous = 0.739085133922       Current = 0.739085132739
Previous = 0.739085132166       Current = 0.739085133922
Previous = 0.739085134772       Current = 0.739085132166
Previous = 0.739085130904       Current = 0.739085134772
Previous = 0.739085136647       Current = 0.739085130904
Previous = 0.739085128121       Current = 0.739085136647
Previous = 0.739085140777       Current = 0.739085128121
Previous = 0.739085121989       Current = 0.739085140777
Previous = 0.739085149881       Current = 0.739085121989
Previous = 0.739085108474       Current = 0.739085149881
Previous = 0.739085169945       Current = 0.739085108474
Previous = 0.739085078689       Current = 0.739085169945
Previous = 0.739085214161       Current = 0.739085078689
Previous = 0.739085013048       Current = 0.739085214161
Previous = 0.739085311607       Current = 0.739085013048
Previous = 0.739084868387       Current = 0.739085311607
Previous = 0.739085526362       Current = 0.739084868387
Previous = 0.739084549575       Current = 0.739085526362
Previous = 0.739085999648       Current = 0.739084549575
Previous = 0.739083846965       Current = 0.739085999648
Previous = 0.739087042695       Current = 0.739083846965
Previous = 0.739082298522       Current = 0.739087042695
Previous = 0.739089341403       Current = 0.739082298522
Previous = 0.739078885995       Current = 0.739089341403
Previous = 0.739094407379       Current = 0.739078885995
Previous = 0.739071365299       Current = 0.739094407379
Previous = 0.739105571927       Current = 0.739071365299
Previous = 0.739054790747       Current = 0.739105571927
Previous = 0.73913017653        Current = 0.739054790747
Previous = 0.739018262427       Current = 0.73913017653
Previous = 0.739184399771       Current = 0.739018262427
Previous = 0.738937756715       Current = 0.739184399771
Previous = 0.739303892397       Current = 0.738937756715
Previous = 0.738760319874       Current = 0.739303892397
Previous = 0.739567202212       Current = 0.738760319874
Previous = 0.738369204122       Current = 0.739567202212
Previous = 0.740147335568       Current = 0.738369204122
Previous = 0.737506890513       Current = 0.740147335568
Previous = 0.74142508661        Current = 0.737506890513
Previous = 0.735604740436       Current = 0.74142508661
Previous = 0.744237354901       Current = 0.735604740436
Previous = 0.731404042423       Current = 0.744237354901
Previous = 0.750417761764       Current = 0.731404042423
Previous = 0.722102425027       Current = 0.750417761764
Previous = 0.763959682901       Current = 0.722102425027
Previous = 0.701368773623       Current = 0.763959682901
Previous = 0.793480358743       Current = 0.701368773623
Previous = 0.654289790498       Current = 0.793480358743
Previous = 0.857553215846       Current = 0.654289790498
Previous = 0.540302305868       Current = 0.857553215846
Previous = 1    Current = 0.540302305868
输出很好,我按照请求找到了点编号,但我不明白为什么递归函数在当前值等于前一个值后仍继续执行(因为这是我在函数中定义的基本情况)。这与浮点精度有关吗?值是否在某个点被截断,或者我只是没有正确打印?

函数没有继续。当递归调用返回时,您将看到堆栈展开

换句话说,
Previous=。。。当前=
在执行递归调用后打印的信息,因此您看到的是相反的信息

这些打印调用不能显示全部精度;Python只打印前12位左右的小数,而不是浮点数所能模拟的全部50+位数

您可以显式地将数字格式设置为更高的精度:

print 'Dottie number: {:.53f}'.format(cur)

请注意,这取决于你的确切平台,你是否能找到这个数字;在Python2.7和OSX10.10上,我用完了递归堆栈。您不应使用精确匹配,而应使用阈值差:

from math import cos

def func(n, precision=10):
    prev = n
    cur = cos(n)
    if abs(cur - prev) < (10 ** -precision):
        print 'Dottie number: {1:.{0}f}'.format(precision, cur)
    else:
        func(cur)
    print 'Previous = {1:.{0}f}\tCurrent = {2:.{0}f}'.format(precision, prev, cur)
从数学导入cos
def func(n,精度=10):
prev=n
cur=cos(n)
如果abs(当前-先前)<(10**-精度):
打印“点号:{1:.{0}f}”。格式(精度,cur)
其他:
func(cur)
打印'Previous={1:.{0}f}\t当前={2:.{0}f}'。格式(精度、上一个、当前)

显示给您的数字不是实际值,因为对数字调用
str
不会显示所有数字。如果改用
repr
,您将得到以下结果:

Dottie number: 0.7390851332151607
Previous = 0.7390851332151607   Current = 0.7390851332151607
Previous = 0.7390851332151606   Current = 0.7390851332151607
Previous = 0.7390851332151608   Current = 0.7390851332151606
Previous = 0.7390851332151603   Current = 0.7390851332151608
Previous = 0.7390851332151611   Current = 0.7390851332151603
# ... etc.

您可以看到最后几次迭代是不一样的。

问题是您正在将实数与==运算符进行比较。建议您像这样比较实数:

if abs(cur - prev) <= allowedDifference:

如果abs(cur-prev)我想他指的是前几行在两列中的值相同的事实,而不是它们在开头而不是结尾的事实。这不是我关心的问题(事实上我当时的确挠头了,但在尝试了一些其他东西之后,我注意到了这一点);我关心的是,为什么以前的值和当前值在不应该打印的时候被打印多次。它们实际上不是相同的数字。你只是没有打印出足够的数字来显示仍然在发生变化的“较小”数字。我喜欢额外的精度!谢谢Martijn@Juxhin两个不同的基数为2的浮点数可能会被舍入,使其看起来相同,即使具有额外的精度,因此您确实应该小心比较二进制数的十进制表示形式;他们的代表性不足以使其可靠。相反,做一些类似于abs(a-b)
的事情,看看它们是否相同(或非常相似)!我认为简单地使用
str
并不能在涉及浮点数时减少它。谢谢:-)
if abs(cur - prev) <= allowedDifference: