Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.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 为什么math.log接受大的整数值?_Python_Floating Point_Logarithm - Fatal编程技术网

Python 为什么math.log接受大的整数值?

Python 为什么math.log接受大的整数值?,python,floating-point,logarithm,Python,Floating Point,Logarithm,我得到了一个有效的结果 现在通过sqrt更改log,您将获得(如预期的那样): 因此,我假设在log函数中有一个整数参数技巧,使用整数对数,但我在文档中没有找到。只是: math.log(x[,base]) 使用一个参数,返回x的自然对数(以e为底) 使用两个参数,将x的对数返回给给定的基数,计算为log(x)/log(base) 哪里有文档记录?我认为线程是有用的,因为python现在使用长int,避免溢出的诀窍是使用\u PyLong\u Frexp函数,请参见,并使用另一个公式来计算log

我得到了一个有效的结果

现在通过
sqrt
更改
log
,您将获得(如预期的那样):

因此,我假设在
log
函数中有一个整数参数技巧,使用整数对数,但我在文档中没有找到。只是:

math.log(x[,base])

使用一个参数,返回x的自然对数(以e为底)

使用两个参数,将x的对数返回给给定的基数,计算为log(x)/log(base)

哪里有文档记录?

我认为线程是有用的,因为python现在使用长int,避免溢出的诀窍是使用
\u PyLong\u Frexp
函数,请参见,并使用另一个公式来计算
log
函数,即使在尝试将长int转换为Double时引发了
溢出错误,检查模块上的
loghelper

我最终深入研究了python并发现:

OverflowError: int too large to convert to float
我将为您保留源代码的其余部分(检查链接),但我从中了解到Python会检查传递的参数是否为整数,如果为整数,则不要使用math lib(如果为int,则自己做)。另外:即使对于大整数,也很容易计算一个合适的对数,但libm本身无法做到这一点——loghelper可以做到

如果是双精度的,则调用本机数学库

从源代码注释中,我们可以看到Python尽最大努力提供结果,即使在发生溢出的情况下也是如此(这里是双溢出的转换,但是仍然可以计算日志。清除异常并继续)


因此,多亏了
log
函数的python包装,python能够计算大整数的对数(这是特定于某些函数的,因为像
sqrt
这样的一些函数无法做到),并且它已经被记录在案,但仅在源代码中,这可能使它成为Jon暗示的一个实现细节

“CPython实现细节:数学模块主要由围绕平台C数学库函数的薄包装组成。”-我认为这包括
log
,因此这可能与平台有关,不一定是Python问题。在我的机器(osx)上,你真的得到了
760**890
的有效结果吗这是
5903.65340562
但是wolfram alpha说:
451.131905409
@user1767754尝试890*log(760)来确认结果是正确的。不确定,你在wolfram alpha上找到了什么。@Jean-Françoisfare:log(760*890)返回“5903.65340561953531894679467199852448280840806381065970869403…”。。。。“.在Wolfram Alpha中。这似乎是正确的(用我自己的BigInteger的Delphi实现进行了测试)。@user1767754:Wolfram Alpha为我返回5903.etc。你从哪里得到的451.etc值?所有这些链接让我头晕目眩。你不能提取答案中的有效位吗?
\u PyLong\u Frexp
返回初始长整数
arg
的近似值,该值在
loghelper
中给出,借助于双
x
和指数
e
arg~x*2**e
)的帮助,
log
计算为
log~log(x*2**e)=对数(x)+对数(2)*e
。我缺少使用
x,e
的近似值的细节,但是您可以在提供的链接中的
\u PyLong\u Frexp
的实现中找到它。
OverflowError: int too large to convert to float
/* A decent logarithm is easy to compute even for huge ints, but libm can't
   do that by itself -- loghelper can.  func is log or log10, and name is
   "log" or "log10".  Note that overflow of the result isn't possible: an int
   can contain no more than INT_MAX * SHIFT bits, so has value certainly less
   than 2**(2**64 * 2**16) == 2**2**80, and log2 of that is 2**80, which is
   small enough to fit in an IEEE single.  log and log10 are even smaller.
   However, intermediate overflow is possible for an int if the number of bits
   in that int is larger than PY_SSIZE_T_MAX. */

static PyObject*
loghelper(PyObject* arg, double (*func)(double), const char *funcname)
{
    /* If it is int, do it ourselves. */
    if (PyLong_Check(arg)) {
        double x, result;
        Py_ssize_t e;

        ...