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

This code allows you to calculate Sin(x), e^(x), and Pi without using any of the built-in VB functions

Original Author: Jonathon Lopez

### Assumptions

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.

### Code

`'Sin(x) function'Note: this is in radians, not degreesPublic Function Sine(x as Double) as DoubleDim i As Integer, sum As Double: sum = 0'Calculate the taylor expansion of sinFor i = 1 To 10  sum = sum + (((-1) ^ (i + 1)) * ((x) ^ (2 * i - 1)) / fact(2 * i - 1))Next iSine=sumEnd Function'e^(x) functionPublic Function e(x as Integer) as DoubleDim i As Integer, sum As Double: sum = 0'Calculate the Taylor expansion of eFor i = 0 To 150  sum = sum + (x ^ i) / fact(i)Next ie=sumEnd Function'Pi functionPublic Function pi() as DoubleDim i As Integer, sum As Double: sum = 0For i = 1 To 15000  sum = sum + ((-1) ^ (i + 1)) * (1 ^ (2 * i - 1)) / (2 * i - 1)Next ipi = sum * 4End Function'Function that calculates factorialsPublic Function fact(n As Integer) As DoubleDim i As Long, r As Double: r = 1If n = 0 Then fact = 1For i = 1 To n  r = i * rNext ifact = rEnd Function`

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 106 times

Categories

Visual Basic 6

Attachments

No attachments for this post

Loading Comments ...

## Comments

No comments have been added for this post.

You must be logged in to make a comment.