如果硬编码浮点数可以在IEEE 754中用二进制格式表示,那么它是否精确? [英] Is hardcode float precise if it can be represented by binary format in IEEE 754?

查看:103
本文介绍了如果硬编码浮点数可以在IEEE 754中用二进制格式表示,那么它是否精确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如0,0.5,0.15625,1,2,3 ...是从IEEE 754转换的值.其硬编码版本是否准确?

for example, 0 , 0.5, 0.15625 , 1 , 2 , 3... are values converted from IEEE 754. Are their hardcode version precise?

例如:

float a=0;
if(a==0){
    return true;
}

总是返回true吗?另一个例子:

always return true? other example:

float a=0.5;
float b=0.25;
float c=0.125;

a * b是否始终等于0.125,而a * b == c是否始终为true?还有一个示例:

is a * b always equal to 0.125 and a * b==c always true? And one more example:

int a=123;
float b=0.5;

a * b始终为61.5吗?还是一般来说,整数乘以IEEE 754二进制浮点数精确吗?

is a * b always be 61.5? or in general, is integer multiply by IEEE 754 binary float precise?

或更笼统的问题:如果值是硬编码,并且值和结果都可以用IEEE 754中的二进制格式表示(例如:0.5-0.125),那么值是否精确?

Or a more general question: if the value is hardcode and both the value and result can be represented by binary format in IEEE 754 (e.g.:0.5 - 0.125), is the value precise?

推荐答案

浮点数没有固有的模糊性.只是不能完全表示某些但不是全部实数.

There is no inherent fuzzyness in floating-point numbers. It's just that some, but not all, real numbers can't be exactly represented.

与固定宽度的十进制表示形式进行比较,比方说三位数.整数1可以用1.00表示,而1/10可以用0.10表示,但是1/3只能用0.33表示.

Compare with a fixed-width decimal representation, let's say with three digits. The integer 1 can be represented, using 1.00, and 1/10 can be represented, using 0.10, but 1/3 can only be approximated, using 0.33.

如果改为使用二进制数字,则整数1将表示为1.00(二进制数字),1/2表示为0.10,1/4表示为0.01,但是1/3(再次)只能(近似)表示.

If we instead use binary digits, the integer 1 would be represented as 1.00 (binary digits), 1/2 as 0.10, 1/4 as 0.01, but 1/3 can (again) only be approximated.

不过,有些事情要记住:

There are some things to remember, though:

  • 它不是与小数位相同的 数字. 1/10可以是 使用十进制数字精确地写为0.1,但不使用二进制 数字,无论您使用多少(无穷远).
  • 在实践中,很难跟踪可以拨打哪些号码
    表示,但不能. 0.5可以,但0.4不能.所以当你需要的时候 确切的数字,例如(经常)在赚钱时,您不应该 使用浮点数.
  • 根据某些消息来源,一些处理器会做一些奇怪的事情 在内部对数字执行浮点计算时 无法准确表示,导致结果以某种方式变化 实际上,这是不可预测的.
  • It's not the same numbers as with decimal digits. 1/10 can be written exactly as 0.1 using decimal digits, but not using binary digits, no matter how many you use (short of infinity).
  • In practice, it is difficult to keep track of which numbers can be
    represented and which can't. 0.5 can, but 0.4 can't. So when you need exact numbers, such as (often) when working with money, you shouldn't use floating-point numbers.
  • According to some sources, some processors do strange things internally when performing floating-point calculations on numbers that can't be exactly represented, causing results to vary in a way that is, in practice, unpredictable.

(我的观点是,说是的,浮点数本质上是模糊的,这实际上是一个合理的第一近似值,因此除非您确定自己的特定应用程序可以处理该问题,否则请不要使用它们. )

(My opinion is that it's actually a reasonable first approximation to say that yes, floating-point numbers are inherently fuzzy, so unless you are sure your particular application can handle that, stay away from them.)

要获取比您可能需要或想要的更多的详细信息,请阅读著名的每位计算机科学家都应了解的浮点运算法则.另外,该网站更易于访问:浮点指南.

For more details than you probably need or want, read the famous What Every Computer Scientist Should Know About Floating-Point Arithmetic. Also, this somewhat more accessible website: The Floating-Point Guide.

这篇关于如果硬编码浮点数可以在IEEE 754中用二进制格式表示,那么它是否精确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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