Results 1 to 3 of 3
  1. #1
    jankidudel is offline Member
    Join Date
    Jul 2010
    Rep Power

    Question Possible loss of precision

    Is it normal that my java compiler complaints against using this ?

    Java Code:
    public class InputTest {
        public static void main(String[] args) {
            [COLOR="Red"]float x = 10000.0 / 3.0;[/COLOR]
    } possible loss of precision
    found : double
    retuired : float.

    float x = 10000.0 / 3.0;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    The right hand side of your statement uses two double literals, one divided by another, producing a double. Since a double is more precise than a float, you are likely losing precision by making the result a float. Why not stick with double?

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Destiny Islands
    Rep Power


    Just to clarify, if you're looking to use float, you'd want to do:
    float x = 10000.0f / 3.0f;

    But Fubarable's idea is better; just use double as it keeps the necessary precision.

Similar Threads

  1. possible loss of precision?
    By gandalf5166 in forum New To Java
    Replies: 6
    Last Post: 04-13-2010, 08:21 PM
  2. Precision
    By c_walker in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 11:36 AM
  3. Wrong precision
    By lclclc in forum New To Java
    Replies: 0
    Last Post: 09-28-2009, 09:55 AM
  4. help me- loss of precision error??
    By j2vdk in forum New To Java
    Replies: 6
    Last Post: 09-01-2008, 09:23 AM
  5. Connection Loss
    By CrazyShells Slam in forum New To Java
    Replies: 0
    Last Post: 05-15-2008, 02:56 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