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

    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]
            System.out.println(x);
        }
    }
    InputTest.java:3: 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
    Posts
    19,313
    Blog Entries
    1
    Rep Power
    31

    Default

    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
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    10

    Default

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