449,422 Members | 1,309 Online
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

# quick help for weird problem.

4 Replies

 P: n/a "Jeffrey Barrett" wrote Can someone tell me why I'm having this problem: When I select drink 5 and deposit too little money, there's a problem with the 'difference' variable. Try to deposit \$.80 instead of the full \$.85 and see what happens if you then keep entering .01 until you have added the full .85. The program still asks the user to deposit \$0.00. It is telling me for some reason that 0.850000 - 0.850000 = 5.96046e-008. Any suggestions? The amount 0.01 has no exact binary representation. In effect you have a small random error whenever you calculate a multiple of such an amount, hence the tiny difference in your subtraction. The solution is to store the monetary value as an amount of pennies / cents, and then just convert to dollars immediately before output to the user. Nov 14 '05 #2

 P: n/a [snips] On Fri, 30 Jul 2004 15:50:32 -0700, Jeffrey Barrett wrote: Can someone tell me why I'm having this problem: When I select drink 5 and deposit too little money, there's a problem with the 'difference' variable. Try to deposit \$.80 instead of the full \$.85 and see what happens if you then keep entering .01 until you have added the full .85. The program still asks the user to deposit \$0.00. It is telling me for some reason that 0.850000 - 0.850000 = 5.96046e-008. Any suggestions? Yeah; don't use floats/doubles. What you're seeing, apparently, is the following: instead of storing 0.85 exactly (in at least one case) it's storing a value something like 0.8500000059604. Since the two aren't the same, any comparison that assumes they should be will fail. Simplest answer: use integer types and scale on I/O. That is, instead of storing 0.85 dollars, store 85 cents and, if you need to, display it as 0.85 dollars. Nov 14 '05 #3