Results 1 to 5 of 5

Thread: Sin Function

  1. #1
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 11
    Location
    South Africa
    Posts
    865

    Sin Function

    Hi all,

    If you have ever wondered how the sin function works then this function should explain nicely.

    It uses some standard reduction formulas and a Taylor series approximation of the sin function of 4 terms.(if you had highschool math this should make sense)
    The difference between this function and Math.sin is usually smaller than 0.0000001

    The sin Function is where the reduction formulas are contained and the sin_ex is where the taylor series approximation is. You can add more terms to make it more accurate but the difference will be extremely small.

    vb.net Code:
    1. Const pi As Double = 3.1415926535897931
    2.     Function Sin(ByVal x As Double) As Double 'valid for x > 0
    3.         x = x Mod (2 * pi)
    4.         If x < (pi / 2) And x > -(pi / 2) Then
    5.             Return Sin_ex(x)
    6.         Else
    7.             Return Sin_ex(pi - x)
    8.         End If
    9.     End Function
    10.  
    11.     Function Sin_ex(ByVal x As Double) As Double 'Valid for [-pi/2;pi/2]
    12.         Return x - ((x ^ 3) / 6) + ((x ^ 5) / 120) - ((x ^ 7) / 5040) '+ ((x ^ 9) / 326880)
    13.     End Function

    Some notes:
    • Only for positive values
    • It's in radians
    • For some reason the approximation gets inaccurate for values between multiples of pi/2 and pi.(about accurate to the second decimal)
    • this was compared to the Math.Sin Function. I assumed in was 100&#37; correct


    EDIT: If you want Cos, there is a Taylor series approximation for that as well, but you can implement cos using sin quite easily since cos(x) = sin(x+pi/2) . And if you have cos then tan is quite easy aswell since tan(x) = sin(x)/cos(x)
    Last edited by BlindSniper; Jun 2nd, 2012 at 01:58 AM.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  2. #2
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,845

    Re: Sin Function

    Just so you know...the framework has Math.PI built in.

  3. #3
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 11
    Location
    South Africa
    Posts
    865

    Re: Sin Function

    I know, when I made the function I didn't want any dependency on the Math module, but then I ended up using math.truncate

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  4. #4
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 11
    Location
    South Africa
    Posts
    865

    Re: Sin Function

    I discovered that you can actually use mod on floating point numbers. Now I don't need that ugly first if statement.

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

  5. #5
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,845

    Re: Sin Function

    Quote Originally Posted by BlindSniper View Post
    I know, when I made the function I didn't want any dependency on the Math module, but then I ended up using math.truncate
    Ahh gotcha.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •