We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Say I want to draw 2 circles. One followed by another, both spreading out.
To make the second circle come behind the first one, I'd use variable i,j like below.
This one works as I expect, but when I writej+=1/2
instead of j+=0.5
, it doesn't work.
What's wrong with the a fraction?
float i,j=0;
void setup(){
size(200,200);
}
void draw(){
ellipse(width/2,height/2,i,i);
ellipse(width/2,height/2,j,j);
++i;
j+=0.5;
}
Answers
When you do math with ints, the result is an int. 1/2 is 0
When you do math with a float, the result is a float. 1/2.0 is 0.5
integer division...
http://forum.processing.org/two/discussion/6013/not-sure-why-this-happens-integer-division
(there used to be a FAQ for this, i'm sure. but i can't find one)
oh, here it is. it was mentioned above but has moved to github:
https://github.com/processing/processing/wiki/Troubleshooting#why-does-2--5--0-instead-of-04
That is 1 of the most serious omissions for Processing's web reference for operator
/
: X(https://Processing.org/reference/divide.html
There are only 2 examples there using
int
.But no mention about the result is truncated when both operands are of whole type! 8-}
Remember that for a long time, ArrayList's reference didn't say a single word about generics & its diamond
<>
.The result is that many folks still dunno about
<>
due to the long wait to include such important information! (:|Thank you. Now I understand why. This is what I'd never figure out by myself. You guys are so good at teaching :-bd