Crib Sheets
Home Page
Calculator
Chemistry
Convert Units
Electronic
First Aid
Geography
Geology
Mathematics
Music
Nautical
Numbers
Physics
VB.net Programming
Dates Date Example Maths Text Numeric Textbox

Weather
Web Programming
Wiring
DigitalDan Websites
DigitalDan Sites
Hit Counter 232
 
©2000-2013 DigitalDan.co.uk
 
Contact Details
Email address scroller
DigitalDan.uk
VB.NET Mathematical Functions

 
The functions outlined below are written in VB.net but it should be possible to modify them for other computer languages.
' Test a SMALL number to see if it is a prime number
' Testing large numbers requires so much computer time
' that they are often used for computer encryption.

Public Function isItPrime(ByVal value As Integer) As Boolean
    Dim answer As Boolean = True
    Dim x As Integer
    value = Math.Abs(value)
    If value = 0 Then
        answer = False
    Else
        For x = 1 To Int(value / 2) + 1 Step 2
            If value Mod x = 0 Then
                answer = False
                Exit For
            End If
        Next
    End If
    Return answer
End Function


' A selection of trignometric functions ' Computer trignometric functions use Radians instead of Degrees ' Some of the functions in this list require other functions in the ' list to be present Public Function sinh(ByVal x As Double) As Double Return (Math.Pow(Math.E, x) - Math.Pow(Math.E, 0 - x)) / 2 End Function Public Function cosh(ByVal x As Double) As Double Return (Math.Pow(Math.E, x) + Math.Pow(Math.E, 0 - x)) / 2 End Function Public Function tanh(ByVal x As Double) As Double Return (sinh(x) / cosh(x)) End Function Public Function sec(ByVal angle As Double) As Double Return Math.Sqrt(1 + (Math.Tan(angle) ^ 2)) End Function Public Function cot(ByVal angle As Double) As Double Return 1 / (Math.Tan(angle)) End Function Public Function cosec(ByVal angle As Double) As Double Return Sqrt(1 + (cot(angle) ^ 2)) End Function Public Function verSine(num As Double) As Double Return 1 - Math.Cos(num) End Function Public Function verCosine(num As Double) As Double Return 1 + Math.Cos(num) End Function Public Function coVerSine(num As Double) As Double Return 1 - Math.Sin(num) End Function Public Function coVerCosine(num As Double) As Double Return 1 + Math.Sin(num) End Function Public Function sinc(num As Double) As Double If Math.Abs(num) < 0.000001 Then Return 1 ' prevents overflow error - sinc(0) = 1 Return Math.Sin(num) / num End Function Public Function tanc(num As Double) As Double If Math.Abs(num) < 0.000001 Then Return 1 ' prevents overflow error - tanc(0) = 1 Return Math.Tan(num) / num End Function
' converting Radians to Degrees Public Function degrees(ByVal value As Double) As Double Return value * 180 / Math.PI End Function ' Converting Degrees to Radians Public Function radians(ByVal value As Double) As Double Return value * Math.PI / 180 End Function
'Temperature conversion Public Function Farenheit2Celcius(ByVal value As Double) As Double Return ((5 * value) - 160) / 9 End Function Public Function Celcius2Farenheit(ByVal value As Double) As Double Return ((9 * value) + 160) / 5 End Function
' Convert number to English Text Public Function num2text(ByVal num As Integer) As String Dim unit, thousand, million As Integer Dim answer As String = "" If num > 999999999 Then num = 0 million = Int(num / 1000000) thousand = (Int(num / 1000) Mod 1000) unit = num Mod 1000 If million > 0 Then answer &= thousand2text(million) & " million" If thousand > 0 Then answer &= "," answer &= " " If thousand > 0 Then answer &= thousand2text(thousand) & " thousand" If unit > 0 Then answer &= "," answer &= " " If unit > 0 Then answer &= thousand2text(unit) If num = 0 Then answer &= " zero" answer = Replace(answer, " ", " ") answer = Trim(answer) Return answer End Function Private Function thousand2text(ByVal n As Integer) As String Dim hundred As Integer = Int(n / 100) Dim tens As Integer = n Mod 100 Dim words(20) As String Dim ans As String = "" Dim wordunits() As String = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"} Dim wordtens() As String = {"zero", "ten", "twenty", "thirty", "fourty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"} If hundred > 0 Then ans = " " & wordunits(hundred) & " " ans &= " hundred " If tens > 0 Then ans = " and " If tens < 20 Then ans &= wordunits(tens) Else ans = wordtens(Int(tens / 10)) & " " If tens Mod 10 > 0 Then ans = wordunits(tens Mod 10) & " " End If End If End If Return ans End Function
' Convert a number to Roman Numerals Public Function num2Roman(ByVal number As Integer, Optional ByVal lowercase As Boolean = False) As String Dim arabic(12) As String Dim roman(12) As String Dim i As Integer, result As String arabic = Split("1000,900,500,400,100,90,50,40,10,9,5,4,1", ",", 13) roman = Split("M,CM,D,CD,C,XC,L,XL,X,IX,V,IV,I", ",", 13) result = "" For i = 0 To 12 Do While number >= Val(arabic(i)) result = result + roman(i) number -= Val(arabic(i)) Loop Next i If lowercase Then result = result.ToLower Return result End Function ' Convert Roman Numbers to a number Public Function roman2num(roman As String) As Integer Dim valid As Boolean = False Dim romanset As String = "ivxlcdm" roman = roman.Replace(" ", "").ToLower If roman = "" Then Return 0 For i As Integer = 1 To Len(roman) If Not romanset.Contains(Mid(roman, i, 1)) Then valid = False Next If roman.Contains("iiii") Or roman.Contains("vv") Or roman.Contains("xxxx") Or roman.Contains("ll") Or roman.Contains("cccc") Or roman.Contains("dd") Or roman.Contains("mmmm") Then valid = False End If If Not valid Then Return 0 Dim tot As Integer = 0 Dim pre As Integer = -999 Dim cur As Integer = 0 For i As Integer = Len(roman) To 1 Step -1 cur = valu(Mid$(roman, i, 1)) If cur < pre Then tot -= cur Else tot += +cur End If pre = cur Next Return tot End Function Private Function valu(c As Char) As Integer If c = "i" Then Return 1 If c = "v" Then Return 5 If c = "x" Then Return 10 If c = "l" Then Return 50 If c = "c" Then Return 100 If c = "d" Then Return 500 If c = "m" Then Return 1000 Return 0 End Function
DigitalDan.uk is part of the DigitalDan.co.uk group