Comparison of double with int value

May 26, 2011 at 8:21am
I have function like this

void MyLocationClass::setLocation(QString lat, QString lon)
{
double mLat = lat.toDobule();
double mLon = lon.toDouble();

if (mLat==0 && mLon==0) {
qDebug("Invalid Position");
} else {
qDebug("Valid Position");
}
}

Suppose I call setLocation funtion like this
myOjbect->setLocation(0.00, 0.00)

which block of if will be executed ?

Note:
I am working on C++ for Qt Application. QString and qDebug() is work same as Starndard C++ String and cout.


May 26, 2011 at 12:01pm
Well, 0.00 is not a string, so this shouldn't compile.. (unless there is a QString(double) constructor,b ut I don't think there is..)

As for comparisons, you can compare them, but when dealing with doubles there are sometimes errors in calculations when instead of 0 you can get 0.000...001 and == will return false.
A thing you can do is change a==b to abs(a-b) < epsilon where epsilon is a really small number.
May 26, 2011 at 12:55pm
Thanks for the reply...
My mistake was there... Actually I need to call setLocation() function like this

myObject->setLocation("0.00", "0.00");
Topic archived. No new replies allowed.