views:

382

answers:

4

I need to compare the integer part of two doubles for inequality and I'm currently doing this:

int iA = (int)dA;
int iB = (int)dB;

if( iA != iB )
{
    ...
}

but I wonder if there's a better approach than this.

Thanks.

If I used Math.Truncate() instead of a cast to int, would it still be accurate to compare the two resulting double values for equality?

About the hungarian notation comments:

I never use HN myself, not at least in the way most of people do. But this is one of these rare cases where the semantic of a variable directly relates to its type. I could have chosen another syntax like A_As_Integer and B_As_NonInteger but what would have been the difference then?

+4  A: 

Yes, that is best approach.

Assuming from your wording in you question you arent worried about rounding... just the integerr part itself.

ie. (int)4.1 will give the same result as (int)4.9

As Ovid stated, if you only need the variables for the comparison, then you will only need to cast the doubles in the comparison statement. I was looking only at the theory.

JTA
Question: what does (int)-4.1 go to?
biozinc
It will equal -4
JTA
+1  A: 

Personally, I try to avoid creating extra variables unless I need them.

if ( (int)dA != (int)dB ) {
    ...
}

As code evolves over time, having extra variables hanging around for no purpose leads to confusion. Of course, if you need those variables, that's another issue :)

Side note: you appear to be trying to hint at the data type via a simple Hungarian notation. May I recommend not doing that? If you must prefix information to your variables, try to prefix what the variable is for, rather than its type. If you do that, mistakes in code can be easier to see:

if ( (int)ageA != (int)idB ) {
    ...
}

In this case, without even knowing what the data is, seeing that you're trying to compare an 'age' with an 'id' is a good clue that something is wrong here.

Ovid
+.5 for avoiding extra variables, and another .5 bonus points for mentioning the original intention of the hungarian notation (using semantic rather than type prefixes).
OregonGhost
+10 for OregonGhost!
DOK
-.5 for not answering the question, -.5 for bringing up an unrelated topic. :)
Trap
@Trap: the original poster was asking if there was a "better way to do this". Oddly enough, I answered that question :)
Ovid
+6  A: 

Use Math.Truncaet. i.e.

if (Math.Truncate(x) == Math.Truncate(y))

[Edit] Realised that if you are comparing integer parts of doubles, casting to int values first runs the risk of overflows should your doubles be outside the range that could be represented as int.

Truncate returns either a Decimal or a double, avoiding this issue.

biozinc
Both -2 and -2.1 both == -2 after the cast.
JTA
You are right. My bad
biozinc
On a positive note, I would argue that your method is more descriptive to what the code is doing.
JTA
i honestly hadn't thought of the double->int cast outside of the int bounds, cheers for pointing that out.
olliej
+1  A: 

I agree that Truncate is what you want.

Some helpful information from MSDN:

Truncate returns the number that remains after any fractional digits have been discarded.

It rounds to the nearest integer towards zero.

double floatNumber;

floatNumber = 32.7865;
// Displays 32      
Console.WriteLine(Math.Truncate(floatNumber));

floatNumber = -32.9012;
// Displays -32       
Console.WriteLine(Math.Truncate(floatNumber));
DOK