Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Loss of Precision (Double/Int)

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Loss of Precision (Double/Int)

    Hiya. Im still a newbie so sorry! I get a loss of precision error on the Double (says should be int), however my intended values are decimals so I cannot use int.Works fine with int, but then I cant use decimals. Any way round this?

    Thanks!

    public class officeBuilding {
        public static void main(String[] args) {
            int groDouPress, topDouPress;
            String groPress =
                JOptionPane.showInputDialog("Enter Pressure at Ground Level");
            String topPress=
                JOptionPane.showInputDialog("Enter Pressure at roof of Building");
            groDouPress = Double.parseDouble(groPress);
            topDouPress = Double.parseDouble(topPress);
            double feet = 25000*(Math.log(groDouPress/topDouPress));
            JOptionPane.showMessageDialog(null,("The height is feet is " +feet), ("Building Height"), JOptionPane.INFORMATION_MESSAGE);
        }
    }
    Last edited by Scotty; October 7th, 2010 at 04:00 PM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Loss of Precision (Double/Int)

    What do you mean by loss of precision?
    There is a certain number of decimal places the point double can hold, and any more than that it is forced to round, you can't get an exact answer using double. If it is computing the math wrong let me give you an example of what it does.

    Consider writing the value 1/3 in decimal notation:

    0.333333333333333333

    You would literally need an infinite number of 3's for it to give you 1/3, but a double cannot hold them, rather 64 bit(which correct me if I'm wrong relates to 14 decimal places), so it would look something like
    0.3333333333334 (If I counted my digits right). Now if you multiplied it by another rounded number and another rounded number, it could possibly give you a very wrong answer. If your getting an answer like 0.123E2 It means it's giving it to you in scientific notation, and the E2 means * 10^2 There is a certain point where the number will just give you a wrong answer if the numbers are to big. Hope that helps

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Loss of Precision (Double/Int)

    Thanks for your reply. I mean the error:



    D:\java\officeBuilding.java:9: possible loss of precision
    found   : double
    required: int
                    groDouPress = Double.parseDouble(groPress);
                                                    ^
    D:\java\officeBuilding.java:10: possible loss of precision
    found   : double
    required: int
                    topDouPress = Double.parseDouble(topPress);
                                                    ^
    2 errors

    I dont get this if I use int, but I get an error when I run it if I use decimal values (obviously)

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Loss of Precision (Double/Int)

    The variable on the left of the = is an int. The method on the right of the = does not return an int.
    Use a cast to change the value to an int.

  5. #5
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Loss of Precision (Double/Int)

    The error means that you are attempting to put a double value (a value with decimals) in an int (a value without decimals). This will give you an error as the compiler is worried you made a mistake and all hell will break loose or something.

    Look at this bit of your code:
    int groDouPress, topDouPress;
    ...
    groDouPress = Double.parseDouble(groPress);
    topDouPress = Double.parseDouble(topPress);
    You declared groDouPress and topDouPress as ints, but you attempt to set them as doubles.

    To fix this, you just need to declare them as doubles:
    double groDouPress, topDouPress;
    ...
    groDouPress = Double.parseDouble(groPress);
    topDouPress = Double.parseDouble(topPress);

    Tell me if that makes sense.



    EDIT: Norm, he is trying to use decimals but declared it incorrectly:
    however my intended values are decimals so I cannot use int.Works fine with int, but then I cant use decimals. Any way round this?
    Last edited by aussiemcgr; October 7th, 2010 at 06:24 PM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  6. The Following User Says Thank You to aussiemcgr For This Useful Post:

    Scotty (October 9th, 2010)

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: Loss of Precision (Double/Int)

    int groDouPress, topDouPress;

    what happens if you change those two to type double?

  8. The Following User Says Thank You to javapenguin For This Useful Post:

    Scotty (October 9th, 2010)

  9. #7
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Loss of Precision (Double/Int)

    Thank you!

Similar Threads

  1. need to add a Double Button to the code
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 15
    Last Post: August 5th, 2010, 11:19 AM
  2. [SOLVED] "possible loss of precision", except not, code doesn't work, simple question
    By Perd1t1on in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 07:11 PM
  3. Double Buffering
    By Ganezan in forum Java Theory & Questions
    Replies: 2
    Last Post: November 20th, 2009, 03:51 AM
  4. Replies: 6
    Last Post: October 23rd, 2009, 03:53 AM
  5. string to double
    By wolfgar in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 13th, 2009, 10:47 PM