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 › float incremented by 0.005 stuck at 132072.00
Page Index Toggle Pages: 1
float incremented by 0.005 stuck at 132072.00 (Read 1047 times)
float incremented by 0.005 stuck at 132072.00
Jul 25th, 2007, 8:12am
 
I found this in a bigger code, but it can be easily reproduced. Try this;

Quote:


float x=131070;

void draw(){

 x=x+0.005;
 println(x);
}


It will never go any bigger than 132072.00!
It's probably a rounding issue (no more bits for decimals?). How's that? How to get around?
thanks for helping.
vicente
Re: float incremented by 0.005 stuck at 132072.00
Reply #1 - Jul 25th, 2007, 10:32am
 
hmm, are you sure? works fine for me
Re: float incremented by 0.005 stuck at 132072.00
Reply #2 - Jul 25th, 2007, 10:49am
 
I've got the same issue with 124 & 125.

It is stuck at 131072.00

Defining x as any number bigger than 131072.00 is not working. :/
Re: float incremented by 0.005 stuck at 132072.00
Reply #3 - Jul 25th, 2007, 10:53am
 
The "problem" has in fact to do with the float datatype and its inner workings. I've posted about this in detail over on the ALPHA board... some things never change Wink

In this case and other's you either have to change the scale of your number range (e.g. times factor 1000) and use int's instead. Alternatively you could switch to the "double" type which has exponentially higher precision, but is slower to deal with on 32bit machines (the main reason Processing is mainly using float's)
Re: float incremented by 0.005 stuck at 132072.00
Reply #4 - Jul 25th, 2007, 11:01am
 
Just to confirm the above has to do with a precision overflow whenever another bit is required to encode the integer part of a float:

131070 = 2^17 - 2
131072 = 2^17

Reaching 2^17 the fractional precision of the number has to be reduced since the extra bit needs to be fitted into the 32 available bits within a float...
Re: float incremented by 0.005 stuck at 132072.00
Reply #5 - Jul 25th, 2007, 11:27am
 
Code:

float x=131072;

void draw(){

x=x+0.005;
println(x);
}


Doesn't work on my computer. PC - Dell.

But the code does work in Flash - however, Flash starts randomly adding 0.0000001 to fractions on a regular basis. That's Flash for you.

Code:

double x=131072;

void draw(){

x=x+0.005;
println((float)x);
}


This works.

It's odd that 131072 in binary is 11111111111111111. So you get 17 bits before you start to lose anything less that 0.01 added to it, and it doesn't shave off more even as the bits start piling on.

Odd.
Re: float incremented by 0.005 stuck at 132072.00
Reply #6 - Jul 25th, 2007, 11:29am
 
Bah - took me that long to write it.
Re: float incremented by 0.005 stuck at 132072.00
Reply #7 - Jul 25th, 2007, 6:45pm
 
Just to add an easy "rule of thumb" to the already more rigorous answer given by toxi:  you can expect problems whenever you need more than about 7 significant digits to represent a number (regardless of magnitude) with single-precision floats.  Thus, attempting to represent 131070.000 versus 131070.005, each with 9 digits, can be expected to fail.
Re: float incremented by 0.005 stuck at 132072.00
Reply #8 - Jul 26th, 2007, 3:11am
 
Thanks everyone for the help.
I didn't even know about doubles in processing. I tested, it works.
Are there any concern, besides already mentioned slower speed, when using double. I mean it is java and not processing right? So any difference?

Else, Toxi what do you mean by "(e.g. times factor 1000)". I did not get, could you be more specific?

I used two ints to do the job, it works and is faster.

again, thanks.

vicente
Re: float incremented by 0.005 stuck at 132072.00
Reply #9 - Jul 26th, 2007, 10:51am
 
Processing IS Java. They've just made it not look like Java so it isn't scary. If you understand how classes and objects and packages work you can probably start to guess how they did it.

Neat isn't it?

Doubles are slower than floats are slower than ints are slower than bytes (at least I think the last ones are right).

Make whatever sacrifices you need to get your results.

Toxi means that you could get precision by doing this sneaky trick:
Code:

int x=131072000;
void draw(){
x+=5;
println(x);
println((float)x/1000);
}

The float conversion will always be a bit off but the true value will be proper.

Wikipedia's definition of a factor

Oh the joy of binary.
Re: float incremented by 0.005 stuck at 132072.00
Reply #10 - Jul 28th, 2007, 4:16am
 
Thanks a lot st33D. Very instructive.
Page Index Toggle Pages: 1