i have a problem with the function of a square root. anyone here who knows how to decode a square function into a VB codes would be grealy appreciated
Printable View
i have a problem with the function of a square root. anyone here who knows how to decode a square function into a VB codes would be grealy appreciated
can u elaborate?
VB Code:
Private Sub Form_Load() MsgBox SquareRoot(9) End Sub Private Function SquareRoot(ByVal n As Long) As Single SquareRoot = n ^ (1 / 2) End Function
There is also a built in function in VB that will return a Square Root. It is SQR. ExampleVB Code:
MsgBox Sqr(4)
Ya know, I didn't know that actually!
Is that what he wanted? :confused:
:D Me either until I did web search on Square Root and the first flippin' hit I got told me about that VB function. LOL :rolleyes:Quote:
Originally posted by plenderj
Ya know, I didn't know that actually!
So, I looked it up in VB Help, and there it was.
PS: plenderj congrats on 7k :D
Cheers.
I'll be congratulating you on 10k soon!
... should send megatron a pm too about that :)
Wow, and I found out about it in the 3rd chapter of the first VB-programming book I ever read. (72 chapters in total... Mostly about 5 pages per chap. so no need to go :eek: )Quote:
Originally posted by Hack
:D Me either until I did web search on Square Root and the first flippin' hit I got told me about that VB function. LOL :rolleyes:
So, I looked it up in VB Help, and there it was.
PS: plenderj congrats on 7k :D
Do I take it that my question was answered in the positive? ;)
Guess so.
silly me!:rolleyes: I was busy porting an old Babylonian Series to generate the square root the hard way!:rolleyes:
For sq.rooting with other exponents:
VB Code:
Public Function Root(Exponent As Integer, Number As Long) As Single Root = Number ^ (1 / Exponent) End Function
Well, for what its worth :)
The above uses no exponentiation. only add, sub, mul, and div.VB Code:
Private Function SquareRoot(thenumber As Double) Dim theVals() As Double ReDim theVals(1 To thenumber) theVals(1) = (1 / 2) + (k / 2) For i = 2 To thenumber theVals(i) = (theVals(i - 1) / 2) + (thenumber / (2 * theVals(i - 1))) If theVals(i) = theVals(i - 1) Then SquareRoot = theVals(i) Exit Function End If Next i End Function Private Sub Command1_Click() MsgBox Round(SquareRoot(256), 16) & vbCrLf & vbCrLf & Round(Sqr(256), 16) MsgBox Round(SquareRoot(10000), 16) & vbCrLf & vbCrLf & Round(Sqr(10000), 16) MsgBox Round(SquareRoot(123456), 16) & vbCrLf & vbCrLf & Round(Sqr(123456), 16) MsgBox Round(SquareRoot(123.456), 16) & vbCrLf & vbCrLf & Round(Sqr(123.456), 16) End Sub