Comparing double with int variables

Apr 11, 2012 at 9:47am
The idea here is to look up for some specific element inside an array using another array, a bit like this:

counter1=0; check==false;
for (i=counter2;i<5-counter2 && check==false;i++)
{
for (j=0;j<4 && check==false;j++)
{
if (array1[i+1]==array2[j] && array3[i+1]==820)
{
check==true;
}
}
counter1++;
}



array1 and array3 are 'double' type variables and array2 is integer type

counter2 starts off with 0
The values in array1 are:
0.00000000000000000
1.0000000000000000
5000.0000000000000
10000.000000000000
15000.000000000000

The values in array2 are:
0
5000
10000
15000

the values in array3 are all 820.00000000000000

Problem is, when i=1 and j=1, the 'if' expression should evaluate to true, but it doesn't and I just can't see anything wrong with it.

Any help would be much appreciated.
Apr 11, 2012 at 9:54am
Apr 11, 2012 at 11:15am
Thanks for your answer.

I should have precised that all values on those arrays have been set up manually with integer numbers and haven't been altered at all since then, so there shouldn't be any imprecision at all in that case, if I got it well.
Last edited on Apr 11, 2012 at 11:16am
Apr 11, 2012 at 11:25am
What does the following code give you?

1
2
3
4
5
6
7
8
9
10
11
double x = 5.000;
int y = 5;

if (y == x)
{
  cout << "Same";
}
else
{
  cout << "Different";
}


If they come out the same, then the problem isn't your comparison.
Apr 11, 2012 at 11:30am
'Same'.

I had tried a similar test before and obtained this same conclusion and this is actually what brought me here.

Still when I debug it, when i=1 and j=1 and therefore the 'if' should evaluate to true and run the check=true line, it just skips it.

Edit: Nevermind, I'm an idiot, I put 2 equal signs instead of one in that sentence...

Edit2: It's all good EssGeEich, thanks for answering anyway.
Last edited on Apr 11, 2012 at 11:53am
Apr 11, 2012 at 11:39am
You should do something like this for comparison:
1
2
3
4
5
6
7
8
9
10
11
double x = 5.000;
int y = 5;
if( abs(x-(double)y) < 0.01 ) // If the difference between x and y is less than 0.01...
// 0.01 is the value you want to use as a "Rounding Error"
{
    // They are almost equal
}
else
{
    // They aren't equal at all
}

And you can create a function like this:
1
2
3
4
5
6
7
bool isDoubleEqual(double x, int y, double RoundingError = 0.01)
{
    return ( abs(x - (double)y) < RoundingError);
    // What it actually does, is to take the difference between the numbers,
    // And return a positive-only number, so if difference is 0, 0.1 or -0.1,
    // it will return 0, 0.1 and 0.1. So you can check their difference in a faster way
}

And you can call it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
double x = 5.005;
int y = 5;
if(isDoubleEqual(x,y)) // You don't need to give a third value, it is a
// default value (see "Double RoundingError = 0.01", 0.01 is the default value)
{
    // Is Almost Equal
    // Will Be Executed
}
else
{
    // Is Not
}
// This will give you a "Almost Equal" result.
// But the following will not:
if(isDoubleEqual(x,y,0.001)) // x - y is more than 0.001,
// so they are NOT equal with this rounding error check
{
    // Is Almost Equal
}
else
{
    // Is not
    // Will Be Executed
}

EDIT: Whoops, didn't notice your post.
Last edited on Apr 11, 2012 at 1:17pm
Apr 11, 2012 at 4:54pm
@Moschops
Unfortunately, floating-point stuff doesn't work that way. The example you gave does not actually match the way that floating-point arithmetic works in the OPs example.

@EssGeEich bool isDoubleEqual(double x, double y, double RoundingError = 0.01)
An int will type-promote to a double, so this version will be more friendly in a lot of ways. Nice!

More reading about What Every Computer Scientist Should Know About Floating-Point Arithmetic
http://perso.ens-lyon.fr/jean-michel.muller/goldberg.pdf

Hope this helps.
Apr 11, 2012 at 4:58pm
Really? He's comparing a double val and an int val using the == operator. Why does that not match my example?
Apr 11, 2012 at 5:04pm
Read the link.
Apr 11, 2012 at 5:28pm
The link doesn't mention any difference (that I can see - I have not read it cover to cover) between dereferencing a double/int from arrays (OP's example) and comparing them, and just comparing double/int (my example).

I think my example does match the OPs.
Last edited on Apr 11, 2012 at 5:35pm
Apr 11, 2012 at 6:11pm
@Duoas , Well, I made the second parameter an Int so he will not get a Compiler Warning for Implicit Type Casting... But maybe you are "right-er" than me, because if he inverts the values, the function will have a different result.
Apr 11, 2012 at 6:51pm
The entire bottom half of the paper deals with it... but put simply, there are precision errors that aggregate when transferring the number around in the computer. What looks like one line of code can involve a number of FPU stack operations -- which can easily change your number in miniscule ways, and make simplistic comparisons fail.

Your example avoids any of that.
Apr 11, 2012 at 7:02pm
So, to every beginner, every time you need to have floating-point precision, use strings! lol
Apr 11, 2012 at 7:31pm
Thank you. I appreciate not having to read it; I'm not a computer scientist, I'm a hack (not hacker, just a hack; the coding equivalent of a hack journalist).
Apr 11, 2012 at 9:19pm
LOL, I just posted the link. When you said you didn't get anything out of it, I took another look and remembered that it was fairly technical. I'm sure there is a more friendly writeup somewhere, but I didn't care to google it ATM.

FP stuff has a surprising number of gotchas when you think you are dealing with normal numbers.
Topic archived. No new replies allowed.