Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when converting double (or floats) to ints?

Tags:

java

casting

2d

I'm practicing some simple 2D game programming, and came up with a theory that during animation (the actual change in a image position is best calculated with floating point numbers). I have a feeling that if you move an image around with ints the animation won't be as smooth.

In Java it seems you can't draw an image with floating point numbers to give an image a position. But apparently when you initially declare your x and y 's, you can declare them as Double, or Float, and when it comes to actually drawing the image you have to cast them to ints. Like I find HERE :

    /**
 * Draw this entity to the graphics context provided
 * 
 * @param g The graphics context on which to draw
 */
public void draw(Graphics g) {
    sprite.draw(g,(int) x,(int) y);
}

My question is about how Java handles the conversion? If the code casts these doubles at the last minute, why have them as doubles in the first place? Does Java hide the numbers after the decimal?

I know in C and C++ the numbers after the decimal get cut off and you only see whats before it. How does Java handle this casting?

like image 546
mastrgamr Avatar asked Nov 02 '11 19:11

mastrgamr


People also ask

What happens when you convert a double to an int?

As we all know, a Double primitive contains decimal digits. On conversion of these values to integers, the decimal digits get truncated, by rounding off the number to the nearest integer according to the method you choose.

What happens when float is converted to int?

Method 1: Conversion using int(): To convert a float value to int we make use of the built-in int() function, this function trims the values after the decimal point and returns only the integer/whole number part. Example 1: Number of type float is converted to a result of type int.

Can you go from double to int?

We can convert double to int in java using typecasting. To convert double data type into int, we need to perform typecasting. Typecasting in java is performed through typecast operator (datatype).


1 Answers

Pixels on a display are discrete and limited in number; therefore display coordinates need to be integer numbers - floating point numbers make no sense, as you do not physically have a pixel at e.g. (341.4, 234,7).

That said, integers should only be used at the final drawing stage. When you calculate object movement, speeds etc, you need to use floating point numbers. Integers will cause an amazing number of precision problems. Consider the following snippet:

a = 1;
x = (a / 2) * 2;

If a and x are floating point numbers, x will finally have the expected number of 1. If they are integers, you will get 0.

Baseline: use floating point types for physics computations and convert to int at drawing time. That will allow you to perform the physics calculations with as much precision as required.

EDIT:

As far as the conversion from FP numbers to integers is concerned, while FP numbers have a greater range, the values produced by your physics calculation after normalization to your drawing area size should not normally overflow an int type.

That said, Java truncates the floating point numbers when converting to an integer type, which can create artifacts (e.g. an animation with no pixels at the rightmost pixel column, due to e.g. 639.9 being converted to 639 rather than 640). You might want to have a look at Math.round() or some of the other rounding methods provided by Java for more reasonable results.

like image 64
thkala Avatar answered Dec 08 '22 19:12

thkala