Results 1 to 10 of 10
Thread: converting a decimal to an int
 10122009, 08:15 AM #1Member
 Join Date
 Oct 2009
 Posts
 18
 Rep Power
 0
converting a decimal to an int
Hi, good to be here.
I am writing a program that simulates a projectile's trajectory. I have the mathematical equation to do so but I have a problem when I try to implement graphics.
The problem is that when I compute the math the resulting X and Y coordanints of the projectile ends up in a long decimal..
for example the X coordanint might be 9.2938 and the Y coordanint might be 17.3726.
The graphical framework I'm using only excepts integers when drawing objects to the screen so I can't use the exact position of the X and Y coordanints. This model does not have to be extremely accurate so I figure the easy way to solve this problem is to round the number with "Math.round()" function.
This is math equation I'm trying to round...
yPos = Math.round((startHeight + velocity * Math.sin(angle) * time  gravity * time * time/2));
xPos = Math.round((startLength + velocity * Math.cos(angle) * time));
when I try to do this I get an error saying, "Possible loss of precision. found: long, required: int"
All the variables in this equation are type "double", and the xPos and Ypos variables are type int...
Can someon please enlighten me on the situation?
 10122009, 08:25 AM #2Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 8
 10122009, 08:26 AM #3Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
If accuracy is not a problem, why not just
Java Code:double d = 5.43764826; int i = (int) d;
Java Code:double d = 5.43764826; int i = (int) (d + 0.5);
Last edited by masijade; 10122009 at 08:30 AM.
 10122009, 08:31 AM #4Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
 10122009, 08:34 AM #5Senior Member
 Join Date
 Jun 2008
 Posts
 2,571
 Rep Power
 10
Using "your" code
Simply rounding everything down
Java Code:yPos = (int) (startHeight + velocity * Math.sin(angle) * time  gravity * time * time/2); xPos = (int) (startLength + velocity * Math.cos(angle) * time);
Java Code:yPos = (int) ((startHeight + velocity * Math.sin(angle) * time  gravity * time * time/2) + 0.5); xPos = (int) ((startLength + velocity * Math.cos(angle) * time) + 0.5);
 10122009, 08:46 AM #6Member
 Join Date
 Oct 2009
 Posts
 18
 Rep Power
 0
Thanks both of you for the advice. I will try casting it as an int. I will post the results when I get it done. Hopefully it works
 10122009, 09:05 AM #7Member
 Join Date
 Oct 2009
 Posts
 18
 Rep Power
 0
Worked great!! thanks. Such a simple solution, I'm embarrassed I didnt try that before :p
 10122009, 09:26 AM #8Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 8
 10122009, 09:32 AM #9Member
 Join Date
 Oct 2009
 Posts
 18
 Rep Power
 0
Huh?? what is a defenestration time?
 10122009, 09:41 AM #10Senior Member
 Join Date
 Aug 2009
 Posts
 2,388
 Rep Power
 8
Defenestration  Wikipedia, the free encyclopedia
Watch out for the lacerations when your turn comes.
Similar Threads

converting decimal to binary value using recursion in java
By Anindo in forum New To JavaReplies: 3Last Post: 07252009, 01:44 PM 
decimal calculations?
By arnab321 in forum CLDC and MIDPReplies: 5Last Post: 11192008, 04:36 AM 
numbers with two decimal places
By little_polarbear in forum New To JavaReplies: 8Last Post: 08272008, 11:04 PM 
Java calculator decimal
By cart1443 in forum New To JavaReplies: 2Last Post: 04162008, 01:19 PM 
Capping decimal places
By Rageagainst20 in forum New To JavaReplies: 1Last Post: 12202007, 10:28 PM
Bookmarks