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
   Programs
(Moderators: fry, REAS)
   int float mismatch causes hang
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: int float mismatch causes hang  (Read 265 times)
kevinP

Email
int float mismatch causes hang
« on: May 8th, 2004, 1:54am »

This seems to reliably (well twice) cause Processing (Alpha 0068, Linux) to hang:
 
Code:

for(int i = 0; i < width; i += 0.1) {
  point(x, x+1);
}

 
Shouldn't it fail with some kind of error? Strangely (?), this doesn't hang:
Code:

int i = 1;
i += 0.1;
 

Kevin Pfeiffer
TomC

WWW
Re: int float mismatch causes hang
« Reply #1 on: May 8th, 2004, 2:11am »

OK, it's not immediately obvious, but if you add a float to an int, it will only use the whole number part of the float.  The whole number part of 0.1 is 0. (The whole number part of 0.9 is also 0).
 
That means that your first code is actually an infinite loop, since i += 0.1 is equivalent to i += 0 when i is an int.
 
The second snippet doesn't crash because it's perfectly OK to add 0.1 to an integer.  But it won't change the value of i.
 
kevinP

Email
Re: int float mismatch causes hang
« Reply #2 on: May 8th, 2004, 10:47am »

Thanks Tom! (See what happens when I post a bug report: operator error).
 

Kevin Pfeiffer
Pages: 1 

« Previous topic | Next topic »