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:
Private Function AddNumberRange(ByVal lngStart As Long, ByVal lngFinish As Long) As Long
AddNums = ((((lngFinish^ 2) - ((lngStart ^ 2)) + lngFinish + (lngStart) / 2)
End Function
Example: AddNums(1, 100) ' 5050
-----------------------------------------------------------------------------
Name: Compute Nth root of a number
VB Code:
Private Function NthRoot(ByVal lngRoot, ByVal lngNumber) As Double
NthRoot = (lngNumber ^ (1 / lngRoot))
End Function
Example: NthRoot(13, 8192) ' Prints 2 (13th root of 8192)
-----------------------------------------------------------------------------
Name: Convert Radians to Degrees:
VB Code:
Private Function Rad2Deg(ByVal dblRadians As Double) As Double
Rad2Deg = dblRadians * 180 / 3.14159265358979323846
End Function
-----------------------------------------------------------------------------
Name: Convert Degrees to Radians:
VB Code:
Private Function Deg2Rad(ByVal dblDegrees As Double) As Double
Deg2Rad = dblDegrees * 3.14159265358979323846 / 180
End Function
-----------------------------------------------------------------------------
Name: Return Fractional Part Of a Number
VB Code:
Private Function ReturnFraction(ByVal dblNumber As Double) As Double
Dim intTempVal As Integer
intTempVal = dblNumber \ 1
ReturnFraction = dblNumber - intTempVal
End Function
Example: ReturnFraction(1.2345) ' Returns 0.2345
-----------------------------------------------------------------------------
Name: Return a factorial of a number.
VB Code:
Private Function Factorial(ByVal intNumber As Integer) As Long
If intNumber <> 1 Then
Factorial = (intNumber * Factorial(intNumber - 1))
Else
Factorial = 1
End If
End Function
Example: Factorial(5) ' Returns 120, 5*4*3*2*1 = 120
--------------------------------------------------------------------------
Cheers and hope those help someone :)
RyanJ
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:
Private Function Sinh(ByVal dblNumber As Double) As Double
Sinh = (Exp(dblNumber) - Exp(dblNumber * -1)) / 2
End Function
Example: Sinh(0.707) ' ~ 0.767388542
-----------------------------------------------------------------------------
Name: Hyperbolic Cosine
VB Code:
Private Function Cosh(ByVal dblNumber As Double) As Double
Cosh = (Exp((dblNumber) + Exp((dblNumber * -1)) / 2
End Function
Example: Cosh(0.707) ' ~ 1.260509887
-----------------------------------------------------------------------------
Name: Hyperbolic Tangent
VB Code:
Private Function Tanh(ByVal dblNumber As Double) As Double
Tanh = ((Exp(dblNumber) - Exp(dblNumber * -1)) / 2) / ((Exp(dblNumber) + Exp(dblNumber * -1)) / 2)
End Function
Example: Tanh(0.707) ' ~ 0.608792164
-----------------------------------------------------------------------------
Name: Cotangent of an angle
VB Code:
Private Function Cot(ByVal dblRadians As Double) As Double
Cot = (1 / Tan(dblRadians))
End Function
Example: None
-----------------------------------------------------------------------------
Name: Secant of an angle
VB Code:
Private Function Sec(ByVal dblRadiansAs Double) As Double
Sec = (1 / Cos(dblRadians))
End Function
Example: None
-----------------------------------------------------------------------------
Name: Cosecant of an angle
VB Code:
Private Function Csc(ByVal dblRadians As Double) As Double
Csc = 1 / Sin(dblRadians)
End Function
Example: None
-----------------------------------------------------------------------------
Some New Ones, Added 25-05-05 8:40PM
-----------------------------------------------------------------------------
Name: Arc sine
VB Code:
Private Function ASin(ByVal dblNumber As Double) As Double
If Abs(value) <> 1 Then
ASin = Atn(dblNumber / Sqr(1 - dblNumber * dblNumber))
Else
ASin = 1.5707963267949 * Sgn(dblNumber)
End If
End Function
Example: None
-----------------------------------------------------------------------------
Name: Arc cosine
VB Code:
Private Function ACos(ByVal dblNumber As Double) As Double
If Abs(dblNumber) <> 1 Then
ACos = 1.5707963267949 - Atn(dblNumber / Sqr(1 - dblNumber * dblNumber))
ElseIf number = -1 Then
ACos = 3.14159265358979
End If
End Function
Example: None
-----------------------------------------------------------------------------
Name: Arc cotangent
VB Code:
Private Function ACot(ByVal dblNumber As Double) As Double
ACot = Atn(1 / dblNumber)
End Function
Example: None
-----------------------------------------------------------------------------
Name: Arc secant
VB Code:
Private Function ASec(ByVal dblNumber As Double) As Double
If Abs(value) <> 1 Then
ASec = 1.5707963267949 - Atn((1 / dblNumber / Sqr(1 - 1 / (dblNumber * dblNumber)))
Else
ASec = 3.14159265358979 * Sgn(dblNumber)
End If
End Function
Example: None
-----------------------------------------------------------------------------
Name: Arc cosecant
VB Code:
Private Function ACsc(ByVal dblNumber As Double) As Double
If Abs(dblNumber) <> 1 Then
ACsc = Atn((1 / dblNumber) / Sqr(1 - 1 / (dblNumber * dblNumber)))
Else
ACsc = 1.5707963267949 * Sgn(dblNumber)
End If
End Function
Example: None
-----------------------------------------------------------------------------
Edit: OOPs, forgot my ByVal's ;)
Cheers,
RyanJ
Re: VB - Some new Math Functions
Nice functions... :thumb:
Here's my favourite :)
VB Code:
Private Function GetPI() As Double
GetPI = 4 * Atn(1)
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...
Re: VB - Some new Math Functions
Quote:
Originally Posted by CVMichael
Nice functions... :thumb:
Here's my favourite :)
VB Code:
Private Function GetPI() As Double
GetPI = 4 * Atn(1)
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
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.