odd numbers

Reply

Join Date: Oct 2007
Posts: 60
Reputation: Grub is an unknown quantity at this point 
Solved Threads: 0
Grub Grub is offline Offline
Junior Poster in Training

odd numbers

 
0
  #1
Oct 30th, 2007
Hi I am making an vending machine application. Now the vending Machine takes 5, 10, 20, 50 pence and 1 pound coins. Now I create a String object and use this to read input from the user i.e. read the denomination (Money e.g. 5 or 10 pence) the user enters. Now I parse the string to an double. The user selects an item from the machine which then asks the user to enter coins to complete the purchase. When the user adds 5 pence coins for an item costing 45 pence, a loop is activated to repeat the method in which the coins are to be inserted until the amount entered = or is greater than the price of the snack selected. By the time it gets to 35 pence everything is alright however after adding another 5 pence the number becomes strange

Here is a copy of what happens


Insert Coins : - .05
0.05

Insert Coins : - .05
0.1

Insert Coins : - .05
0.15000000000000002

Insert Coins : - .05
0.2

Insert Coins : - .05
0.25

Insert Coins : - .05
0.3

Insert Coins : - .05
0.35

Insert Coins : - .05
0.39999999999999997

Insert Coins : - .05
0.44999999999999996

Insert Coins : - .05
0.49999999999999994

As such it requires me to enter more money than is necessary.
May you help explain this and possible suggest a solution.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,190
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 483
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: odd numbers

 
0
  #2
Oct 30th, 2007
Please post your code
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: odd numbers

 
0
  #3
Oct 30th, 2007
This is natural behavior for floating point numbers. They are a limited precision representation of a decimal value and you should not expect them to be an exact result. This is why they should not be used for financial calculations which require perfect precision.
Last edited by Ezzaral; Oct 30th, 2007 at 1:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

Re: odd numbers

 
0
  #4
Oct 30th, 2007
You should stick to integers and work with them in terms of cents. So 1 = 1 cent rather than 1 dollar in your example. Calculations could also be unnoticably faster as well.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: odd numbers

 
0
  #5
Oct 31st, 2007
Originally Posted by Phaelax View Post
You should stick to integers and work with them in terms of cents. So 1 = 1 cent rather than 1 dollar in your example. Calculations could also be unnoticably faster as well.

That is a good idea. Floating points normally behave like that, you have explanation on Ezzarals' last post.

Basically used in one format, either in cents or dolors.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC