Hello, I am trying to compare a double to be within various ranges, but the comparison is not working correctly. Rounding precision is not a concern, because if the value will match the boundaries so rarely, and it is rounding up to the higher range is acceptable. Below is the code at issue:

if (kq == 2)

if (fundt[kq-1][ky-1][2-1] < 450,000,000.00) ktti[ky-1][0] = 1;
else if ((fundt[kq-1][ky-1][2-1] >= 450,000,000.00) && (fundt[kq-1][ky-1][2-1] < 525,000,000.00)) ktti[ky-1][0] = 2;
else if ((fundt[kq-1][ky-1][2-1] >= 525,000,000.00) && (fundt[kq-1][ky-1][2-1] < 650,000,000.00)) ktti[ky-1][0] = 3;
else if ((fundt[kq-1][ky-1][2-1] >= 650,000,000.00) && (fundt[kq-1][ky-1][2-1] < 750,000,000.00)) ktti[ky-1][0] = 4;
else if ((fundt[kq-1][ky-1][2-1] >= 750,000,000.00) && (fundt[kq-1][ky-1][2-1] < 850,000,000.00)) ktti[ky-1][0] = 5;
else ktti[ky-1][0] = 6;
printf("DEBUG9: The Quarter is %d, Trust Fund is %20.3f, the Tax Table is %d\n", kq, fundt[kq-1][ky-1][2-1], ktti[ky-1][0]);

else if (kq==4)

if (fundt[kq-1][ky-1][2-1] < 450,000,000.00) ktti[ky-1][1] = 1;
else if ((fundt[kq-1][ky-1][2-1] >= 450,000,000.00) && (fundt[kq-1][ky-1][2-1] < 525,000,000.00)) ktti[ky-1][1] = 2;
else if ((fundt[kq-1][ky-1][2-1] >= 525,000,000.00) && (fundt[kq-1][ky-1][2-1] < 650,000,000.00)) ktti[ky-1][1] = 3;
else if ((fundt[kq-1][ky-1][2-1] >= 650,000,000.00) && (fundt[kq-1][ky-1][2-1] < 750,000,000.00)) ktti[ky-1][1] = 4;
else if ((fundt[kq-1][ky-1][2-1] >= 750,000,000.00) && (fundt[kq-1][ky-1][2-1] < 850,000,000.00)) ktti[ky-1][1] = 5;
else ktti[ky-1][1] = 6;
printf("DEBUG10: The Quarter is %d, Trust Fund is %20.3f, the Tax Table is %d\n", kq, fundt[kq-1][ky-1][2-1], ktti[ky-1][1]);

else;

I know that a triple-dimension array looks complex, but I can guarantee that is a double. The literal constants I am using should automatically be doubles. The subscript of [2-1] looks odd, but this is code I was given to maintain, and that is how it is written elsewhere, so I kept it for consistency. The problem I am having is that when I run this code, all the data I input is less than the 450 million value, but the run falls through all the if and if-else conditions, and the else code is what is actually executed, i.e., I always get six as my result. I am running on a Sun Sparc, and the compiler used is SunStudio 12. I have tried using variables, with the values listed above assigned to each variable, but it does work either. When I use variables, if the input value is negative, the comparison for less than 450 million works, but any positive input values will through to the else and give me six. Thank you for any help you can give me with this.

Recommended Answers

All 3 Replies

Well, one thing that stands out immediately is C isn't so pretty that it allows commas in a large number. In fact, if you're exceptionally unlucky (which it seems you are), including them will be syntactically correct but semantically bonkers.

commented: aaaand how did I miss that? +10

Side note - please indent code so it reads better, e.g.:

if (fundt[kq-1][ky-1][2-1] < 450,000,000.00) ktti[ky-1][0] = 1;

First thing I notice is you're indexing the array every single time you check the value... it seems pretty clear you don't expect the value to change, so it will be much easier to read if you index it once and stuff it into a variable, like this:

if (value < 450,000,000.00) ktti[ky-1][0] = 1;
else if ((value >= 450,000,000.00) && (value < 525,000,000.00)) ktti[ky-1][0] = 2;
else if ((value >= 525,000,000.00) && (value < 650,000,000.00)) ktti[ky-1][0] = 3;
...

Also, in the if/else if blocks, you don't need to test the low value in the else ifs; you've already tested for that in the previous line. Eliminate those tests, and it'll look more like this:

if (value < 450,000,000.00) ktti[ky-1][0] = 1;
else if (value < 525,000,000.00) ktti[ky-1][0] = 2;
else if (value < 650,000,000.00) ktti[ky-1][0] = 3;

Make those changes, and see if your problem becomes more apparent.

Thank you your help. The commas were the problem, once I removed them, everything worked fine. I should have know better than to inclde them, but I am surprised the complier allowed them.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.