Sign in to follow this  

Calculation problems with floating / double data types.

Recommended Posts

	double f = 3.14159;
	float g = 3.14159;
	cout << fixed << showpoint;
	cout << f << endl;
	cout << g << endl;
This causes problems down the road because calculations don't give the right answers. Anyway to fix this? I'm working with currencies so I tried rounding it off with:
But that doesn't help.

Share this post

Link to post
Share on other sites
float and double both have a limited amount of bits available to represent a number. The precision and/or the magnitude will be limited earlier or later.

If you need a guaranteed precision down do a defined number of decimal places then use fixed point math instead. If you want (nearly) arbitrary precision then use a sizeable array of bytes where each byte represents a digit (there are libs for this purpose already out there; unfortunately I haven't any name at hand).

Share this post

Link to post
Share on other sites
What Every Computer Scientist Should Know About Floating-Point is a must read. Googling for the title yields 22,400 results here - that's a rough measure of its value.

Quick answer: The floating point standard used on the x86 is IEEE754. Basically it comes down to the fact that you can't express infinite precision in a finite number of bits - and so some precision is lost, with a few guarantees as made in that standard (if you're in the mood for some fairly technical reading, it's worth looking it up). If you really need very high precision, you'll need to either switch to fixed point arithmetic or write your own library for it.

EDIT: Beaten, but that link is still worth reading.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this