Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.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 - Fatal编程技术网

Python 对数间模

Python 对数间模,python,Python,我正在尝试在Python中检查math.log(x,base)是否有小数: import math # math.log(8, 2) = 3 print math.log(8) print math.log(2) print math.log(8) / math.log(2) print 2.07944154168 % 0.69314718056 print math.log(8) % math.log(2) 输出为: 2.07944154168 0.69314718056 3.0 0.0

我正在尝试在Python中检查math.log(x,base)是否有小数:

import math

# math.log(8, 2) = 3
print math.log(8)
print math.log(2)
print math.log(8) / math.log(2)
print 2.07944154168 % 0.69314718056
print math.log(8) % math.log(2)
输出为:

2.07944154168
0.69314718056
3.0
0.0
0.69314718056

为什么第四个打印行返回零,而第五个不返回零?

这是我使用python 3得到的结果

>>> print (math.log(8))
2.0794415416798357
>>> print (math.log(2))
0.6931471805599453
>>> print (math.log(8) / math.log(2))
3.0
>>> print (2.07944154168 % 0.69314718056)
0.0
>>> print (math.log(8) % math.log(2))
0.6931471805599452
>>> print (2.0794415416798357 % 0.6931471805599453)
0.6931471805599452

在您的示例(python 2?)中,math.log的精度似乎不够。

这可能会因为是一个副本而关闭,但只是为了让您可以看到它是如何运行的:

>>> import math
>>> math.log(8)
2.0794415416798357
>>> math.log(2)
0.6931471805599453
现在假设您需要计算
math.log(8)%math.log(2)
。将
math.log(2)
除以
math.log(8)
后,需要计算余数。让我看看,它会在3次吗

  0.6931471805599453
+ 0.6931471805599453
+ 0.6931471805599453
--------------------
  2.0794415416798359
哇!我们超出了2.0794415416798357的值,这意味着它实际上是两倍的:

  0.6931471805599453
+ 0.6931471805599453
--------------------
  1.3862943611198906
好的,剩下的是什么

  2.0794415416798359
- 1.3862943611198906
--------------------
  0.6931471805599453
So TL;DR由于舍入错误,您的余数接近
math.log(2)
。它不会准确地进入三次。它用剩下的大约
math.log(2)
进行了两次


是的,当您打印商时,它会显示
3.0
,但同样,这是浮点中的所有舍入错误,这不是Python独有的。

这有帮助吗?您是否注意到%返回的值与第二个运算符0.69314718056的值相同???a%b=b。。。lol@mlwn是的,我做了,但我不知道该怎么想。@RayToal有助于理解一些东西,但我如何检查log(x,base)是否给出了不带小数的结果?是的,我在tutorialpoint中尝试过,使用Python 2.7,我知道现在发生了什么。也许我应该读一些书,以便更深入地了解文章中的建议。你说TL是什么意思;博士