Rules for Comparison

This is a discussion on Rules for Comparison within the C++ Programming forums, part of the General Programming Boards category; I'm trying to error check against being able to put in a value less than .3 and more than .4. ...

  1. #1
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26

    Question Rules for Comparison

    I'm trying to error check against being able to put in a value less than .3 and more than .4. I'm having some problems though. When I enter a value outside of this range, the program accepts it. Are the rules for comparisons different when dealing with decimal or have I done something else wrong?

    Thanks,
    Vireyda

    Code:
    		if(expenses[i].mileage_rate < 0.3 && expenses[i].mileage_rate > 0.4)
    			printf("\nThat mileage rate does not fall between $0.30 and $0.40.  Please try again.\n");

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You have your signs backwards...mileage_rate should be > than .3 and < .4

    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Actually, your signs are correct, but you should switch it to || instead of &&.

  4. #4
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    I've tried both suggestions, and I still can't get it to work for me. With the || instead of the &&, I end up in a never ending loop that rejects all input. With the reversed signs the program is still accepting everything. Any other suggestions?

    Here is my most recent section of code:
    Code:
    do
    		{
    		printf("Mileage Rate (between $0.30 and $0.40 per km):  ");
    		scanf("%f", &expenses[i].mileage_rate);
    
    		if(expenses[i].mileage_rate > 0.3 && expenses[i].mileage_rate < 0.4)
    			printf("\nThat mileage rate does not fall between $0.30 and $0.40.  Please try again.\n");
    
    		}while(expenses[i].mileage_rate > 0.3 && expenses[i].mileage_rate < 0.4);

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Eh my bad, I don't know what I was thinking...I believe jlou is correct, but I'm not sure what your problem is. Did you try this?
    Code:
    {
    		printf("Mileage Rate (between $0.30 and $0.40 per km):  ");
    		scanf("%f", &expenses[i].mileage_rate);
    
    		if(expenses[i].mileage_rate < 0.3 || expenses[i].mileage_rate > 0.4)
    			printf("\nThat mileage rate does not fall between $0.30 and $0.40.  Please try again.\n");
    
    		}while(expenses[i].mileage_rate < 0.3 || expenses[i].mileage_rate > 0.4);
    Although based on my previous answer you probably shouldn't listen to me...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    I should listen to you...Thank you. Your suggestion works perfectly.

    Vireyda

  7. #7
    Registered User Vireyda's Avatar
    Join Date
    Mar 2004
    Posts
    26
    actually, I've just done some testing of the program and I'm finding that it is excluding .4 when it should be included, but if I change the .4 to .41, the program starts including .4 and .41, when it should exclude .41. Any ideas?

    Vireyda

  8. #8
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You should show the code as you have it.
    Code:
    if(expenses[i].mileage_rate < 0.3 || expenses[i].mileage_rate > 0.4)
        printf("\nThat mileage rate does not fall between $0.30 and $0.40.  Please try again.\n");
    That code will not print the error message if the value is 0.4. I would assume that is what you want.

    There is another possible reason for it being off. The precision of floating point numbers isn't exact, so it is possible that if you do calculations (like 2.0 / 5.0) you will get something that is not exactly equal to 0.4 (like 0.40000001). That might mess up your comparisons. To fix that, use an epsilon value to check for small errors like that:
    Code:
    const double epsilon = 0.0001;
    if (0.3 - expenses[i].mileage_rate > epsilon || expenses[i].mileage_rate - 0.4 > epsilon)
        printf("\nThat mileage rate does not fall between $0.30 and $0.40.  Please try again.\n");
    Last edited by jlou; 04-13-2004 at 04:30 PM. Reason: Fixed typo - should be const double epsilon, not const int

  9. #9
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    actually, I've just done some testing of the program and I'm finding that it is excluding .4 when it should be included, but if I change the .4 to .41, the program starts including .4 and .41, when it should exclude .41. Any ideas?
    in addition to > and <, you also have >= and <= :P(i assume you knew about these.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  2. Replies: 6
    Last Post: 11-01-2008, 12:01 PM
  3. Pls let me know the rules in C
    By ice_breaekr in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-28-2005, 06:27 AM
  4. Addition to the forum rules
    By webmaster in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-08-2004, 04:31 PM
  5. Outlook: rules not on exchange --> but local --> but how?
    By gicio in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-31-2002, 11:47 AM

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