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)
   the dumbest question ever
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: the dumbest question ever  (Read 299 times)
lunetta

7200475272004752 WWW Email
the dumbest question ever
« on: Dec 4th, 2004, 7:14pm »

If I depend on my programming skills for surviving... sweet old Darwin laws may apply.
 
check this complex code:
 
float n;
void setup() {
n=0; }
void loop() {
  n= n+(0.5);
  println(n);
}
 
 
works, right?
 
now check this one:
 
float n;
void setup() {
n=0; }
void loop() {
  n= n+(1/2);
  println(n);
}
 
It doesn't. Returns only zeros. Why?
 
this is me:
 
void conceptual() {
float understanding = 0;
if (understanding > 0) { return miracle; }
}
 
 
TomC

WWW
Re: the dumbest question ever
« Reply #1 on: Dec 4th, 2004, 7:50pm »

1 and 2 are integer constants (ints).
 
Integer division in Java (and lots of other languages) will throw away the remainder.  1/2 doesn't represent a half, it represents integer division of 1 by 2.  1 divided by 2 is 0, remainder 0.5.  But the 0.5 gets thrown away.
 
In this case, 1/2 will be calculated by the compiler as 0, and then when the code is run each time loop is called it will add 0 onto n.
 
If you use 1.0/2.0, then it will use float division and give you the answer you expect.
 
lunetta

7200475272004752 WWW Email
Re: the dumbest question ever
« Reply #2 on: Dec 4th, 2004, 8:06pm »

return miracle;
 
Pages: 1 

« Previous topic | Next topic »