-
Jun 30th, 2018, 05:20 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] VB.NET - Attempting to write a custom sine function
Hi,
Curious to learn a bit more about trigonometry and trying to get a better understanding of the underlying functions (sine, cosine, tangent, etc.) I tried to write a custom sine function in VB.NET:
Code:
Imports System
Module Module1
Sub Main()
Console.WriteLine(Math.Sin(Math.PI / 2))
Console.WriteLine(CustomSine(Math.PI / 2))
End Sub
Function CustomSine(Angle As Double) As Double
Return Angle - (Angle ^ 3.0 / 6.0) + (Angle ^ 5.0 / 120.0)
End Function
End Module
The "CustomSine" function above is based on information found at https://stackoverflow.com/questions/...-code-for-sinx. Specifically the following algebraic formula:
Code:
x - (x^3/3!) + (x^5/5!)
As I understand it "3!" and "5!" simply mean "give me the factorials for 3 and 5". Seeing no reason to write a custom function (did that once already and would only slow the "CustomSine" function down) for the factorials of two numeric constants the precalculated values (6.0 and 120.0 respectively) were used instead.
As to the question:
My "CustomSine" produces consistently higher values than the "System.Math.Sin" function. As I understand the output values should never exceed the range of -1 to +1 when using 0 to PI / 2.0 as inputs. Either my code has errors (likely rounding errors) or the approximation given at StackOverflow isn't accurate enough. What formula would produce better results and why?
Just to be clear, this is just for a hobby and higher math isn't my specialty. Thanks.
Last edited by Peter Swinkels; Jul 1st, 2018 at 05:24 AM.
Reason: Fixed a sentence.
-
Jun 30th, 2018, 12:04 PM
#2
Re: VB.NET - Attempting to write a custom sine function
PI / 2 is approx 1.570796327 (call it x) and sine(x) should give an answer of 1
(x ^ 3) / 6 = 0.645964098
(x ^ 5) / 120 = 0.0796926262
So sine(x) is 1.570796327 - 0.645964098 + 0.0796926262 = 1.0045248552
which is correct 2 dp. If you need more accuracy then you can consider further terms in the series.
The next two terms are -(x ^ 7) / 7! and (x ^ 9) / 9! (and then -(x ^ 11) / 11! and (X^13) / 13! )
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jul 1st, 2018, 05:53 AM
#3
Thread Starter
Frenzied Member
Re: VB.NET - Attempting to write a custom sine function
Originally Posted by 2kaud
PI / 2 is approx 1.570796327 (call it x) and sine(x) should give an answer of 1
(x ^ 3) / 6 = 0.645964098
(x ^ 5) / 120 = 0.0796926262
So sine(x) is 1.570796327 - 0.645964098 + 0.0796926262 = 1.0045248552
which is correct 2 dp. If you need more accuracy then you can consider further terms in the series.
The next two terms are -(x ^ 7) / 7! and (x ^ 9) / 9! (and then -(x ^ 11) / 11! and (X^13) / 13! )
Fixed! Thanks.
Code:
Function CustomSine(Angle As Double) As Double
Return Angle - (Angle ^ 3.0 / 6.0) + (Angle ^ 5.0 / 120.0) - (Angle ^ 7.0 / 5040.0) + (Angle ^ 9.0 / 362880) - (Angle ^ 11.0 / 39916800.0) + (Angle ^ 13.0 / 6227020800.0) - (Angle ^ 15.0 / 1307674368000.0) + (Angle ^ 17.0 / 355687428096000.0) - (Angle ^ 19.0 / 121645100408832000)
End Function
Any factorials beyond 19 appear to be pointless and result in an overflow anyway.
-
Jul 1st, 2018, 08:59 AM
#4
Re: [RESOLVED] VB.NET - Attempting to write a custom sine function
Great - but you usually won't implement like that.
Note that if t is the term number (starting at 1)
third term (t = 3)
Angle ^ 5 = <prev value> * Angle * Angle
5! = <prev value> * 4 * 5
note that 4 is (t - 1) * 2 and 5 is (t - 1) * 2 + 1
fourth term (t = 4) where <prev value> is that obtained from the third term
Angle ^7 = <prev value> * Angle * Angle
7! = <prev value> * 6 * 7
note that 6 is (t - 1) * 2 and 7 is (t - 1) * 2 + 1
etc etc
and that the signs of the terms alternate between + and -.
Given this, the calculation can be done in a simple loop to the required number of terms.
All advice is offered in good faith only. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
Jul 1st, 2018, 09:55 AM
#5
Thread Starter
Frenzied Member
Re: [RESOLVED] VB.NET - Attempting to write a custom sine function
This code has been removed.
Last edited by Peter Swinkels; Sep 5th, 2018 at 06:18 AM.
Reason: Posted code did not work.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|