奇怪的问题,In range or not?
程序设定上下限为0.6和0.3,判断x和y的差是不是大于等于0.3小于0.6
为什么x=1.5,y=1.2时,和 x=4.5,y=4.2结果不一样?
图和程序见附件(LV8.0)
请大牛解释一下。
把结果的精度位数放大
不是0.3, 是0.2999999....
不对啊,放得再大也是0.300000000000000000
奇怪了,搂主说的问题实际上是小数的双精度数据判断相等的bug导致的吧
好像也不是判断相等的问题。。。
4.5-4.2判断起来的确是小于0.3的,难道真的是2.99999999?
太诡异了
看来的确是了。。。
4.5-4.2=0.299999999999999822
nndx,labview会不会算算数啊
Double是浮点数,
数在计算机中都是二进制,
十进制的有限小数转为二进制很可能是无限小数,
而位数是有限的,只能舍去后面部分,所以有的数不精确
只有2的N次方分之一的组合,才是二进制有限小数,才是精确的
比如0.5 0.25 0.57 0.375 0.2625
在编译器把你的十进制小数转换为二进制小数的时候,误差就已经发生了
比如0.1在计算机里已不是0.1,而是一个近似数
之所以你把0.1显示出来还是0.1,是舍入的结果
如果把所有运算全换成二进制的就明白了
版大真不是一般的zt
55555555555
我想到过这个问题
所以我举的两个例子
x1=4.5 y1=4.2
x2=1.5 y2=1.2
小数位都是一样的啊,如果是二进制的问题,是不是都应该有误差?
似乎当 x〉1.8 就出错。
你再仔细看看人说的,浮点数本来就存在舍入误差,你给的这两个例子,摄入误差可能
就是不一样,没有什么好奇怪的
hink, you are absolutely right.
In fact the problem of comparing floating-point numbers has nothing to do with LabVIEW or Windows. I am wondering if professors are teaching this topic in the "computer fundamentals" course nowadays. This should be a common problem mentioned at the very beginning of such a course. One should always be careful when comparing two floating-point numbers. As Kink has pointed out, all the numbers saved in a computer, whatever the CPU is, 32-bit, or 64-bit, they are saved in a finite-possible binary representations. It is strongly recommended to read this Wikipedia article:
http://en.wikipedia.org/wiki/Floating_point_numbers
If you cannot access wikipedia, just google IEEE-754 standard, you will find some useful information.
All right, let's come back to the original question. People NEED to compare floating-point numbers anyway. How can we do this? As I know, there is not a perfect solution. Fortunately, however, LabVIEW has a function "Check for Equality.vi" buried at <LabVIEW>\vi.lib\Waveform\WDTOps.llb, which is able to compare two floating-point numbers within some digits of precision. Try the attached example.
Another method you can try is to multiply all your floating-point numbers by a factor, say, 100, to make them all integers, and then compare. But this is method is not applicable in many cases, because (1) you have to decide what factor you need to multiply, (2) sometimes you have to use a huge factor which may overflow.
range 相关文章: