我之前正在玩/r/dailyprogrammer's轻松挑战;在这种情况下,你很难发现Dottie号码(~0.739085)。虽然挑战需要radians
,但我决定暂时将其保持在学位状态。以下是一些快速代码:
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
输出没问题,我设法找到了所要求的dottie数,但我无法理解为什么递归函数即使在当前值等于前一个值之后仍继续执行(因为那是基本情况)我在函数中定义的)。这与浮点精度有关吗?值是在某个时刻被截断还是我没有正确打印它?
答案 0 :(得分:7)
显示给您的数字不是实际值,因为在数字上调用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.
你可以看到最后几次迭代不一样。
答案 1 :(得分:4)
该功能不继续。您正在看到堆栈展开,因为递归调用返回。
换句话说,您在后打印的Previous = ... Current =
信息进行了递归调用,因此您反向看到此信息。
那些打印调用没有显示完整的精度; Python只打印前12个左右的小数,而不是浮点数可以建模的完整50+个数字。
您可以明确地将数字格式化为更高的精度:
print 'Dottie number: {:.53f}'.format(cur)
和
print 'Previous = {:.53f}\tCurrent = {:.53f}'.format(prev, cur)
请注意,这取决于您的确切平台是否实际找到了数字;在OS X 10.10上的Python 2.7上,我用尽了递归堆栈。您不应该使用完全匹配,而是使用阈值差异:
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)
答案 2 :(得分:1)
问题是您要将实数与==运算符进行比较。建议您比较这样的实数:
if abs(cur - prev) <= allowedDifference:
您必须使用非常小的数字定义allowedDifference,例如0.000001甚至更小。
这也是你在C / C ++上遇到的一个问题。