C 浮点数和双重比较
C float and double comparisons
我正在比较 C 中的简单浮点数和双精度数,特别是两者的值 8.7。现在我为每个变量分配 8.7,当我打印时,我得到两个值的结果都是 8.7000。为什么编译器添加了这些零。我想问的主要问题是,是否还有其他我没有看到的数字,例如隐藏在尾随零之后的数字。我读到我不应该因为缺乏精度而与 float 进行这样的比较,但我认为这么小的值肯定可以存储 8.7 并具有将自身与另一个 8.7 值进行比较所需的精度?
我唯一担心的是它实际上在内存中的某个地方表示为 8.70000003758 或其他东西,这让我的比较失败了?我尝试使用 %.20f 进行 printf 以查看可能隐藏的任何其他数字,但我认为这只是创建了本来不存在的数字,因为数字的整体精度更改为 8.6918734634834929 或类似的东西。
I'm comparing simple floats and doubles in C, specifically the value 8.7 for both of them.
错误的选择,因为 8.7 没有精确的二进制表示。
Now I assign 8.7 to each variable, when I print I get a result of 8.7000 for both values. Why has the compiler added these zeros.
没有,你的打印例程有。
And the main question I wanted to ask was is there any further numbers that I'm not seeing, as in hidden after the trailing zeros.
当然,因为 8.7 没有精确的二进制表示。 (试着写成2的整数次幂之和,不行。)
I read that I shouldn't do comparisons like this with float because of a lack of precision, but I thought with such a small value surely it can store 8.7 with a degree of accuracy needed to compare itself with another 8.7 value?
你想错了。 1/3 很小,但没有有限位数的精确十进制表示。一个值的大小与它能否用特定基数中的有限位数精确表示无关。
My only worry is that its actually being represented somewhere in memory as eg 8.70000003758 or something, which is throwing my comparisons off?
完全正确,就像 0.333333333
一样表示 1/3。
I tried to printf with %.20f to see any further numbers that might be hiding but I think that just created numbers that were otherwise not there as the whole accuracy of the number changed to 8.6918734634834929 or something similar.
这可能只是一个错误。向我们展示该代码。也许你试图输出一个双精度而遗漏了 l
.
我正在比较 C 中的简单浮点数和双精度数,特别是两者的值 8.7。现在我为每个变量分配 8.7,当我打印时,我得到两个值的结果都是 8.7000。为什么编译器添加了这些零。我想问的主要问题是,是否还有其他我没有看到的数字,例如隐藏在尾随零之后的数字。我读到我不应该因为缺乏精度而与 float 进行这样的比较,但我认为这么小的值肯定可以存储 8.7 并具有将自身与另一个 8.7 值进行比较所需的精度?
我唯一担心的是它实际上在内存中的某个地方表示为 8.70000003758 或其他东西,这让我的比较失败了?我尝试使用 %.20f 进行 printf 以查看可能隐藏的任何其他数字,但我认为这只是创建了本来不存在的数字,因为数字的整体精度更改为 8.6918734634834929 或类似的东西。
I'm comparing simple floats and doubles in C, specifically the value 8.7 for both of them.
错误的选择,因为 8.7 没有精确的二进制表示。
Now I assign 8.7 to each variable, when I print I get a result of 8.7000 for both values. Why has the compiler added these zeros.
没有,你的打印例程有。
And the main question I wanted to ask was is there any further numbers that I'm not seeing, as in hidden after the trailing zeros.
当然,因为 8.7 没有精确的二进制表示。 (试着写成2的整数次幂之和,不行。)
I read that I shouldn't do comparisons like this with float because of a lack of precision, but I thought with such a small value surely it can store 8.7 with a degree of accuracy needed to compare itself with another 8.7 value?
你想错了。 1/3 很小,但没有有限位数的精确十进制表示。一个值的大小与它能否用特定基数中的有限位数精确表示无关。
My only worry is that its actually being represented somewhere in memory as eg 8.70000003758 or something, which is throwing my comparisons off?
完全正确,就像 0.333333333
一样表示 1/3。
I tried to printf with %.20f to see any further numbers that might be hiding but I think that just created numbers that were otherwise not there as the whole accuracy of the number changed to 8.6918734634834929 or something similar.
这可能只是一个错误。向我们展示该代码。也许你试图输出一个双精度而遗漏了 l
.