机器epsilon计算问题 [英] Machine epsilon computation issue

查看:72
本文介绍了机器epsilon计算问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我偶然发现了机器epsilon计算结果之间的差异.与0相比,PHP产生4.9406564584125E-324.对于1,它会弹出并显示1.1102230246252E-16.

有所不同.猜猜这与最初在PHP中默认设置的数据类型有关.

代码是:

 <?php//机器epsilon计算$ e = 1;$ eTmp = null;for($ i = 0; 0!= 0 + $ e; $ i ++){//将0更改为1会产生完全不同的结果$ e = $ e/2;if($ e!= 0){$ eTmp = $ e;}}echo $ eTmp;//var_dump($ eTmp);?> 

是否对两者之间的区别有任何澄清?以及如何通过手动分配变量或值使其在PHP中浮动呢?非常感谢您的想法!

解决方案

您的PHP实现似乎使用了通用的IEEE 754 64位二进制格式.在这种格式中,有限值有效地表示为符号,小于2 53 的整数 M 和之间的整数指数 e −1074和971(含).表示的值为+ M •2 e 或- M •2 e ,根据标志.(这种格式通常用 M 来描述,它是介于1和2之间的小数,并且在小数点后有一定位数.描述在数学上是等效的,仅用于不同目的.)

鉴于此,我们可以很容易地看到0之后的下一个可表示数字是+ 1•2 −1074 ,大约是4.94065645841246544•10 −324 .

在这种格式下,1可以表示为+ 1•2 0 .但是,要查看可以对1进行的最小更改是什么,我们必须通过使 M 尽可能大来对其进行归一化.1也可以用 M = 2 52 e = −52表示,从而导致+2 52 •2 −52 .通过这种形式,我们可以看到,通过将数字+(2 52 +1)•2 M 中,可以实现下一个大于1的可表示值.> −52 .

1与+(2 52 +1)•2 −52 之差当然是1•2 −52 就是2.220446049250313080847263336181640625•10 −16 .

请注意,使用 $ e =减少 $ e 后,您的代码记录了 $ eTmp (如果 $ e 不为零).$ e/2 ,这表示它会报告不会引起更改的 $ e 的第一个值.因此,它报告的是1.11e-16而不是2.22e-16,后者是确实导致更改为1的最后一个值.

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!

解决方案

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.

这篇关于机器epsilon计算问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆