[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?
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.
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.
Re: Get the distance of two points on a coordinate grid.
Quote:
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.
Re: Get the distance of two points on a coordinate grid.
Quote:
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!
Re: Get the distance of two points on a coordinate grid.
Quote:
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.