problem with relational operators

This is a discussion on problem with relational operators within the C++ Programming forums, part of the General Programming Boards category; When I run this program everything works fine except I cannot enter 0.016 for thickness. Can anyone give me some ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    5

    problem with relational operators

    When I run this program everything works fine except I cannot enter 0.016 for thickness. Can anyone give me some help on what the problem might be?

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main ()
    { 
        float length;
        float height;
        float thickness;
        float weight;
        cout.setf(ios::fixed, ios::floatfield);
        cout.setf(ios::showpoint);
        cout << setprecision(2);
        cout << "Please enter the package length(inches): ";
        cin >> length;
        cout << "\nHeight(inches): ";
        cin >> height;
        cout << "\nThickness(inches): ";
        cin >> thickness;
        cout << "\nWeight(pounds): ";
        cin >> weight;
        if (length >= 3.5 && length <= 4.25)
        {
                   if (height >= 3.5 && height <= 6)
                   {
                              if (thickness >= 0.007 && thickness <= 0.016)
                              {
                                            cout << "\nYour post card is a regular post card. ";
                                            cout << "The cost to mail your post card is $" << ((weight / 0.0625) * 0.2)
                                                 << ".";
                              } 
                   }
         }
         else if (length > 4.25 && length < 6)
         {
                   if (height > 6 && height < 11.5)
                   {
                              if (thickness >= 0.007 && thickness <= 0.016)
                              {
                                            cout << "\nYour post card is a large post card. ";
                                            cout << "The cost to mail your post card is $" << ((weight / 0.0625) * 0.3)
                                                 << ".";
                              } 
                   }
         }
        return 0;
    }
    Last edited by manoncampus; 12-12-2005 at 08:07 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    if (thickness >= 0.007 && thickness <= 0.016)
    That's kind of dangerous to do with floating point numbers. The number "0.016" that you enter might be stored as "0.016000102", making that expression false.

    You might want to round the number, or compare it to 0.0164 or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    but the thing is, I will need to use 0.016 specifically because the specifications for an envelope thickness will be between .016 and .25(not inclusive). I havn't done this part yet because I want to get past this first. Also, if I enter .007 it works fine.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Then round the number:
    Code:
    #include <iostream>
    
    int main(void) {
        double unrounded = .016004;
        double rounded = (double)((int)((unrounded*1000)+.5)/1000.0);
    
        std::cout << unrounded << std::endl << rounded << std::endl;
    
        return 0;
    }
    That should output
    Code:
    0.016400
    0.016000
    although I don't have a compiler here and I can't tell you for sure.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Also, if I enter .007 it works fine.
    Perhaps your .007 is being stored as .007001004, making the expression true.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    You might want to do some research on how Floats are stored in the machine. To put it simply, floats aren't exactly as precise as you might think.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Especially where == and != are concerned. This code might not output anything, depending on SOME_CONSTANT:
    Code:
    double x = SOME_CONSTANT;
    
    if(x == SOME_CONSTANT) {
        std::cout << "equal\n";
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Newbie
    Join Date
    Dec 2005
    Posts
    22
    I pluged this in and it work:

    Code:
    if (thickness >= 0.007 && thickness <= (((0.016 * 1000) + .5) / 1000))
    you're good dwks
    Last edited by tomahawker; 12-12-2005 at 06:51 PM.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Store everything internally as 1000th's of an inch, then all your comparisons become integer comparisons.

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    5
    Quote Originally Posted by dwks
    Then round the number:
    Code:
    #include <iostream>
    
    int main(void) {
        double unrounded = .016004;
        double rounded = (double)((int)((unrounded*1000)+.5)/1000.0);
    
        std::cout << unrounded << std::endl << rounded << std::endl;
    
        return 0;
    }
    That should output
    Code:
    0.016400
    0.016000
    although I don't have a compiler here and I can't tell you for sure.
    Weather I round it or not it just automatically rounds up to .02. Do you think it could be a problem with the compiler that I'm using?

  11. #11
    Registered User
    Join Date
    Sep 2004
    Posts
    197
    no, its not the compiler at all, its simply how floating point numbers are stored in binary. Seriously, do some research on the format, and try the conversion for yourself.

    EDIT: Did a quick search for you
    http://en.wikipedia.org/wiki/IEEE_fl...point_standard
    This might give you some insight
    http://en.wikipedia.org/wiki/Binary_...system#Decimal
    Check that link on how to convert from decimal into binary
    Last edited by Xipher; 12-13-2005 at 06:21 PM.
    If any part of my post is incorrect, please correct me.

    This post is not guarantied to be correct, and is not to be taken as a matter of fact, but of opinion or a guess, unless otherwise noted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  2. Replies: 5
    Last Post: 11-07-2005, 10:34 PM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Relational and Logical Operators
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 03-03-2002, 03:33 PM

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