IEEE-754 Float64中PI最准确的近似值?

发布于 2025-02-01 00:11:27 字数 156 浏览 2 评论 0原文

IEEE-754 Float64中最准确的PI近似值是什么?

FWIW似乎同时使用JavaScript和PHP使用3.1415926535897931159979979634685444185161590576171875 ,这可能是答案,我不知道。

what is the most accurate approximation of pi possible in IEEE-754 float64?

fwiw it seems both Javascript and PHP use 3.141592653589793115997963468544185161590576171875
, which might be the answer, i don't know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2025-02-08 00:11:27

是的,3.141592653589793115997963468544185161590576171875是IEEE-754 binary64 1 1 数字。它也可以写入十六进制的浮点数常数,0x1.921fb544442d18p1。 (我保留0x3.243F6A8888888D313198A2E03707344AP0L也要具有更宽的格式的值。 ,并且不需要小数点浮点数常数,因此,当您使用十六进制形式时,您可能更有可能获得正确的结果。

脚注

1 IEEE-754 2008使用标准的64位基底两种格式使用“ binary64”。它也称为“双重精度”。一些编程语言可能称其为float64float64

Yes, 3.141592653589793115997963468544185161590576171875 is the IEEE-754 binary641 number closest to π. It can also be written as a hexadecimal floating-point constant, 0x1.921fb54442d18p1. (I keep 0x3.243f6a8885a308d313198a2e03707344ap0L on hand to have the value for wider formats as well.) The C standard requires C implementations that use a base-two floating-point format to correctly round hexadecimal floating-point constants, and it does not require that for decimal floating-point constants, so you may be more likely to get a correct result when you use the hexadecimal form.

Footnote

1 IEEE-754 2008 uses “binary64” for the standard 64-bit base-two format. It is also called “double precision.” Some programming languages might call it float64 or Float64.

GRAY°灰色天空 2025-02-08 00:11:27

@Eric是正确的,IEEE-754-BINARY64 PI大约0.000000000000000001低于Real Pi,而下一个可能的增量为IEEE-754-Binary64大约0.0000000000000003高于真实PI,这是两者相同的零数量,16个零是相同的零数, 3大于1,这意味着Eric+JavaScript+PHP是正确的。证明这一点的PHP测试代码:

  • 警告,在我的笔记本电脑(i7-8565U,2018年中端笔记本电脑CPU)上运行大约7分钟的
  • 警告:未进行同行评审,可能是错误的
  • 警告:我假设0.000000000000000000000000000000000000000000000000000000000001 /code>是最低的增量,实际上有任何不同。如果我在这个假设中错了,则设置更低的增量可能会显示出不同的数字! (我没有足够的CPU+耐心来舒适地测试任何较低的增量。)
#!/usr/bin/env php
<?php
declare(strict_types=1);
function s($x) {
    $ret = number_format($x, bcscale(), '.', '');
    if(false!==strpos($ret, '.')) {
        $ret = rtrim($ret, '0');
        if(substr($ret, -1)==='.') {
            $ret = substr($ret, 0, -1);
        }
    }
    return $ret;
}
bcscale(100);
$realPi                = "3.141592653589793238462";
$IEEE64Pi              = "3.141592653589793115997";
$nextPossibleIncrement = "3.141592653589793560087";// $nextPossibleIncrement = "3.141592653589793560087173318606801331043243408203125"
$testIncrement         = "0.000000000000000000000001";
// var_dump(bcsub($realPi, $IEEE64Pi));die(); // IEEE64Pi this much LOWER than realPi:                            0.000000000000000122465
// var_dump(bcsub($realPi, $nextPossibleIncrement));die(); // nextPossibleIncrement this much HIGHER than realPi: 0.000000000000000321625

$test = $IEEE64Pi;
for(;;){
    $d1 = (float)$test;
    $new = bcadd($test, $testIncrement);
    $d2 = (float)$new;
    if($d1 !== $d2){
        echo "Error: $test != $new\n";
        echo "d1: ".s($d1)."\n";
        echo "d2: ".s($d2)."\n";
        break;
    }
    $test = $new;
    //echo ".";
}

输出:

$ time php test.php
Error: 3.1415926535897933380425680000000000000000000000000000000000000000000000000000000000000000000000000000 != 3.1415926535897933380425690000000000000000000000000000000000000000000000000000000000000000000000000000
d1: 3.141592653589793115997963468544185161590576171875
d2: 3.141592653589793560087173318606801331043243408203125

real    6m33.130s
user    6m13.593s
sys     0m0.421s

@Eric is correct, IEEE-754-binary64 pi is approximately 0.0000000000000001 lower than real pi, and the next possible increment to IEEE-754-binary64 is approximately 0.0000000000000003 higher than real pi, that's the same number of zeroes for both, 16 zeroes, and 3 is more than 1, which means Eric+Javascript+PHP are all right. PHP test code to prove it:

  • warning, takes like 7 minutes to run on my laptop (i7-8565U, a 2018 mid-range laptop cpu)
  • warning: not peer-reviewed, might be buggy
  • Warning: i assumed 0.000000000000000000000001 is the lowest increment that actually makes any difference. if i'm wrong in this assumption, setting an even lower increment might reveal a different number! (i don't have enough cpu+patience to comfortably test any lower increments..)
#!/usr/bin/env php
<?php
declare(strict_types=1);
function s($x) {
    $ret = number_format($x, bcscale(), '.', '');
    if(false!==strpos($ret, '.')) {
        $ret = rtrim($ret, '0');
        if(substr($ret, -1)==='.') {
            $ret = substr($ret, 0, -1);
        }
    }
    return $ret;
}
bcscale(100);
$realPi                = "3.141592653589793238462";
$IEEE64Pi              = "3.141592653589793115997";
$nextPossibleIncrement = "3.141592653589793560087";// $nextPossibleIncrement = "3.141592653589793560087173318606801331043243408203125"
$testIncrement         = "0.000000000000000000000001";
// var_dump(bcsub($realPi, $IEEE64Pi));die(); // IEEE64Pi this much LOWER than realPi:                            0.000000000000000122465
// var_dump(bcsub($realPi, $nextPossibleIncrement));die(); // nextPossibleIncrement this much HIGHER than realPi: 0.000000000000000321625

$test = $IEEE64Pi;
for(;;){
    $d1 = (float)$test;
    $new = bcadd($test, $testIncrement);
    $d2 = (float)$new;
    if($d1 !== $d2){
        echo "Error: $test != $new\n";
        echo "d1: ".s($d1)."\n";
        echo "d2: ".s($d2)."\n";
        break;
    }
    $test = $new;
    //echo ".";
}

output:

$ time php test.php
Error: 3.1415926535897933380425680000000000000000000000000000000000000000000000000000000000000000000000000000 != 3.1415926535897933380425690000000000000000000000000000000000000000000000000000000000000000000000000000
d1: 3.141592653589793115997963468544185161590576171875
d2: 3.141592653589793560087173318606801331043243408203125

real    6m33.130s
user    6m13.593s
sys     0m0.421s
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文