浮点表示和精度 [英] float representation and precision

查看:76
本文介绍了浮点表示和精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




昨天我发现了一些对经验丰富的C ++程序员来说不会感到惊讶的东西 - 但我真的很惊讶。我有这行代码:


浮动x = 0.9f;


当我在VS.NET中查看它时2003调试器,它说x有0.89999998的价值!当我将x打印到日志文件时,它是0.899999976158142090000000000000。现在,为什么呢?据我所知,由于机器限制,浮点运算的结果总是有些不精确,但0.9?任何人都可以向我解释一下吗?

Hi,

yesterday I discovered something that won''t come as a surprise to more experienced C++ - programmers - but I was genuinely amazed. I had this line of code:

float x = 0.9f;

and when I had a look at it in the VS.NET 2003 debugger, it said x had avalue of 0.89999998 ! When I printf''ed x to a logfile, it was 0.899999976158142090000000000000. Now, why on earth is that? I understand that the results of floatingpoint operations will always be somewhat imprecise due to machine limitations, but 0.9? Can anybody explain this to me?

推荐答案



Sebastian写道:

Sebastian wrote:


昨天我发现了一些对经验丰富的C ++程序员来说不会感到惊讶的东西 - 但我真的很惊讶。我有这行代码:

浮动x = 0.9f;

当我在VS.NET 2003调试器中查看它时,
它说x的值为0.89999998!当我打印x / /到日志文件时,它是0.899999976158142090000000000000。现在,
为什么到底是这样的?据我所知,由于机器的限制,浮点运算的结果总是有些不精确,但是0.9?有人可以向我解释这个吗?
Hi,

yesterday I discovered something that won''t come as a
surprise to more experienced C++ - programmers - but I
was genuinely amazed. I had this line of code:

float x = 0.9f;

and when I had a look at it in the VS.NET 2003 debugger,
it said x had a value of 0.89999998 ! When I printf''ed x
to a logfile, it was 0.899999976158142090000000000000. Now,
why on earth is that? I understand that the results of
floatingpoint operations will always be somewhat imprecise
due to machine limitations, but 0.9? Can anybody explain
this to me?




IEEE754,这是我在大多数
平台上使用的浮点表示意识到,是一个基本的两个编码浮点

值。正如基数10有不合理的分数(有点像1/3)

那么基数为2。这里的冒险是,基数为2的非理性分数与基数为10的非理性分数不同。显然9/10是非理性分数之一在基地

2.


问候,


Jon Trauntvein



IEEE754, which is the floating point representation used on most
platforms that I am aware of, is a base two encoding for floating point
values. Just as base 10 has irrational fractions (somewhat like 1/3)
so does base two. The adventure here is that the fractions that are
irrational in base two are different from those that are irrational in
base 10. Apparently 9/10 is one of those irrational fractions in base
2.

Regards,

Jon Trauntvein


Sebastian写道:
Sebastian wrote:


昨天我发现了一些不会让人感到惊讶的东西<经验丰富的C ++ - 程序员 - 但我真的很惊讶。我有这个代码行:

浮动x = 0.9f;

当我在VS.NET 2003调试器中查看它时,它说x的值为0.89999998!当我将x打印到日志文件时,它是
0.899999976158142090000000000000。现在,为什么呢?我理解由于机器的限制,浮点运算的结果总是有些不精确,但是0.9?有人可以向我解释这个吗?
Hi,

yesterday I discovered something that won''t come as a surprise to more
experienced C++ - programmers - but I was genuinely amazed. I had this
line of code:

float x = 0.9f;

and when I had a look at it in the VS.NET 2003 debugger, it said x had a
value of 0.89999998 ! When I printf''ed x to a logfile, it was
0.899999976158142090000000000000. Now, why on earth is that? I understand
that the results of floatingpoint operations will always be somewhat
imprecise due to machine limitations, but 0.9? Can anybody explain this to
me?




浮点数接近连续的数字范围。既然那个
范围有无数不同的数字,就不可能将
存储在计算机中的每个数字。

请记住,计算机不是在10号基础上计算,因为人类通常是b $ b,但是基数为2.因此对我们来说看起来很简单的数字(如0.9)可以

in base 2具有无限数量的数字,因此无法将
存储在基数为2的浮点数中。



Floating point numbers approximate a continuous range of numbers. Since that
range has an infinite count of different numbers, it isn''t possible to
store every number exactly in a computer.
Remember that computers are not calculating in base 10 as humans usually
are, but in base 2. So a number that might look simple to us (like 0.9) can
in base 2 have an infinite number of digits, and thus be impossible to
store exactly in a base 2 floating point number.


哇。谢谢你的快速回答。我喜欢这个 - 二元系统中存在无理数的想法从未在我脑海中浮现,但听起来绝对符合逻辑。 :-)

2005年5月7日05:21:19 -0700,JH Trauntvein< j。********** @ comcast.net>写道:
Wow. Thanks for the speedy answer. I like this - the idea that there would be irrational numbers in the binary system never crossed my mind, butit sounds absolutely logical. :-)
On 7 May 2005 05:21:19 -0700, JH Trauntvein <j.**********@comcast.net> wrote:

塞巴斯蒂安写道:

Sebastian wrote:


昨天我发现了一些不会来的东西对经验丰富的C ++程序员来说是一个惊喜 - 但我确实感到非常惊讶。我有这行代码:

浮动x = 0.9f;

当我在VS.NET 2003调试器中查看它时,
它说x的值为0.89999998!当我打印x / /到日志文件时,它是0.899999976158142090000000000000。现在,
为什么到底是这样的?据我所知,由于机器的限制,浮点运算的结果总是有些不精确,但是0.9?有人可以向我解释这个吗?
Hi,

yesterday I discovered something that won''t come as a
surprise to more experienced C++ - programmers - but I
was genuinely amazed. I had this line of code:

float x = 0.9f;

and when I had a look at it in the VS.NET 2003 debugger,
it said x had a value of 0.89999998 ! When I printf''ed x
to a logfile, it was 0.899999976158142090000000000000. Now,
why on earth is that? I understand that the results of
floatingpoint operations will always be somewhat imprecise
due to machine limitations, but 0.9? Can anybody explain
this to me?



IEEE754是我所知道的大多数平台上使用的浮点表示,它是一个基本的两个编码对于浮点值。正如基数10具有不合理的分数(有点像1/3),基数2也是如此。这里的冒险是在基数2中不合理的分数与在基数10中不合理的分数不同。显然9/10是基数中的非理性分数之一
2。

问候,

Jon Trauntvein



IEEE754, which is the floating point representation used on most
platforms that I am aware of, is a base two encoding for floating point
values. Just as base 10 has irrational fractions (somewhat like 1/3)
so does base two. The adventure here is that the fractions that are
irrational in base two are different from those that are irrational in
base 10. Apparently 9/10 is one of those irrational fractions in base
2.

Regards,

Jon Trauntvein




-


问候,


Sebastian



--

Regards,

Sebastian


这篇关于浮点表示和精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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