VB icon

Calculate Sin(x), e^(x) , and Pi

Email
Submitted on: 1/16/2015 6:38:00 AM
By: Jonathon Lopez (from psc cd)  
Level: Intermediate
User Rating: By 3 Users
Compatibility: VB 5.0
Views: 932
 
     This code allows you to calculate Sin(x), e^(x), and Pi without using any of the built-in VB functions
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
'**************************************
' Name: Calculate Sin(x), e^(x) , and Pi
' Description:This code allows you to calculate Sin(x), e^(x), and Pi without using any of the built-in VB functions
' By: Jonathon Lopez (from psc cd)
'
' Assumes:The way this code works is by using what is called Taylor Expansion. By generating a taylor series using derivatives it is possible to take the sum of the elements in that series to find such functions as Sin(x) and even Pi.
'**************************************

'Sin(x) function
'Note: this is in radians, not degrees
Public Function Sine(x as Double) as Double
Dim i As Integer, sum As Double: sum = 0
'Calculate the taylor expansion of sin
For i = 1 To 10
sum = sum + (((-1) ^ (i + 1)) * ((x) ^ (2 * i - 1)) / fact(2 * i - 1))
Next i
Sine=sum
End Function
'e^(x) function
Public Function e(x as Integer) as Double
Dim i As Integer, sum As Double: sum = 0
'Calculate the Taylor expansion of e
For i = 0 To 150
sum = sum + (x ^ i) / fact(i)
Next i
e=sum
End Function
'Pi function
Public Function pi() as Double
Dim i As Integer, sum As Double: sum = 0
For i = 1 To 15000
sum = sum + ((-1) ^ (i + 1)) * (1 ^ (2 * i - 1)) / (2 * i - 1)
Next i
pi = sum * 4
End Function
'Function that calculates factorials
Public Function fact(n As Integer) As Double
Dim i As Long, r As Double: r = 1
If n = 0 Then fact = 1
For i = 1 To n
r = i * r
Next i
fact = r
End Function


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.