473,326 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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?
Nov 28 '07 #1
9 2032
oler1s
671 Expert 512MB
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.
Nov 28 '07 #2
so there's no way to convert dollars to cents?
Nov 28 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
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??
Nov 28 '07 #4
no, of course I know how, but it seems like there should be a way to do this within the program.
Nov 28 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
Just code the steps you follow in real life in C++- and you are done.
Nov 28 '07 #6
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?
Nov 28 '07 #7
oler1s
671 Expert 512MB
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).
Nov 29 '07 #8
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!
Nov 29 '07 #9
weaknessforcats
9,208 Expert Mod 8TB
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.
Nov 29 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Justin Lemkul | last post by:
Hello all, I am hoping someone out there will be able to help me. I am trying to install a program that utilizes NumPy. In installing NumPy, I realized that I was lacking Atlas. I ran into...
3
by: eternalD3 | last post by:
Hi, I have a problem to get this working on Opera 7.x+. This does not need to work on older Opera browsers There are problems on rendering the sub-level navigation. It aligns right on Firefox...
43
by: J.K. Becker | last post by:
Hi there, I am trying to multiply doubles with floats (actually I tried every possible combination by now) and it never works (well, it does something but it is always wrong). I have no idea...
16
by: chandanlinster | last post by:
As far as I know floating point variables, that are declared as float follow IEEE format representation (which is 32-bit in size). But chapter1-page no 9 of the book "The C programming language"...
2
by: monkeon | last post by:
Hi, I'm pretty new to C++ I've been doing a course for a few months. I've been trying to design a simple console application that reads in floats from a text file and returns the average,...
13
by: Shirsoft | last post by:
I have a 32 bit intel and 64 bit AMD machine. There is a rounding error in the 8th digit. Unfortunately because of the algorithm we use, the errors percolate into higher digits. C++ code is...
10
by: Cliff | last post by:
Greetings, I have been trying to teach myself C++ over the past few weeks and have finally came across a problem I could not fix. I made a simple program that prints out a square or rectangle...
13
by: orobalage | last post by:
Hi! I was developing some number-crunching algorithms for my university, and I put the processor into a class. While testing, I found a quite *severe performance problem* when the object was...
5
by: SpennH | last post by:
I have had some basic knowledge of java through flash 8. I also use a freeware 3d software called blender. And it uses python for its game engine. So I have decided to pick up some python. This is...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.