本文介绍了为什么这个减法不等于零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ColdFusion代码中发生了这些值,但Google计算器似乎有相同的错误,差异是非零的。



416582.2850 - 411476.8100 - 5105.475 = -2.36468622461E-011





Java将这些拖放到long / float / double不起作用 - 它导致其他非零的差异。

解决方案

在基础10中,在基础2(这是计算机用来表示浮点数)是不完全可表示的。请参阅文章每个计算机科学家应该了解的浮点运算有关此问题和解决方法的详细说明。


I happened upon these values in my ColdFusion code but the Google calculator seems to have the same "bug" where the difference is non-zero.

416582.2850 - 411476.8100 - 5105.475 = -2.36468622461E-011

http://www.google.com/search?hl=en&rlz=1C1GGLS_enUS340US340&q=416582.2850+-+411476.8100+-+5105.475&aq=f&oq=&aqi=

JavaCast'ing these to long/float/double doesn't help- it results in other non-zero differences.

解决方案

This is because decimal numbers that "look" round in base 10, are not exactly representable in base 2 (which is what computers use to represent floating point numbers). Please see the article What Every Computer Scientist Should Know About Floating-Point Arithmetic for a detailed explanation of this problem and workarounds.

这篇关于为什么这个减法不等于零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 11:16