我的浮点数有额外的数字,当我打印出来 [英] My floating point number has extra digits when I print it

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

问题描述

我定义了一个浮点数为浮动透明度= 0.85f; 键,下一行,我把它传递给一个函数 - fcn_name (透明度) - 但事实证明,变量透明度的价值 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?

推荐答案

浮点值重新以二进制格式psented $ P $没有任何具体的小数的precision。仅仅因为你在某些规范,该数字可以重新present的一些固定阅读量的小数的数字,它并不真正意味着什么。它是物理(和有意义)二进制precision其更有意义的十进制近似值只是一个粗略的转换。

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.

二进制浮点格式的一个特性是,它只能重新present precisely,可以前$ P $的数字(它的尾数宽的范围内)pssed的权力是有限的总和2(包括2负的权力)。号码,如 0.5 0.25 0.75 (十进制)会在二进制浮点格式pcisely重新presented $ p $,因为这些数字是任列强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的幂十进制的重新presentation的有限和pssed前$ P $ 0.1 浮点二进制文件的无限的长度。这直接意味着 0.1 不能永远在有限的二进制浮点格式pcisely重新presented $ P $。需要注意的是 0.1 只有一个十进制数。然而,这个数字仍没有重新presentable。这说明了一个事实,即在十进制数字前而言pressing浮点precision不是非常有用的。

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.85 0.65 从你的榜样也非再presentable,这是为什么你看到这些值转换为有限二进制浮点格式后扭曲。其实,你必须习惯这样的事实,大多数分数小数你会在日常生活中遇到不会重新二进制浮点类型$ P ​​$ psentable precisely,的无论多大,这些浮点点类型

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天全站免登陆