Results 1 to 14 of 14

Thread: problem with the function(square root)

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    tacloban
    Posts
    1

    problem with the function(square root)

    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

  2. #2
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    can u elaborate?

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  3. #3
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    VB Code:
    1. Private Sub Form_Load()
    2.     MsgBox SquareRoot(9)
    3. End Sub
    4.  
    5.  
    6. Private Function SquareRoot(ByVal n As Long) As Single
    7.     SquareRoot = n ^ (1 / 2)
    8. End Function
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    There is also a built in function in VB that will return a Square Root. It is SQR. Example
    VB Code:
    1. MsgBox Sqr(4)

  5. #5
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Ya know, I didn't know that actually!
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  6. #6
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Is that what he wanted?

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  7. #7
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Originally posted by plenderj
    Ya know, I didn't know that actually!
    Me either until I did web search on Square Root and the first flippin' hit I got told me about that VB function. LOL

    So, I looked it up in VB Help, and there it was.

    PS: plenderj congrats on 7k

  8. #8
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Cheers.
    I'll be congratulating you on 10k soon!

    ... should send megatron a pm too about that
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  9. #9
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    Originally posted by Hack
    Me either until I did web search on Square Root and the first flippin' hit I got told me about that VB function. LOL

    So, I looked it up in VB Help, and there it was.

    PS: plenderj congrats on 7k
    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 )
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  10. #10
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Do I take it that my question was answered in the positive?

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  11. #11
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    Guess so.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  12. #12
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    silly me! I was busy porting an old Babylonian Series to generate the square root the hard way!

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  13. #13
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    For sq.rooting with other exponents:
    VB Code:
    1. Public Function Root(Exponent As Integer, Number As Long) As Single
    2.     Root = Number ^ (1 / Exponent)
    3. End Function
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  14. #14
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Well, for what its worth
    VB Code:
    1. Private Function SquareRoot(thenumber As Double)
    2. Dim theVals() As Double
    3. ReDim theVals(1 To thenumber)
    4. theVals(1) = (1 / 2) + (k / 2)
    5. For i = 2 To thenumber
    6.     theVals(i) = (theVals(i - 1) / 2) + (thenumber / (2 * theVals(i - 1)))
    7.     If theVals(i) = theVals(i - 1) Then
    8.         SquareRoot = theVals(i)
    9.         Exit Function
    10.     End If
    11. Next i
    12. End Function
    13.  
    14. Private Sub Command1_Click()
    15. MsgBox Round(SquareRoot(256), 16) & vbCrLf & vbCrLf & Round(Sqr(256), 16)
    16. MsgBox Round(SquareRoot(10000), 16) & vbCrLf & vbCrLf & Round(Sqr(10000), 16)
    17. MsgBox Round(SquareRoot(123456), 16) & vbCrLf & vbCrLf & Round(Sqr(123456), 16)
    18. MsgBox Round(SquareRoot(123.456), 16) & vbCrLf & vbCrLf & Round(Sqr(123.456), 16)
    19. End Sub
    The above uses no exponentiation. only add, sub, mul, and div.

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

Posting Permissions

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



Click Here to Expand Forum to Full Width