# Possible loss of precision

kavitha ms

Greenhorn

Posts: 1

posted 7 years ago

import java.awt.*;

import java.util.*;

import java.applet.*;

import java.lang.*;

public class m1 extends Applet

{

public void paint (Graphics g)

{

float Dx=0,Dy=0,Dz=0, Vxy=0,Vz=0,Vx=0,Vy=0,Axy=0,Az=0;

double heading=75, climb=30;

float TAccn=15,TSpeed=10,t=5;

int n=1,i;

double[] X = new double[5];

double[] Y = new double[5];

double[] Z = new double[5];

//Acceleration with climb motion

Axy = TAccn * ((float) Math.cos(climb));

Az = TAccn * ((float) Math.sin(climb));

Vxy = TSpeed * ((float) Math.cos(climb));

Vz = TSpeed * ((float) Math.sin(climb));

Vx = Vxy * ((float) Math.sin(heading));

Vy = Vxy * ((float) Math.cos(heading));

float sin_heading = (float) Math.sin(heading);

float t1 = t * t;

Dx = 0.5 * Axy * t1 * sin_heading;

Dy = 0.5 * Axy * t1 * ((float) Math.cos(heading));

Dz = 0.5 * Az * t1;

for(i=0;i<n;i++)

{

X[i] = X[i] + Vx * t + Dx;

Y[i] = Y[i] + Vy * t + Dy;

Z[i] = Z[i] + Vz * t + Dz;

int x1 = (int) X[i];

int y1 = (int) Y[i];

g.drawLine(0,0,x1,y1);

}

}

}

this is the program i hav written

but its giving error like

possible loss of precision for the lines

Dx = 0.5 * Axy * t1 * sin_heading;

Dy = 0.5 * Axy * t1 * ((float) Math.cos(heading));

Dz = 0.5 * Az * t1;

import java.util.*;

import java.applet.*;

import java.lang.*;

public class m1 extends Applet

{

public void paint (Graphics g)

{

float Dx=0,Dy=0,Dz=0, Vxy=0,Vz=0,Vx=0,Vy=0,Axy=0,Az=0;

double heading=75, climb=30;

float TAccn=15,TSpeed=10,t=5;

int n=1,i;

double[] X = new double[5];

double[] Y = new double[5];

double[] Z = new double[5];

//Acceleration with climb motion

Axy = TAccn * ((float) Math.cos(climb));

Az = TAccn * ((float) Math.sin(climb));

Vxy = TSpeed * ((float) Math.cos(climb));

Vz = TSpeed * ((float) Math.sin(climb));

Vx = Vxy * ((float) Math.sin(heading));

Vy = Vxy * ((float) Math.cos(heading));

float sin_heading = (float) Math.sin(heading);

float t1 = t * t;

Dx = 0.5 * Axy * t1 * sin_heading;

Dy = 0.5 * Axy * t1 * ((float) Math.cos(heading));

Dz = 0.5 * Az * t1;

for(i=0;i<n;i++)

{

X[i] = X[i] + Vx * t + Dx;

Y[i] = Y[i] + Vy * t + Dy;

Z[i] = Z[i] + Vz * t + Dz;

int x1 = (int) X[i];

int y1 = (int) Y[i];

g.drawLine(0,0,x1,y1);

}

}

}

this is the program i hav written

but its giving error like

possible loss of precision for the lines

Dx = 0.5 * Axy * t1 * sin_heading;

Dy = 0.5 * Axy * t1 * ((float) Math.cos(heading));

Dz = 0.5 * Az * t1;

posted 7 years ago

The problem is you are multiplying the float variable with 0.5 (which is double by default). Try using 0.5f if you want to do it the same way you are doing.

[ October 22, 2008: Message edited by: Vijitha Kumara ]

0.5 * Axy * t1 * sin_heading;

The problem is you are multiplying the float variable with 0.5 (which is double by default). Try using 0.5f if you want to do it the same way you are doing.

[ October 22, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5

[How to ask questions] [Twitter]

Campbell Ritchie

Sheriff

Posts: 48642

56

posted 7 years ago

Also please do UseCodeTags, it makes it easier for us to help you . Now onto your question, did you manage to resolve your problem?

Cheers, Martijn - Blog,

Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,

My start-up.