Results 1 to 5 of 5

Thread: VB - Some new Math Functions

  1. #1

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    VB - Some new Math Functions

    I love Maths, in a calculator script I wrote a while ago I used these, someone may find them useful

    General Functions:

    Name: Add a number range

    VB Code:
    1. Private Function AddNumberRange(ByVal lngStart As Long, ByVal lngFinish As Long) As Long
    2.     AddNums = ((((lngFinish^ 2) - ((lngStart ^ 2)) + lngFinish + (lngStart) / 2)
    3. End Function

    Example: AddNums(1, 100) ' 5050

    -----------------------------------------------------------------------------
    Name: Compute Nth root of a number

    VB Code:
    1. Private Function NthRoot(ByVal lngRoot, ByVal lngNumber) As Double
    2.     NthRoot = (lngNumber ^ (1 / lngRoot))
    3. End Function

    Example: NthRoot(13, 8192) ' Prints 2 (13th root of 8192)

    -----------------------------------------------------------------------------
    Name: Convert Radians to Degrees:

    VB Code:
    1. Private Function Rad2Deg(ByVal dblRadians As Double) As Double
    2.     Rad2Deg = dblRadians * 180 / 3.14159265358979323846
    3. End Function

    -----------------------------------------------------------------------------
    Name: Convert Degrees to Radians:

    VB Code:
    1. Private Function Deg2Rad(ByVal dblDegrees As Double) As Double
    2.     Deg2Rad = dblDegrees * 3.14159265358979323846 / 180
    3. End Function

    -----------------------------------------------------------------------------
    Name: Return Fractional Part Of a Number

    VB Code:
    1. Private Function ReturnFraction(ByVal dblNumber As Double) As Double
    2.     Dim intTempVal As Integer
    3.     intTempVal = dblNumber \ 1
    4.     ReturnFraction = dblNumber - intTempVal
    5. End Function

    Example: ReturnFraction(1.2345) ' Returns 0.2345

    -----------------------------------------------------------------------------
    Name: Return a factorial of a number.

    VB Code:
    1. Private Function Factorial(ByVal intNumber As Integer) As Long
    2.     If intNumber <> 1 Then
    3.         Factorial = (intNumber * Factorial(intNumber - 1))
    4.     Else
    5.         Factorial = 1
    6.     End If
    7. End Function

    Example: Factorial(5) ' Returns 120, 5*4*3*2*1 = 120

    --------------------------------------------------------------------------

    Cheers and hope those help someone

    RyanJ
    Last edited by sciguyryan; May 25th, 2005 at 02:43 PM.
    My Blog.

    Ryan Jones.

  2. #2

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: VB - Some new Math Functions

    The post was too big, I had to split it to maintain readability.

    Trig Functions:

    -----------------------------------------------------------------------------
    Name: Hyperbolic Sine

    VB Code:
    1. Private Function Sinh(ByVal dblNumber As Double) As Double
    2.     Sinh = (Exp(dblNumber) - Exp(dblNumber * -1)) / 2
    3. End Function

    Example: Sinh(0.707) ' ~ 0.767388542

    -----------------------------------------------------------------------------
    Name: Hyperbolic Cosine

    VB Code:
    1. Private Function Cosh(ByVal dblNumber As Double) As Double
    2.     Cosh = (Exp((dblNumber) + Exp((dblNumber * -1)) / 2
    3. End Function

    Example: Cosh(0.707) ' ~ 1.260509887

    -----------------------------------------------------------------------------
    Name: Hyperbolic Tangent

    VB Code:
    1. Private Function Tanh(ByVal dblNumber As Double) As Double
    2.     Tanh = ((Exp(dblNumber) - Exp(dblNumber * -1)) / 2) / ((Exp(dblNumber) + Exp(dblNumber * -1)) / 2)
    3. End Function

    Example: Tanh(0.707) ' ~ 0.608792164

    -----------------------------------------------------------------------------
    Name: Cotangent of an angle

    VB Code:
    1. Private Function Cot(ByVal dblRadians As Double) As Double
    2.     Cot = (1 / Tan(dblRadians))
    3. End Function

    Example: None

    -----------------------------------------------------------------------------
    Name: Secant of an angle

    VB Code:
    1. Private Function Sec(ByVal dblRadiansAs Double) As Double
    2.     Sec = (1 / Cos(dblRadians))
    3. End Function

    Example: None
    -----------------------------------------------------------------------------
    Name: Cosecant of an angle

    VB Code:
    1. Private Function Csc(ByVal dblRadians As Double) As Double
    2.     Csc = 1 / Sin(dblRadians)
    3. End Function

    Example: None

    -----------------------------------------------------------------------------

    Some New Ones, Added 25-05-05 8:40PM

    -----------------------------------------------------------------------------
    Name: Arc sine

    VB Code:
    1. Private Function ASin(ByVal dblNumber As Double) As Double
    2.     If Abs(value) <> 1 Then
    3.         ASin = Atn(dblNumber / Sqr(1 - dblNumber * dblNumber))
    4.     Else
    5.         ASin = 1.5707963267949 * Sgn(dblNumber)
    6.     End If
    7. End Function

    Example: None

    -----------------------------------------------------------------------------
    Name: Arc cosine

    VB Code:
    1. Private Function ACos(ByVal dblNumber As Double) As Double
    2.     If Abs(dblNumber) <> 1 Then
    3.         ACos = 1.5707963267949 - Atn(dblNumber / Sqr(1 - dblNumber * dblNumber))
    4.     ElseIf number = -1 Then
    5.         ACos = 3.14159265358979
    6.     End If
    7. End Function

    Example: None

    -----------------------------------------------------------------------------
    Name: Arc cotangent

    VB Code:
    1. Private Function ACot(ByVal dblNumber As Double) As Double
    2.     ACot = Atn(1 / dblNumber)
    3. End Function

    Example: None

    -----------------------------------------------------------------------------
    Name: Arc secant

    VB Code:
    1. Private Function ASec(ByVal dblNumber As Double) As Double
    2.     If Abs(value) <> 1 Then
    3.         ASec = 1.5707963267949 - Atn((1 / dblNumber / Sqr(1 - 1 / (dblNumber * dblNumber)))
    4.     Else
    5.         ASec = 3.14159265358979 * Sgn(dblNumber)
    6.     End If
    7. End Function

    Example: None

    -----------------------------------------------------------------------------
    Name: Arc cosecant

    VB Code:
    1. Private Function ACsc(ByVal dblNumber As Double) As Double
    2.     If Abs(dblNumber) <> 1 Then
    3.         ACsc = Atn((1 / dblNumber) / Sqr(1 - 1 / (dblNumber * dblNumber)))
    4.     Else
    5.         ACsc = 1.5707963267949 * Sgn(dblNumber)
    6.     End If
    7. End Function

    Example: None

    -----------------------------------------------------------------------------

    Edit: OOPs, forgot my ByVal's

    Cheers,

    RyanJ
    Last edited by sciguyryan; May 25th, 2005 at 02:49 PM.
    My Blog.

    Ryan Jones.

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: VB - Some new Math Functions

    Nice functions...

    Here's my favourite
    VB Code:
    1. Private Function GetPI() As Double
    2.     GetPI = 4 * Atn(1)
    3. End Function
    I don't actually use this function, but usually when I need the PI value, I just type this in the Immediate window "Print 4 * Atn(1)" to get the PI value...

  4. #4

    Thread Starter
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: VB - Some new Math Functions

    Quote Originally Posted by CVMichael
    Nice functions...

    Here's my favourite
    VB Code:
    1. Private Function GetPI() As Double
    2.     GetPI = 4 * Atn(1)
    3. End Function
    I don't actually use this function, but usually when I need the PI value, I just type this in the Immediate window "Print 4 * Atn(1)" to get the PI value...

    Thanks

    How could I forget PI? I love that one too

    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  5. #5
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: VB - Some new Math Functions

    sciguyryan,
    can you edit your post so that asin and asec has a uses dblnumber rather than value in the code, caught me out but thanks for this very useful post.

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