Results 1 to 10 of 10
  1. #1
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default 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 co-ordanints of the projectile ends up in a long decimal..

    for example the X co-ordanint might be 9.2938 and the Y co-ordanint 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 co-ordanints. 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?

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by shuks View Post
    ....

    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?
    Sure. The Math.round method that takes a double returns a long.

  3. #3
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    If accuracy is not a problem, why not just

    Java Code:
    double d = 5.43764826;
    int i = (int) d;
    Edit: And, if you want it to round up at .5 or higher (as is common in standard mathematics) just do
    Java Code:
    double d = 5.43764826;
    int i = (int) (d + 0.5);
    Assuming, of course, that the values are all between Integer.MIN_VALUE and Integer.MAX_VALUE.
    Last edited by masijade; 10-12-2009 at 09:30 AM.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by shuks View Post
    Ok, I changed all the variables to type "float". The Math.round() function is supposed to take a float and return an int, but this is not working??
    Don't do that. Why would you want to? Simply cast the things.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    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);
    rounding up at .5 or higher
    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);

  6. #6
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    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

  7. #7
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    Worked great!! thanks. Such a simple solution, I'm embarrassed I didnt try that before :p

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by shuks View Post
    ... Such a simple solution, I'm embarrassed I didnt try that before :p
    Please await your defenestration time.
    It's one at a time, no jumping the queue.
    Last edited by r035198x; 10-12-2009 at 10:42 AM.

  9. #9
    shuks is offline Member
    Join Date
    Oct 2009
    Posts
    18
    Rep Power
    0

    Default

    Huh?? what is a defenestration time?

  10. #10
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

Similar Threads

  1. Replies: 3
    Last Post: 07-25-2009, 02:44 PM
  2. decimal calculations?
    By arnab321 in forum CLDC and MIDP
    Replies: 5
    Last Post: 11-19-2008, 04:36 AM
  3. numbers with two decimal places
    By little_polarbear in forum New To Java
    Replies: 8
    Last Post: 08-28-2008, 12:04 AM
  4. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 02:19 PM
  5. Capping decimal places
    By Rageagainst20 in forum New To Java
    Replies: 1
    Last Post: 12-20-2007, 10:28 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •