I am currently watching some tutorials and I am stuck at a part that looks like this:
int x = (int) a_float / an_int;
What will happed if a_float / an_int; is not an integer? Will the number be rounded / floored / weird stuff?
I am currently watching some tutorials and I am stuck at a part that looks like this:
int x = (int) a_float / an_int;
What will happed if a_float / an_int; is not an integer? Will the number be rounded / floored / weird stuff?
Copyright © 2021 Jogjafile Inc.
In Java, casting takes precedence over division: http://introcs.cs.princeton.edu/java/11precedence/. This means that the statement can be broken down as follows:
a_float
(basically take the floor and store as anint
)an_int
int
value tox
If you want
an_int
to be promoted to afloat
before the operation, then do the division, omit the cast. The result will be afloat
(which you can than truncate back into anint
before assignment):is the same as
You can also read this site for a pretty good explanation of what it all means: http://www.mathcs.emory.edu/~cheung/Courses/170/Syllabus/04/mixed.html