This site requires JavaScript, please enable it in your browser!
Greenfoot back
Pointifix
Pointifix wrote ...

2014/3/16

Possible loss of precision

Pointifix Pointifix

2014/3/16

#
Hi again, im getting this error and i dont know why? possible loss of precision found :double; required: float These are the variables:
public float speed = 10;
public float angle = 30;
This is the code i get the error:
private void SetPosition()
{
    float delta_x = Math.cos(angle) * speed;
    float delta_y = Math.sin(angle) * speed;
        
    setLocation(getX() + delta_x,getY() + delta_y);
}
The error is marked at * speed;
bourne bourne

2014/3/16

#
floats are 32 bit. doubles are 64. The methods cos and sin return double, multiplying by the float "speed", results in a double. Then you try to assign a double to a float variable. There's a couple things you could do. 1. Use doubles instead of floats 2. Cast (truncate, and like the compiler said - possible loss of precision) the values returned by sin and cos to type float
Pointifix Pointifix

2014/3/16

#
ah ok thanks
You need to login to post a reply.