Loading...
Logo
Processing Forum

What Does (float) do ?

in Programming Questions  •  6 months ago  
Hey, guys I have a burning question to ask, which I couldn't find the answer on internet. 
I have been reading  Learning Processing by Daniel Shiffman for a week now. I liked the book, much better than Getting Started with Processing I think.
However, after I got to the image chapter I came across with a line of code that the book hadn't mentioned yet.

Copy code
  1. float adjustBrightness = ((float) mouseX / width) * 8.0; 

Could someone enlighten me about what that float in parenthesis exactly does?  I searched the web page but couldn't find a clue.


Replies(2)

It's a type cast. Basically, there are two kinds of "numbers" that computers use: integers (int) and floating point numbers (float). The "(float)" in in the line of code says that, even though the variable mouseX is an int, treat it like a float this one time so that when you do division, you get a float back instead of an int - basically, it's there to avoid truncating the result of the division to a whole number.


Oh wow, It all makes sense now..
Thanks for the reply tfguy44!