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:
Here is an example of 100sin(30°) with 8 iterations of precision expressed as an integer:
Code:
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)