|
-
Sep 5th, 2009, 03:49 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Get the distance of two points on a coordinate grid.
I'm sure some of you know the equation to get the distance of 2 points on a coordinate grid, and I'm making a program to do just that, the only problem is that when I use the square root function it doesn't return an exact number, what's wrong with my code?
vb Code:
Dim P1 As Integer = CInt(X2.Text) - CInt(X1.Text)
Dim P2 As Integer = CInt(Y2.Text) - CInt(Y1.Text)
Dim XX1 As Integer = P1 * P1
Dim YY1 As Integer = P2 * P2
Dim W1 As Integer = XX1 + YY1
Dim Sq As Integer = System.Math.Sqrt(W1)
Sol.Text = Sq
I don't get why it automatically rounds the number up. Can anyone tell me how to code it to get an exact square root?
-
Sep 5th, 2009, 03:58 PM
#2
Re: Get the distance of two points on a coordinate grid.
Do you really want it to return an integer? By its very definition this will round the answer.
I would have thought you ought to be using a double as this is what the Sqrt function returns.
-
Sep 5th, 2009, 04:20 PM
#3
Re: Get the distance of two points on a coordinate grid.
Turn Option Strict On (write exactly those three words at the top of your code file) and you will see errors like this more easily.
-
Sep 5th, 2009, 04:32 PM
#4
Thread Starter
Hyperactive Member
Re: Get the distance of two points on a coordinate grid.
 Originally Posted by keystone_paul
Do you really want it to return an integer? By its very definition this will round the answer.
I would have thought you ought to be using a double as this is what the Sqrt function returns.
Yeah, I figured that out after a while, but thanks anyway.
-
Sep 5th, 2009, 04:36 PM
#5
Re: Get the distance of two points on a coordinate grid.
 Originally Posted by DerekM
Yeah, I figured that out after a while, but thanks anyway.
Glad you figured it out, but it would be helpful if you'd marked your thread "resolved" using the thread tools menu, so people realise you don't need any help and don't waste their breath!
-
Sep 5th, 2009, 04:37 PM
#6
Thread Starter
Hyperactive Member
Re: Get the distance of two points on a coordinate grid.
 Originally Posted by keystone_paul
Glad you figured it out, but it would be helpful if you'd marked your thread "resolved" using the thread tools menu, so people realise you don't need any help and don't waste their breath!
Yeah, it's been awhile since I've been on the forums, I tend to forget simple thing like that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|