problems with float and int variables

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • earlbooms
    New Member
    • Nov 2007
    • 4

    #1

    problems with float and int variables

    My assignment says to make a program to calculate the amount of change needed to add up to an input amount of money. Doesn't sound too hard, but there's problems somewhere. I initially tried using the modulus division (%), but it didn't give me correct results. I then tried receiving the input as a float, multiply by 100, and converting to a integer, but it gives strange results also. Through trial and error I figured out that when a number ends in >5, the actual number the program sees isn't what was input. Like, if i put in 1.38, it sees 1.379996, so when I change to an integer it cuts off the 9's and I'm always one off. Any ideas?
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Floating point representation comes with its hazards. It's not possible in base 2 to represent all decimal numbers precisely.

    My recommendation? Make your units in cents, not dollars. You never deal with a fraction of a cent, so you won't have to worry about floating point.

    Comment

    • earlbooms
      New Member
      • Nov 2007
      • 4

      #3
      so there's no way to convert dollars to cents?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by earlbooms
        so there's no way to convert dollars to cents?
        Are you saying that if you have a dollar that you don't know how many cents you have??

        Comment

        • earlbooms
          New Member
          • Nov 2007
          • 4

          #5
          no, of course I know how, but it seems like there should be a way to do this within the program.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Just code the steps you follow in real life in C++- and you are done.

            Comment

            • earlbooms
              New Member
              • Nov 2007
              • 4

              #7
              i tried that...refer to initial post. I scanf a float (ie 1.38), multiply by 100, and store the answer as an integer. Should give me 138, but it rounds to 137, because it actually gets stored as 1.379996. Is there anyway to fix this, or am I stuck with asking for cents instead of actual money?

              Comment

              • oler1s
                Recognized Expert Contributor
                • Aug 2007
                • 671

                #8
                Changing from float to double (make sure you use scanf specifier lf) might help. For true precision, you need to implement arbitrary precision arithmetic (there's third party libraries out there for this).

                Comment

                • MarshMallow
                  New Member
                  • Nov 2007
                  • 52

                  #9
                  My suggestion is to have a string as input(e.g. thorugh char *argv[]);then to parse the string to determine properly all the units you need,e.g. dollars and cents;
                  something like this
                  user inputs 1.34
                  take what comes before the dot and convert it to an int,so you get "dollars"
                  take what comes after the dot and convert it to an int,so you get "cents"
                  never rely on dobule precision arithmetic when simple integer computations are needed.Good Luck!

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by earlbooms
                    I scanf a float (ie 1.38), multiply by 100, and store the answer as an integer. Should give me 138, but it rounds to 137, because it actually gets stored as 1.379996. Is there anyway to fix this, or am I stuck with asking for cents instead of actual money?
                    A float is floating point. Floating point rounds. If you don't want rounding, then don't use floating point. Because of this, you cannot write accounting applications using floating point. The rounding screws up the account balances. In some places there are actual laws against this.

                    Personally, I would use an integer and keep everything in pennies. You can always display 150 as $1.50.

                    That decimal point is so enticing that beginners can't resist using floating point as a way to avoid writing code they have to think about. Then they spend hours with rounding issues, number of decimal places issues, etc.

                    You can avoid all of this by using integers.

                    Comment

                    Working...