Results 1 to 11 of 11

Thread: Math issue

  1. #1
    Trevor is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Math issue

    I'm having an issue with a small java program that I have written :
    Java Code:
    int coords[]={500, 750};
    		int vline[]={1, 1};
    		
    		double a = coords[0] + coords[1];
    		double b = vline[0] + vline[1];
    		double c = a / b;
    		double d = c - coords[0];
    		double d1 = c - coords[1];
    		double e = c - d1;
    		double e1 = c - d;
    		
    		System.out.println(e);
    		System.out.println(e1);
    The two arrays at the top with the current values print out the following result e = 750.0, e1 = 500.0. However if I change the values in the vline array to for example "int vline[]={2, 1};" I should get the results e = 333.3333333333333, e1 = 83.33333333333331, but I don't I get the results e = 750.0, e1 = 500.0.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Math issue

    What is the difference between integer and floating point division?

  3. #3
    Trevor is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Math issue

    Quote Originally Posted by Junky View Post
    What is the difference between integer and floating point division?
    I have no idea, what is the difference?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Math issue

    Java Code:
    System.out.println(2 / 3);
    System.out.println(2.0 / 3);
    Run that code and see what results you get.

  5. #5
    Trevor is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Math issue

    I get 0 for the first line and 0.6666666666666666 for the second, so what am I doing wrong?

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Math issue

    Isn't it obvious? The first line is doing integer division. Since integers do not have a floating point part whatever comes after the decimal point gets truncated.

  7. #7
    Trevor is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Math issue

    So I have changed coords and vline to doubles and I'm still having the same problem was that what I needed to change or some thing completely different?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Math issue

    Try printing each of the values as you calculate them to see at which point your program is not doing what you expect or intend.

    If you can't understand the resulting output, post runnable code.

  9. #9
    Trevor is offline Member
    Join Date
    Dec 2011
    Posts
    7
    Rep Power
    0

    Default Re: Math issue

    I have done so and this is what I'm getting if the values for coord are 500, 750 and the values for vline are 2, 1:
    a = 1250.0
    b = 3.0
    c = 416.6666666666667
    d = -83.33333333333331
    d1 = -333.3333333333333
    e = 750.0
    e1 = 500.0

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Math issue


  11. #11
    Herah is offline Member
    Join Date
    Nov 2011
    Posts
    10
    Rep Power
    0

    Default Re: Math issue

    Why do you feel like your results are wrong? Do the math on a calculator, you'll get the same thing.

Similar Threads

  1. math and GUI
    By urbanleg in forum AWT / Swing
    Replies: 3
    Last Post: 08-06-2011, 05:05 PM
  2. Math.cos()
    By ravi1 in forum New To Java
    Replies: 5
    Last Post: 03-27-2011, 03:52 PM
  3. Create Math.sin without math.sin
    By vudoo in forum New To Java
    Replies: 11
    Last Post: 12-07-2010, 07:23 AM
  4. Anyone good at math?
    By iWonder in forum Forum Lobby
    Replies: 12
    Last Post: 01-11-2009, 05:12 PM
  5. need help with math for a new program
    By gotenks05 in forum New To Java
    Replies: 13
    Last Post: 09-26-2008, 08:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •