Results 1 to 11 of 11
Thread: Math issue
 12052011, 11:45 PM #1Member
 Join Date
 Dec 2011
 Posts
 7
 Rep Power
 0
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);
 12052011, 11:50 PM #2
Re: Math issue
What is the difference between integer and floating point division?
 12062011, 12:01 AM #3Member
 Join Date
 Dec 2011
 Posts
 7
 Rep Power
 0
 12062011, 12:14 AM #4
Re: Math issue
Java Code:System.out.println(2 / 3); System.out.println(2.0 / 3);
 12062011, 12:20 AM #5Member
 Join Date
 Dec 2011
 Posts
 7
 Rep Power
 0
Re: Math issue
I get 0 for the first line and 0.6666666666666666 for the second, so what am I doing wrong?
 12062011, 12:22 AM #6
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.
 12062011, 12:32 AM #7Member
 Join Date
 Dec 2011
 Posts
 7
 Rep Power
 0
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?
 12062011, 01:09 AM #8Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
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.
 12062011, 01:15 AM #9Member
 Join Date
 Dec 2011
 Posts
 7
 Rep Power
 0
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
 12062011, 01:38 AM #10Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,708
 Rep Power
 13
Re: Math issue
Double post: Java Maths Program  Java  Dream.In.Code
 12062011, 06:58 AM #11Member
 Join Date
 Nov 2011
 Posts
 10
 Rep Power
 0
Similar Threads

math and GUI
By urbanleg in forum AWT / SwingReplies: 3Last Post: 08062011, 04:05 PM 
Math.cos()
By ravi1 in forum New To JavaReplies: 5Last Post: 03272011, 02:52 PM 
Create Math.sin without math.sin
By vudoo in forum New To JavaReplies: 11Last Post: 12072010, 07:23 AM 
Anyone good at math?
By iWonder in forum Forum LobbyReplies: 12Last Post: 01112009, 05:12 PM 
need help with math for a new program
By gotenks05 in forum New To JavaReplies: 13Last Post: 09262008, 07:32 AM
Bookmarks