Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

iphoneGuy

macrumors member
Original poster
I am working on a new program that uses a lot of float values. However, I notice that if I set the value it will not be precise..

ie.

float val = 10.3

may show up in the debugger as 10.300002 or 10.299999993

How can I make these numbers alway return 10.3 exactly?

Thanks,
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Why's that?

What was I thinking? 3/16 = 0.1875. No big deal for a float in this case, eh?!

It's a problem further down in the precision department. Here's C++ example.

Code:
#include <iostream>
#include <iomanip>
using namespace std ; 

int main(void)
{
	float f ; 
	f = 10.000003 ; 
	cout.precision(12) ; 
	cout << "f=" << f ; 
	return 0 ;
}

output:
Code:
[Session started at 2008-03-30 18:00:48 -0500.]
f=10.000002861
exercise35 has exited with status 0.
 

CaptainZap

macrumors regular
Jan 17, 2007
170
0
Sorry iphoneGuy, I've kind of hijacked your thread...

So is there something else to use like double, or do programmers just get used to it?
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
So is there something else to use like double, or do programmers just get used to it?

Well, there are different ways to get around floating point rounding behavior.

Using this example, if you knew you needed 6 digits of non-rounded precision, you could multiply all your floats by 1,000,000, do all your calculations, and then divide by 1,000,000 for the final output.
Code:
#include <iostream>
#include <iomanip>
using namespace std ; 


int main(void)
{
	float f ; 
	f = 10000003.0 ; 
	cout.precision(12) ; 
	cout << "f=" << f/1000000.0 ; 
	return 0 ;
}
Output:
Code:
[Session started at 2008-03-30 18:43:23 -0500.]
f=10.000003
exercise35 has exited with status 0.

Following that logic, you could even use integers.

Working 3D point data, it's very common to have slight inaccuracies. In those cases, a lot of programs takes EPSILON into account. http://en.wikipedia.org/wiki/Machine_epsilon

Todd
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
Also, doubles have greater precision, but the rounding dilemma still exists.

The same program as above, using a double instead of float, does not round in this case.
Code:
#include <iostream>
#include <iomanip>
using namespace std ; 


int main(void)
{
	double d ; 
	d = 10.000003 ; 
	cout.precision(12) ; 
	cout << "d=" << d ; 
	return 0 ;
}

Output:
Code:
[Session started at 2008-03-30 18:48:07 -0500.]
d=10.000003
exercise35 has exited with status 0.
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
No problem.. I just wish there was a clean wat to represent 10.3 as exactly 10.3....

You will need to invent a computer that uses base-10 instead of base-2 for numerical representations.

There are discussions about this, but base-2 computers are so much easier to architect, that this will probably never come to be.
http://www.madsci.org/posts/archives/2001-10/1002423001.Cs.r.html
http://homepages.transy.edu/~jmiller/web706/chapt3.htm

You may also wish to take this up with the IEEE, as IEEE-754 defines this standard for storing floating point numbers in base-2.

As was mentioned earlier, if you are doing fixed precision arithmatic, using an int, long int, etc. and a fixed bias for your numbers might be a good way to go. We can exactly represent base-10 integers in base-2, so it's no problem (unless you need REALLY big integers...).

-Lee
 

pensfan

macrumors newbie
Feb 6, 2008
11
0
What you want is a "decimal" data type, if available . . . .NET languages have decimal, Java has BigDecimal I believe, I think Python has a decimal as well, etc. Just as toddburch and lee1210 suggested, the idea is that you multiply your floating point value by whatever scaling factor is necessary to represent it as an integer value, do your work on that integer value, and divide by the scale to get your resulting floating point value. Those decimal types implement that behavior so you don't have to manually do it.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
I am working on a new program that uses a lot of float values. However, I notice that if I set the value it will not be precise..

ie.

float val = 10.3

may show up in the debugger as 10.300002 or 10.299999993

How can I make these numbers alway return 10.3 exactly?

Thanks,

Hi,

I know it's annoying but to be honest, it's not a problem that most applications have to worry about it too much. Just be aware of doing things like this:-
Code:
float value ;
…
…
if ( value == 10.3f )
{
…
…
}
which would produce different results depending on how 'value' was calculated.

b e n

EDIT: Here's an example of what I mean by 'not worrying'. Calculators suffer from exactly the same problem but most people use them in blissful ignorance and trust their results.
 

iphoneGuy

macrumors member
Original poster
Hi,

I know it's annoying but to be honest, it's not a problem that most applications have to worry about it too much. Just be aware of doing things like this:-
Code:
float value ;
…
…
if ( value == 10.3f )
{
…
…
}
which would produce different results depending on how 'value' was calculated.

b e n

EDIT: Here's an example of what I mean by 'not worrying'. Calculators suffer from exactly the same problem but most people use them in blissful ignorance and trust their results.

thanks for the responses... I ended up switching to ints it took a couple of minutes and all is good...
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
I am working on a new program that uses a lot of float values. However, I notice that if I set the value it will not be precise..

ie.

float val = 10.3

may show up in the debugger as 10.300002 or 10.299999993

How can I make these numbers alway return 10.3 exactly?

Thanks,

Google for "what every programmer should know about floating point arithmetic" and you should find a few links to the Kahan article, which on one hand contains much more than you want to know, on the other hand just barely covers what you need to know.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.