python ln2怎么写,在cpython中的_math.c中的ln2 const值

6

As user2357112 noted, this code came from FDLIBM. That was carefully written for IEEE-754 machines, where C doubles have 53 bits of precision. It doesn't really care what the actual log of 2 is, but cares a whole lot about the best 53-bit approximation to log(2).

正如user2357112所指出的,此代碼來自FDLIBM。這是為IEEE-754機器精心編寫的,其中C雙精度具有53位精度。它並不關心2的實際日志是什么,但是關注log(2)的最佳53位近似值。

To reproduce the intended 53-bit-precise value, 17 decimal digits would have sufficed.

要重現預期的53位精確值,17個十進制數字就足夠了。

So why did they use 21 decimal digits instead? My guess: 21 decimal digits is the minimum needed to guarantee that the converted result will be correct to 64 bits of precision. Which may have been an issue at the time, if a compiler somehow decided to convert the literal to a Pentium's 80-bit float format (which has 64 bits of precision).

那么為什么他們使用21位小數呢?我的猜測:21個十進制數字是保證轉換結果正確到64位精度所需的最小值。如果編譯器以某種方式決定將文字轉換為Pentium的80位浮點格式(具有64位精度),那么當時可能是一個問題。

So they displayed the 53-bit result with enough decimal digits so that if it were converted to a binary float format with 64 bits of precision, the trailing 11 bits (=64-53) would all be zeroes, thus ensuring they'd be working with the 53-bit value they intended from the start.

所以他們用足夠的十進制數字顯示53位結果,這樣如果它被轉換為具有64位精度的二進制浮點格式,則尾隨的11位(= 64-53)將全為零,從而確保它們是使用從一開始就打算使用的53位值。

>>> import mpmath

>>> x = mpmath.log(2)

>>> x

mpf('0.69314718055994529')

>>> mpmath.mp.prec = 64

>>> y = mpmath.mpf("0.693147180559945286227")

>>> x == y

True

>>> y

mpf('0.693147180559945286227')

In English, x is the 53-bit precise value of log(2), and y is the result of converting the decimal string in the code to a binary float format with 64 bits of precision. They're identical.

在英語中,x是log(2)的53位精確值,y是將代碼中的十進制字符串轉換為64位精度的二進制浮點格式的結果。他們是完全相同的。

In current reality, I expect all compilers now convert the literal to the native IEEE-754 double format, with 53 bits of precision.

在當前的現實中,我希望所有編譯器現在將文字轉換為本機IEEE-754雙格式,具有53位精度。

Either way, the code ensures the best 53-bit approximation to log(2) will be used.

無論哪種方式,代碼確保將使用log(2)的最佳53位近似。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值