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

Thread: Java Calculation Incorrect

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Calculation Incorrect

    Why is:
    public class Main
    {
        public static void main(String[] args)
        {
    		double answer = 107
    				* ((1.00 + (10.00 + 0) / 79.00))
    				* 10.00;
    	System.out.println(answer);
        }
    }
    Producing 1205.4430369636836


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java Calculation Incorrect

    Because it is the correct answer. Remember brackets have greater precedence over multiply and divide.
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Calculation Incorrect

    Quote Originally Posted by Junky View Post
    Because it is the correct answer. Remember brackets have greater precedence over multiply and divide.
    		double answer = 107
    				* ((1.00 + (10.00 + 0) / 79.00))
    				* 10.00;
    10.00 + 0 = 10.00
    10.00 / 79.00 = 0.13
    0.13 + 1.00 = 1.13
    1.13 * 10.00 = 11.3
    But it isn't, at least according to me. Can you explain?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java Calculation Incorrect

    For starters you are introducing a rounding error. 10.0 / 79.0 = 0.12658..... not 0.13. Secondly what about the 107?
    Improving the world one idiot at a time!

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Calculation Incorrect

    it is correct output only

Similar Threads

  1. Calculation error...very New to Java..please be kind.
    By medicinaluser in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 1st, 2011, 11:16 PM
  2. Incorrect Key Input
    By risen375 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 28th, 2011, 01:17 PM
  3. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  4. [SOLVED] Code is correct, but incorrect output?
    By moodycrab3 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: February 6th, 2011, 02:32 PM
  5. Swing incorrect behaviour from JRE5 to JRE6
    By singhkanhaiya in forum AWT / Java Swing
    Replies: 1
    Last Post: August 25th, 2009, 01:23 AM