Machine epsilon computation issue

半城伤御伤魂 提交于 2020-01-16 18:50:46

问题


I stumbled on the difference between the result of Machine epsilon calculation. When compared to 0 PHP yields 4.9406564584125E-324. While for 1 it pops up with 1.1102230246252E-16.

Quite a difference. Guess it's something with the type of data initially set by default in PHP.

The code is:

<?php
//Machine epsilon calculation

$e = 1;
$eTmp = null;

for ($i = 0; 0 != 0 + $e; $i++){ //Changing 0 by 1 produces absolutely different result
     $e = $e/2;
    if ($e != 0) {$eTmp = $e;}
}

echo $eTmp;
//var_dump($eTmp);

?>

Any clarification on the difference between the two? And how can a variable or value by assigned manually to float in PHP? Many thanks for your ideas!


回答1:


Your PHP implementation appear to be using the common IEEE 754 64-bit binary format. In this format, finite values are represented, effectively, as a sign, an integer M less than 253, and an integer exponent e between −1074 and 971, inclusive. The value represented is +M•2e or −M•2e, according to the sign. (This format will often be described with M being a fraction between 1 and 2 with a certain number of bits after the radix point. The descriptions are mathematically equivalent, just useful for different purposes.)

Given this, we can easily see that the next representable number after 0 is +1•2−1074, which is approximately 4.94065645841246544•10−324.

In this format as stated, 1 can be represented as +1•20. However, to see what the smallest change that can be made to 1 is, we must normalize it by making M as large as possible. 1 can also be represented with M = 252 and e = −52, resulting in +252•2−52. In this form, we can see the next representable value greater than 1 is achieved by adding 1 to M, mkaing the number +(252+1)•2−52.

The difference between 1 and +(252+1)•2−52 is of course 1•2−52, which is exactly 2.220446049250313080847263336181640625•10−16.

Note that your code records $eTmp (if $e is not zero) after reducing $e with $e = $e/2, which means it reports the first value of $e that does not cause a change. Thus it reports 1.11e-16 rather than 2.22e-16, which is the last value that does cause a change to 1.



来源:https://stackoverflow.com/questions/48830402/machine-epsilon-computation-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!