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.
Page Index Toggle Pages: 1
why need do it? (Read 552 times)
why need do it?
May 2nd, 2009, 5:45am
 
when I do this "println(mouseX/width);" get the value always zero, but if I change to "println((float)mouseX/width);" then I can get the float value, why? I mean why need convert the mouseX? Is it default type as int? thx
Re: why need do it?
Reply #1 - May 2nd, 2009, 6:53am
 
Indeed, the mouseX reference isn't explicit about its type.
But well, mouse coordinates refers to pixels, so it makes sense it is an integer...
Re: why need do it?
Reply #2 - May 2nd, 2009, 9:06am
 
What PhiLho didn't mention is that an integer divided by an integer results in an integer, and since mouseX is something less than width, the result always rounds down to zero.

Writing (float)mouseX is casting the integer as a float (rational number), and when you do the division the result is a float.

-spxl
Re: why need do it?
Reply #3 - May 2nd, 2009, 11:01am
 
cool! clearly answer, thx a lot
Page Index Toggle Pages: 1