Sign in to follow this  

Calculation problems with floating / double data types.

This topic is 3959 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

Simply:
	double f = 3.14159;
	float g = 3.14159;
	cout.precision(30);
	cout << fixed << showpoint;
	
	cout << f << endl;
	cout << g << endl;
Outputs:
3.141589999999999882000000000
3.141590118408203125000000000
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:
	num*=100;
	num+=0.9;
	num=floor(num);
	num/=100;
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
Thanks for the help. I only need accuracy to the 2nd decimal point so I decided to implement fixed point math by *=100 and storing the data as ints.

Share this post


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