I needed a way to quickly generate many values of sin and cos on build-time. So I created a macro for both of them. It's based on their power series. This is my first complicated fasmg macro.

Code:
macro factorial: n
    if n
        factorial n - 1
        result = result * (n)
    else
        result = 1
    end if
end macro

; Positive only
macro power: base, exponent
    if exponent
        power base, exponent - 1
        result = base * result
    else
        result = 1
    end if
end macro

; Power series
macro sin: theta, iterations
    if iterations
        sin theta, iterations - 1
        old = result
        power -1.0, (iterations)
        var1 = result
        power theta, 2.0 * (iterations) + 1.0
        var2 = result
        factorial 2.0 * (iterations) + 1.0
        result = old + (var1 * var2 / result)
    else
        result = theta
    end if
end macro

; Power series
macro cos: theta, iterations
    if iterations
        cos theta, iterations - 1
        old = result
        power -1.0, (iterations)
        var1 = result
        power theta, 2.0 * (iterations)
        var2 = result
        factorial 2.0 * (iterations)
        result = old + (var1 * var2 / result)
    else
        result = 1.0
    end if
end macro

Here is an example of 100sin(30°) with 8 iterations of precision expressed as an integer:

Code:
sin PI / 6, 8
ld hl, trunc (100 * result)
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement