s2000coder
s2000coder

Reputation: 941

Comparing two double variables Objective-C

double a = 10.123420834;
double b = 100.123412321;

if (a > b) {
// do something here
}

I am trying to compare the two values, the code above doesn't seems to work. Any idea?

Upvotes: 0

Views: 3748

Answers (3)

PengOne
PengOne

Reputation: 48406

The code is correct.

Note that your snippet is equivalent to

float a = 10.123420834;
float b = 100.123412321;

if (a > b) {
// do something here
}

since Objective C uses double by default unless the number is followed by an f.

Also note that a < b, so the if statement will always evaluate to FALSE. Hence you may want to do

double a = 10.123420834;
double b = 100.123412321;

if (a > b) {
// do something here
} else {
// do something else here
}

to test this properly.

Upvotes: 4

user149341
user149341

Reputation:

The code in your example is correct. Your problem must be in the "do something here", or elsewhere.

Upvotes: 0

Dave DeLong
Dave DeLong

Reputation: 243156

double a = 10.123420834
double b = 100.123412321

You need to have a semicolon at the end of each of those lines.

Upvotes: 1

Related Questions