Comparing values

• 09-24-2007
ICool
Comparing values
How would I do this :
Code:

```#include <stdio.h>     FILE * output = stdout;     FILE * input = stdin; int main(void) { float exposure; fscanf(output,"%f",exposure); If ((exposure==0.000025)||(0<exposure<0.000025)) { fprintf(output,"Nearest Exposure values is 1/4000"); return 0; } else if ((exposure==0.0005)||(0.000025<exposure<0.0005)) { fprintf(output,"Nearest Exposure values is 1/2000"); return 0; } else if ((exposure==0.001)||(0.0005<exposure<0.001)) { fprintf(output,"Nearest Exposure values is 1/1000"); return 0; } else { printf("etc..."); return 0; } }```
• 09-24-2007
IceDane
There is a pattern.

You have min and max values to compare to.

Variables:

min = 0;
a = 4000;
max = 1 / a;

start loop
if min < exposure < max
then
do whatever

min = max;
a = a / 2; // a is now 2000;
max = 1 / a;

loop;

It's really not complicated. Stop focusing on how hard it is and start focusing on how you could achieve it.
• 09-24-2007
QuantumPete
Quote:

Originally Posted by ICool
if (0<exposure<0.000025)

that's probably not what you want to do! You want to compare them in seperate clauses, like so:
Code:

`if ((0 < exposure) && (exposure < 0.000025)) {...}`
otherwise the following will happen:
0<exposure will be evaluated to either 0 if false or 1 if true. The result is then compared to 0.000025.
Say exposure is 0.0000000001 then
0< 0.00000001 = true
1 < 0.000025 = false
expression becomes false!

say exposure is 0 then
0 < 0 = false
0< 0.000025 = true
expression becomes true!

As a rule of thumb, you should only compare two values within an expression.

QuantumPete
• 09-24-2007
QuestionC
• C doesn't handle a < b < c. Break it up.
• You are trying to fscanf output.

Code:

```#include <stdio.h> FILE * output = stdout; FILE * input = stdin; int main(void) {   float exposure;   fscanf(input,"%f",exposure);   if (exposure > 0 && exposure <= 0.000025)   {       fprintf(output,"Nearest Exposure values is 1/4000");       return 0;   }   return 0; }```
• 09-24-2007
rdrast
In addition to the above information, do NOT use == to compare floating point values. You will be in for a world of hurt.
• 09-24-2007
QuantumPete
Quote:

Originally Posted by QuestionC
C doesn't handle a < b < c.

Well, it does. Just not the way that you would expect.

QuantumPete
• 09-24-2007
Prelude
>Well, it does. Just not the way that you would expect.
It does exactly what I expect. Maybe it's more a matter of not knowing what to expect and just making up semantics as you go. ;)
• 09-24-2007
QuantumPete
Quote:

Originally Posted by Prelude
>Well, it does. Just not the way that you would expect.
It does exactly what I expect. Maybe it's more a matter of not knowing what to expect and just making up semantics as you go. ;)

:-D I'll revise my comment: "Just not the way that you would *probably* expect as a novice programmer".

QuantumPete
• 09-24-2007
Prelude
>"Just not the way that you would *probably* expect as a novice programmer"
Works for me. :)