比较双精度值 [英] Comparing double values
问题描述
我有一个双重的值i动态获取和值是0.94289988675,也是我收到了另一双,其值是0.94289988777。我只需要这两个值进行比较。但它说,他们是不一样的。我如何比较,直到这些值的前四位数字。
code
为(Z = 0; z,其中,something.Count(); Z ++)
{
如果(最大== Math.Round(0.94289988675 [Z],4))
{
//继续
}
}
=最大0.94289988675
0.94289988675 [Z]来0.94289988777。 z是。
它不会在循环去。请帮助我。
我如何比较,直到前四位
块引用>我假设你的意思是4位数,而不是4位。
只是走差异的绝对值:
如果(Math.Abs(最大 - 0.94289988675)0.0001)
{
//继续
}平等是pretty难以理解的概念与浮点类型,千万不要用简单的
==
I have a double whose value i get dynamically and the value is 0.94289988675 and also i get a another double whose value is 0.94289988777 .I just need to compare the two values. But it says they are not same. How can i compare till first four digits of those values.
Code:
for (z = 0; z < something.Count(); z++) { if (largest == Math.Round(0.94289988675 [z],4)) { //proceed } }
largest = 0.94289988675 0.94289988675[z] comes 0.94289988777. z is.
It does not go in the loop. Please help me out.
解决方案How can i compare till first four bits
I assume you mean 4 digits, not 4 bits.
Just take the Absolute value of the difference:
if (Math.Abs(largest - 0.94289988675) < 0.0001) { //proceed }
Equality is a pretty difficult concept with floating point types, never use the simple
==
这篇关于比较双精度值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!