Comparing values

This is a discussion on Comparing values within the C Programming forums, part of the General Programming Boards category; How would I do this : Code: #include <stdio.h> FILE * output = stdout; FILE * input = stdin; int ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    104

    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;
    }
    }

  2. #2
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    478
    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.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ICool View Post
    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
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    • 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;
    }
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    99
    In addition to the above information, do NOT use == to compare floating point values. You will be in for a world of hurt.

  6. #6
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by QuestionC View Post
    C doesn't handle a < b < c.
    Well, it does. Just not the way that you would expect.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  8. #8
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by Prelude View Post
    >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
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >"Just not the way that you would *probably* expect as a novice programmer"
    Works for me.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  2. putting values into an array
    By zdream8 in forum C Programming
    Replies: 15
    Last Post: 05-21-2008, 11:18 PM
  3. Replies: 1
    Last Post: 12-30-2007, 09:08 AM
  4. Sending values to a control
    By Zyk0tiK in forum C Programming
    Replies: 6
    Last Post: 12-02-2005, 05:29 PM
  5. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21