Attribute VB_Name = "Math2" 'Shift Bits to Left (0001 = 0010) Public Function LSHIFT(ByVal number As Long, ShiftBits As Integer) As Long If ShiftBits > 31 Or ShiftBits <= 0 Then If ShiftBits = 0 Then LSHIFT = number Return Else LSHIFT = 0 Return End If End If LSHIFT = number * 2 ^ ShiftBits End Function 'Shift Bits to Right (0010 = 0001) Public Function RSHIFT(ByVal number As Long, ShiftBits As Integer) As Long If ShiftBits > 31 Or ShiftBits <= 0 Then If ShiftBits = 0 Then RSHIFT = number Return Else RSHIFT = 0 Return End If End If RSHIFT = number \ 2 ^ ShiftBits End Function Public Function POW(ByVal number As Double, ByVal p As Integer) As Double Dim i As Integer If p <= 0 Then POW = 0 Else POW = number If p > 1 Then For i = 2 To p POW = POW * number Next End If End If End Function Public Function Min(a As Variant, b As Variant) As Variant If a < b Then Min = a Else Min = b End Function Public Function Max(a As Variant, b As Variant) As Variant If a > b Then Max = a Else Max = b End Function 'Simplifies a fraction where you give the numerator and denominator. 'Make sure there are no decimals in the numberator/denominator! 'Example: mystring$ = Math_Simplify(30, 10) - will return 3/1 as the answer in mystring$ 'Example 2: mystring2$ = Math_Simplify(3.2, 4.6) - will not return an answer. Rather, make it Math_Simplify(32, 46) and an answer will be given Public Function Math_Simplify(numerator As Double, denominator As Double) As String If Math_IsInteger(numerator) Then If Math_IsInteger(denominator) Then n = numerator d = denominator x = Math_GCF(Val(n), Val(d)) n = n / Val(x) d = d / Val(x) Math_Simplify = Trim(str(n)) + "/" + Trim(str(d)) End If End If End Function 'This gets the rooth root of a number. The rooth root is always positive in this case. Although -3 * -3 = 3 * 3, which are both 9, it will only give 3 as the answer. 'If the number is 9, and you put in a root of 2, it will get the positive square root of 9 = 3. 'Example: Math_Root 2, 2 - gets the square root of 2. 'Note: Do not make 0 the root - it will be number ^ infinity! Public Function Math_Root(number As Double, root As Double) As Double If root > 0 Then Math_Root = number ^ (1 / root) End If End Function 'Determines if a number is odd or not 'Note: This determines if a negative number (< 0) is odd. If the absolute value is odd, then the negative number is also classified as odd... Public Function Math_IsOdd(number As Double) As Boolean Math_IsOdd = (Int(Abs(number)) And 1) End Function 'Solves the equation a ^ x + b = c for x when the value for a and b are given 'Example: Math_GetExponent(3, 0, 5) - returns the value 1.464.... The equation is 3^x + 0 = 5. 'Example: Math_GetExponent(2, 1, 2) - solves the equation 2^x + 1 = 2. The answer is 0. Public Function Math_GetExponent(a As Double, b As Double, c As Double) As Double c = c - b Math_GetExponent = Log(c) / Log(a) End Function 'Solves the equation the x root of a + b = c for x, or x | a + b = c where | = the root sign. 'Example: Math_GetRoot(9, 0, 3) - returns a value of 2, since the square root of 9 is 3. Solves x | 9 + 0 = 3. 'Example: Math_GetRoot(4, 1, 5) - solves x | 4 + 1 = 5. In this case the answer is 1. Public Function Math_GetRoot(a As Double, b As Double, c As Double) As Double c = c - b Math_GetRoot = Log(a) / Log(c) End Function Public Function Math_IsInteger(obj As Variant) As Boolean Math_IsInteger = (CDbl(Val(obj)) = CInt(Val(obj))) End Function 'Gets the greatest common factor between two numbers: n1 and n2. Can take a few seconds of time. 'Make sure n1 and n2 are positive nonzero numbers; it will not work otherwise! 'Example: theGCF = Math_GCF(4, 200) - will return 4 as the GCF 'Example: theGCF = Math_GCF(8, 12) - will return 4 as the GCF Public Function Math_GCF(number1 As Double, number2 As Double) As Double Dim i As Long For i = 1 To CLng(IIf(number1 <= number2, number1, number2)) DoEvents If Math_IsInteger(Math_LCM(number1, number2) / i) Then If Math_IsInteger(number1 / i) Then If Math_IsInteger(number2 / i) Then Math_GCF = i End If End If End If Next End Function 'Gets the least common multiple between two numbers. 'Example: Call Math_LCM(3, 5) - will return 15. 'Example: Math_LCM(6, 4) - will return 12. Public Function Math_LCM(n1 As Double, n2 As Double) As Double Math_LCM = n1 * n2 Dim x As Long For x = 2 To Math_LCM If Math_IsInteger(x / n1) Then If Math_IsInteger(x / n2) Then Math_LCM = n1 / x End If End If Next End Function ' Secant Function Sec(x As Double) As Double Sec = 1 / Cos(x) End Function ' Cosecant Function CoSec(x As Double) As Double CoSec = 1 / Sin(x) End Function ' Cotangent Function CoTan(x As Double) As Double CoTan = 1 / Tan(x) End Function ' Inverse Sine Function ArcSin(x As Double) As Double ArcSin = Atn(x / Sqr(-x * x + 1)) End Function ' Inverse Cosine Function ArcCos(x As Double) As Double ArcCos = Atn(-x / Sqr(-x * x + 1)) + 2 * Atn(1) End Function ' Inverse Secant Function ArcSec(x As Double) As Double ArcSec = Atn(x / Sqr(x * x - 1)) + Sgn(x - 1) * (2 * Atn(1)) End Function ' Inverse Cosecant Function ArcCoSec(x As Double) As Double ArcCoSec = Atn(x / Sqr(x * x - 1)) + (Sgn(x) - 1) * (2 * Atn(1)) End Function ' Inverse Cotangent Function ArcCoTan(x As Double) As Double ArcCoTan = Atn(x) + 2 * Atn(1) End Function ' Hyperbolic Sine Function HSin(x As Double) As Double HSin = (Exp(x) - Exp(-x)) / 2 End Function ' Hyperbolic Cosine Function HCos(x As Double) As Double HCos = (Exp(x) + Exp(-x)) / 2 End Function ' Hyperbolic Tangent Function HTan(x As Double) As Double HTan = (Exp(x) - Exp(-x)) / (Exp(x) + Exp(-x)) End Function ' Hyperbolic Secant Function HSec(x As Double) As Double HSec = 2 / (Exp(x) + Exp(-x)) End Function ' Hyperbolic Cosecant Function HCoSec(x As Double) As Double HCoSec = 2 / (Exp(x) - Exp(-x)) End Function ' Hyperbolic Cotangent Function HCotan(x As Double) As Double HCotan = (Exp(x) + Exp(-x)) / (Exp(x) - Exp(-x)) End Function ' Inverse Hyperbolic Sine Function HArcSin(x As Double) As Double HArcSin = Log(x + Sqr(x * x + 1)) End Function ' Inverse Hyperbolic Cosine Function HArcCos(x As Double) As Double HArcCos = Log(x + Sqr(x * x - 1)) End Function ' Inverse Hyperbolic Tangent Function HArcTan(x As Double) As Double HArcTan = Log((1 + x) / (1 - x)) / 2 End Function ' Inverse Hyperbolic Secant Function HArcSec(x As Double) As Double HArcSec = Log((Sqr(-x * x + 1) + 1) / x) End Function ' Inverse Hyperbolic Cosecant Function HArcCoSec(x As Double) As Double HArcCoSec = Log((Sgn(x) * Sqr(x * x + 1) + 1) / x) End Function ' Inverse Hyperbolic Cotangent Function HArcCoTan(x As Double) As Double HArcCoTan = Log((x + 1) / (x - 1)) / 2 End Function ' Logarithm to base N Function LogN(x As Double, n As Double) As Double LogN = Log(x) / Log(n) End Function