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 6 of 6

Thread: Trouble With Doubles

  1. #1
    Junior Member
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Trouble With Doubles

    No rhyme intended.

    import java.util.Scanner;
     
    public class CalculatorProgram {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    		double firstNumber, secondNumber, answer;
     
    		System.out.println("Enter the first number:");
    		firstNumber = input.nextDouble();
     
    		System.out.println("Enter the second number:");
    		secondNumber = input.nextDouble();
     
    		answer = firstNumber + secondNumber;
    		System.out.println(answer);
    	}
    }

    When given two doubles, this code occasionally gives an answer that is incorrect (unless I'm seriously overlooking something math-wise). For instance, I gave it 23.2 and 26.4 and it gave an answer of 49.599999999999994. Can you think of any reason why the answer is not a double and also incorrect?


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Trouble With Doubles

    The answer is a double.

    Some suggested reading: What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Computers represent real numbers using floating point notation. However, this method has discretization limits. Basically, you can't represent every possible number. So the results of floating point operations aren't always completely correct. They're fairly close, though. For example, the printed result is off by 0.000000000000006.

  3. #3
    Junior Member
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble With Doubles

    I feel really stupid having to have a primitive type defined, but then what exactly is a double? I thought it was a number with a decimal point rounded to the second decimal place.

    Also, why does floating point notation lead to errors? I could understand in very large numbers, but for numbers with only two decimal points?

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Trouble With Doubles

    Some time invested on your favorite search engine would better answer your question than a forum. Spend some time looking up double and there are many lengthy explanations about what happens. Did you try the link posted by helloworld922 above? If not, read that. Also read this.

  5. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Trouble With Doubles

    A double is a floating point number. However, it's "bigger" (i.e. takes up more memory) so can more precisely represent numbers. However, in the end it's still limited in what numbers it can represent.

    It leads to errors because of several possibilities:

    1. One or both of the numbers can't be represented exactly using floating point notation.
    2. The result can't be represented exactly using floating point notation.

  6. #6
    Junior Member
    Join Date
    Aug 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Trouble With Doubles

    Ok, thanks for the help everyone. This one is solved now.

Similar Threads

  1. BlueJ trouble or program trouble (Combining Arraylists)
    By star12345645 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 11th, 2012, 12:15 PM
  2. Doubles are somehow Strings?
    By collegejavastudent in forum What's Wrong With My Code?
    Replies: 14
    Last Post: December 13th, 2011, 04:29 PM
  3. Adding doubles and ints.
    By SkyAphid in forum Java Theory & Questions
    Replies: 6
    Last Post: September 8th, 2011, 05:54 PM
  4. Addition of doubles (newbie question)
    By archyb in forum Java Theory & Questions
    Replies: 2
    Last Post: April 21st, 2011, 09:52 AM
  5. Reading doubles from file
    By fawx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 24th, 2009, 11:57 PM