FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   Floats adding weirdly
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Floats adding weirdly  (Read 226 times)
skloopy

WWW
Floats adding weirdly
« on: Feb 14th, 2004, 4:10am »

Check this one out. Maybe it's just a rounding problem i don't know about, but when you add up .01 one hundred times it doesn't equal 1!
 
I found it out when i was trying to increment a variable by .01 and then test when it was equal to 1, but it never did.
 
Does anybody know why that happens?
 
Code:
float notthesumofmyparts;
int frame;
 
void loop() {
  frame++;
  for(int i=0; i<100; i++) {
    notthesumofmyparts += .01;
  }
  noStroke();
  rect(width/2,0,(frame-notthesumofmyparts)*100,height);
}
« Last Edit: Feb 14th, 2004, 4:10am by skloopy »  
toxi

WWW
Re: Floats adding weirdly
« Reply #1 on: Feb 14th, 2004, 1:05pm »

scloopy, have a look at this thread for an explanation
 

http://toxi.co.uk/
skloopy

WWW
Re: Floats adding weirdly
« Reply #2 on: Feb 14th, 2004, 10:06pm »

Thanks Toxi! Sorry I should have looked a little further with search
 
I'm still not sure i get it, but i'm just going to use ints to avoid the problem. Thanks!
 
toxi_
Guest
Email
Re: Floats adding weirdly
« Reply #3 on: Feb 15th, 2004, 12:16am »

hey scloopy, found another paper which hopefully clears things up:
 
http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html
 
under "Ranges of Floating-Point Numbers" it says...
 
"Let's consider single-precision floats for a second. Note that we're taking essentially a 32-bit number and re-jiggering the fields to cover a much broader range. Something has to give, and it's precision. For example, regular 32-bit integers, with all precision centered around zero, can precisely store integers with 32-bits of resolution. Single-precision floating-point, on the other hand, is unable to match this resolution with its 24 bits. It does, however, approximate this value by effectively truncating from the lower end."
 
this effect of truncating obviously increases with every mathemetical operation, especially if the numbers are relatively small. makes sense?
 
on a related note, this all also borders on the old problems  in math and the definition of real numbers and infinity . there's a great,great book about it by david foster-wallace, called "everything and more"
 
skloopy

WWW
Re: Floats adding weirdly
« Reply #4 on: Feb 15th, 2004, 7:42pm »

Code:
print((.01+.01+.01+.01+.01+.01+.01+.01+.01+.01+.01+.01)+" is ");
print((.01*12)+"!");

So the first one doesn't work because it has 16 times as many errors! I think I get it. If you add .01 12 times then the error becomes visible, but multiplication is only one operation so it has less error, and just gets rounded back to .01? And the problem sneaks in at .04 when you try to add .01 to it.
 
It's amazing to think that we're invoking so much complexity every time we do anything on a computer! Thanks toxi for helping to drill that into my head!
 
Pages: 1 

« Previous topic | Next topic »