我的浮点数在打印时有多余的数字 [英] My floating point number has extra digits when I print it

查看:16
本文介绍了我的浮点数在打印时有多余的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将浮点数定义为 float transparent = 0.85f; 在下一行,我将它传递给一个函数 -- fcn_name(transparency) --但事实证明,变量 transparency 的值是 0.850000002,当我使用默认设置打印时,它是 0.850000002.对于值0.65f,它是0.649999998.

I define a floating point number as float transparency = 0.85f; And in the next line, I pass it to a function -- fcn_name(transparency) -- but it turns out that the variable transparency has value 0.850000002, and when I print it with the default setting, it is 0.850000002. For the value 0.65f, it is 0.649999998.

如何避免这个问题?我知道浮点数只是一个近似值,但是如果我定义一个只有几位小数的浮点数,我怎么能确保它没有被改变?

How can I avoid this issue? I know floating point is just an approximation, but if I define a float with just a few decimals, how can I make sure it is not changed?

推荐答案

以二进制格式表示的浮点值没有任何特定的十进制精度.仅仅因为您在某些规范中读到该数字可以表示一些固定数量的 decimal 数字,它实际上并没有多大意义.它只是将物理(和有意义的)二进制精度粗略转换为其意义不大的十进制近似值.

Floating-point values represented in binary format do not have any specific decimal precision. Just because you read in some spec that the number can represent some fixed amount of decimal digits, it doesn't really mean much. It is just a rough conversion of the physical (and meaningful) binary precision to its much less meaningful decimal approximation.

二进制浮点格式的一个特性是它只能精确地表示(在其尾数宽度的范围内)可以表示为 2 的有限幂和(包括 2 的负幂)的数字.0.50.250.75(十进制)等数字将以二进制浮点格式精确表示,因为这些数字是 2 的幂(2^-1, 2^-2) 或其总和.

One property of binary floating-point format is that it can only represent precisely (within the limits of its mantissa width) the numbers that can be expressed as finite sums of powers of 2 (including negative powers of 2). Numbers like 0.5, 0.25, 0.75 (decimal) will be represented precisely in binary floating-point format, since these numbers are either powers of 2 (2^-1, 2^-2) or sums thereof.

同时,像十进制0.1这样的数字不能用2的有限幂和来表示.浮点二进制中十进制0.1的表示有无限长度.这立即意味着 0.1 永远无法以有限二进制浮点格式精确表示.请注意,0.1 只有一位十进制数字.但是,这个数字仍然无法表示.这说明用十进制数字表示浮点精度并不是很有用.

Meanwhile, such number as decimal 0.1 cannot be expressed by a finite sum of powers of 2. The representation of decimal 0.1 in floating-point binary has infinite length. This immediately means that 0.1 cannot be ever represented precisely in finite binary floating-point format. Note that 0.1 has only one decimal digit. However, this number is still not representable. This illustrates the fact that expressing floating-point precision in terms of decimal digits is not very useful.

您示例中的 0.850.65 等值也是不可表示的,这就是为什么您看到这些值在转换为有限二进制浮点格式后会失真.实际上,您必须习惯这样一个事实,即您在日常生活中遇到的大多数小数小数都不能用二进制浮点类型精确表示,无论这些浮点类型有多大.

Values like 0.85 and 0.65 from your example are also non-representable, which is why you see these values distorted after conversion to a finite binary floating-point format. Actually, you have to get used to the fact that most fractional decimal numbers you will encounter in everyday life will not be representable precisely in binary floating-point types, regardless of how large these floating-point types are.

这篇关于我的浮点数在打印时有多余的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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