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 › Handling Division
Page Index Toggle Pages: 1
Handling Division (Read 1837 times)
Handling Division
May 11th, 2010, 5:42pm
 
Let me start by saying I am a sad, sad novice. But here is my question.  I have a variable called 'scaler' which i declared as a float.  I assign the value  as 2/width.

Code:

float scaler;

void setup(){

size(320,240);
scaler = 2/width;

}

void draw(){

println("scaler = " + scaler);

}


I expect the output to be 0.00625, but instead, it is 0.0   Lips Sealed what am I missing?  Thanks!
Re: Handling Division
Reply #1 - May 11th, 2010, 6:03pm
 
The reason you're having trouble is that the variable width is an inteteger, and the number "2" is treated as an integer.  When dividing an integer by an integer, Processing (i.e., Java) will return an integer -- and since width is always much bigger than 2, the result is always zero.  It doesn't get converted to a float until after the division is complete.

The easiest fix is:

Code:
scalar = 2.0/width; 


Changing 2 to 2.0 forces Processing to treat that number as a float, which means it converts width to a float and then gives you a (correct) float result.

This would also work:

Code:
scalar = ((float) 2) / width; 


The (float) cast forces the program to covert 2 to a float before dividing.  The first way is simpler and easier to read for this case, but the second way will work if you're using an int variable in place of the constant 2.

I also fixed your spelling of "scalar". Wink
Re: Handling Division
Reply #2 - May 21st, 2010, 6:32pm
 
Thank you very much for the help!  Here is my follow on question.  when I define scalar in my void setup:

Code:

float scalar;

void setup(){

size(640, 480);
scalar = 200.0/width;

}

void draw(){

println("scalar = " + scalar);

}



the output is 0.  but when i define scalar in void draw,
Code:

float scalar;

void setup(){

size(640, 480);

}

void draw(){

scalar = 200.0/width;
println("scalar = " + scalar);

}



the output is the float value 0.3125 that I was expecting.  Why doesn't scalar evaluate to a float when it is in the void setup()?

Thanks!
Re: Handling Division
Reply #3 - May 22nd, 2010, 12:57am
 
First version you give works for me (in Processing 0184, but version shouldn't be relevant here).
Re: Handling Division
Reply #4 - May 22nd, 2010, 6:14am
 
First version works fine for me too.

Try copying and pasting your first version into Processing and running it again to be sure.  (Maybe you had some other error you "accidentally" corrected?)
Re: Handling Division
Reply #5 - May 23rd, 2010, 6:58pm
 
Hmm. It worked for me when I cut and pasted it back in.  Strange...Sorry to bother every one, thanks for the help.
Page Index Toggle Pages: 1