We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › wrong float approximation
Page Index Toggle Pages: 1
wrong float approximation (Read 1630 times)
wrong float approximation
Feb 12th, 2010, 5:24am
 
Hi,

I did stumble upon a weird problem, I'm running a basic test to increase a float variable by 0.1 but at some point processing starts to approximate the decimal in a weird way, does anybody had this problem before?

this is the test I wrote

float x_pos, y_pos;

void setup() {
     x_pos = 0f;
     y_pos = 0f;
}

void draw(){
     x_pos = x_pos+0.1f;
     y_pos += 0.1;
     println(x_pos+" "+y_pos);
}


OUTPUT
0.1 0.1
0.2 0.2
0.3 0.3
0.4 0.4
0.5 0.5
0.6 0.6
0.70000005 0.70000005
0.8000001 0.8000001
0.9000001 0.9000001
1.0000001 1.0000001
Re: wrong float approximation
Reply #1 - Feb 12th, 2010, 5:31am
 
Yes, it is a well known problem in computing. Float numbers cannot have exact binary representation, they are approximated. So after some calculations, rounding cumulate and give odd results. That's why accounting never uses floating point numbers.
A possible workaround is to use doubles.
Re: wrong float approximation
Reply #2 - Feb 12th, 2010, 5:33am
 
I've tried to use doubles and I had the same problem, any other idea? I thought to take like 3 or 4 decimal digits on each cicle, it should limit the error by cutting the wrong approximated digits, what do you think about?
Re: wrong float approximation
Reply #3 - Feb 12th, 2010, 7:10am
 
Note 1: in Processing, you don't need to add the f suffix to floating point numbers.
Note 2: if the numbers are used as shape measures (eg. the radius of a circle), you don't have to worry about such drift as it won't show on display.
If you need to display nicely the data on screen, use nf().
Re: wrong float approximation
Reply #4 - Feb 12th, 2010, 12:35pm
 
If you are only concerned about formatting the output you could use nf()

It looks like you are storing x,y positions so an error of 1 in 1000000 is unlikely to be have significant effect and not worth the effort to correct (even if it was possible) since the problem is likely to reoccur when using the results in other calculations.

Re: wrong float approximation
Reply #5 - Feb 12th, 2010, 10:34pm
 
If you must have accuracy, you could implement binary coded decimal (BCD), though this will not be as simple as saying "a = b * c;"

It would be more like "a = multiply(b, c);" where multiply() handles the BCD calculations.
http://en.wikipedia.org/wiki/Binary-coded_decimal
Re: wrong float approximation
Reply #6 - Feb 13th, 2010, 1:03am
 
Note that Java has BigInteger and BigDecimal support.
Page Index Toggle Pages: 1