Jump to content
  • Advertisement
Sign in to follow this  
Toadhead

[c++] Basic divide program, can't find the mistake

This topic is 3493 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Basic "enter an amount and it will divide the amount into fiftys, tens, fives and ones" program written in C++ as learning excersise. When you enter 0.01 or 0.02 it works fine, but if you add a number higher than 0.02 it will give problems. I just don't know what I'm doing wrong in the program. I added the "Added X, Left: X" because I was hoping this would give me any clue but no help there.
#include <iostream>

int main(int argc, char* argv[])
{

	double amount, total_amount;
	int one_e = 0, five_e = 0, ten_e = 0, fifty_e = 0;

	std::cout << "Please enter an amount: ";
	std::cin >> amount;
	total_amount = amount;

	std::cout << "\n\n\nTotal amount: " << total_amount;

	while(true)
	{
		if (amount < 0.01)
		break;

		if(amount >= 0.5)
		{amount -= 0.5; fifty_e++;std::cout << "\nAdded fifty. Left: " << amount ; continue;}

		if(amount >= 0.1)
		{amount -= 0.1; ten_e++;std::cout << "\nAdded ten. Left: " << amount ; continue;}

		if(amount >= 0.05)
		{amount -= 0.05; five_e++;std::cout << "\nAdded five. Left: " << amount ; continue;}

		if(amount >= 0.01)
		{amount -= 0.01; one_e++;std::cout << "\nAdded one. Left: " << amount ; continue;}

	}
	

	std::cout << "\n\nFifty: " << fifty_e;
	std::cout << "\nTen: " << ten_e;
	std::cout << "\nFive: " << five_e;
	std::cout << "\nOne: " << one_e;


	/*** Just to make sure the cmd window doesnt close instantly ***/
	int program_pause;
	std::cin >> program_pause;

	return 0; // Stops the program
}



Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by SiCrane
Define "give problems".


Ow I'm sorry.

If you enter 0.01 it works fine
If you enter 0.02 it works fine
If you enter 0.03 it gives twice the one (0.01) instead of three times so the total value is 0.02 instead of the 0.03.
If you enter something like 2.60 it gives 2.54 in total value if you combine all the fifty's and tens etc instead of 2.60

Share this post


Link to post
Share on other sites
I just tried to debug it using Visual Studio. The problem seems to be floating point inaccuracy. When you enter 0.7 as input for example, the value of amount is slightly less than 0.7 which causes some problems. Subtracting some very small value from the values you compare with should solve the problem.

Share this post


Link to post
Share on other sites
Quote:
Original post by johand_
I just tried to debug it using Visual Studio. The problem seems to be floating point inaccuracy. When you enter 0.7 as input for example, the value of amount is slightly less than 0.7 which causes some problems. Subtracting some very small value from the values you compare with should solve the problem.


Thanks for your aswer.

I don't realy understand where the floating point inaccuracy is caused from, but you are right in the sense that is does work if I enter 0.031 for example.

Still, I find it very strange that I get these problems. Are floating numbers always slightly inaccurate? If so, why using them at all?

And one last question, if there a better way to program this application (in the way that it gives the same result) ?



Share this post


Link to post
Share on other sites
Quote:

Are floating numbers always slightly inaccurate?


Yep. For a very detailed description of the problem see:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

In your particular case SiCrane's solution might be somewhat more elegant (no need to subtract an epsilon value when your working with integers).

Share this post


Link to post
Share on other sites
Oke I did some more research myself on floats.
It is clear to me now for a big part, thanks everyone for the help.

Kind regards,
Rob

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!