Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to compare Double and Int?

The following code in C# doesn't work:

int iValue = 0;
double dValue = 0.0;

bool isEqual = iValue.Equals(dValue);

So, the question: what's the best way to compare Double and Int?

like image 240
Murat from Daminion Software Avatar asked Oct 30 '09 14:10

Murat from Daminion Software


People also ask

How do you compare int and double?

Double compare() Method in Java with Examples The compare() method of Double Class is a built-in method in Java that compares the two specified double values. The sign of the integer value returned is the same as that of the integer that would be returned by the function call.

Can you compare double integers?

All int s are exactly representable as double .

Can we compare int and double in C?

Sure we can. C will automatically convert the int into a double, and is transparent for the programmer.

Can you use == to compare doubles?

Using the == Operator As a result, we can't have an exact representation of most double values in our computers. They must be rounded to be saved. In that case, comparing both values with the == operator would produce a wrong result.


3 Answers

You really can't compare floating point and integral values in a naive way; particularly, since there's the classic floating point representation challenges. What you can do is subtract one from the other and see if the difference between them is less than some precision you care about, like so:

int iValue = 0;
double dValue = 0.0;

var diff = Math.Abs(dvalue - iValue);
if( diff < 0.0000001 ) // need some min threshold to compare floating points
   return true; // items equal

You really have to define for yourself what equality means to you. For example, you may want a floating point value to round towards the nearest integer, so that 3.999999981 will be "equal" to 4. Or you may want to truncate the value, so it would effectively be 3. It all depends on what you're trying to achieve.

EDIT: Note that i chose 0.0000001 as an example threshold value ... you need to decide for yourself what precision is sufficient for comparison. Just realize you need to be within the normal representational bounds of double which I believe is defined as Double.Epsilon.

like image 175
LBushkin Avatar answered Oct 06 '22 00:10

LBushkin


This really depends on what you consider "equal". If you want your comparison to return true if and only if the double precisely matches the integer value (i.e. has no fractional component), you should cast your int to a double to do the comparison:

bool isEqual = (double)iValue == dValue;

If something like 1.1 would be considered equal to 1, you can either cast the double to an int (if you want to ignore the fractional component altogether) or round the double if you want say 1.9 to equal 2.

like image 30
Ryan Brunner Avatar answered Oct 05 '22 23:10

Ryan Brunner


It's an exceedingly bad idea to compare integers and floating-point numbers for equality in any language. It works for very simple cases, but after you do any math at all, the likliehood of the program doing what you want it to decreases dramatically.

It has to do with the way floating-point numbers are stored on a binary, digital system.

If you are very sure you want to use this, create a class to make you own number with fractions. use one int to maintain the whole number, and another int to maintain the fraction.

like image 25
San Jacinto Avatar answered Oct 06 '22 00:10

San Jacinto