# Sine procedure in MASM

#### agentofdarkness

Joined Oct 9, 2007
42
For an assignment I have to write a procedure to calculate the sine of a number (located in EAX). I'm suppose to use a taylor expansion to approximate the sin(eax). I have most of the code working. If I have a number in EAX, I can calculate the 7th order Taylor expansion of the number. I have a macro called mTaylor_Order integer. The macro will calculate ST(0)^integer / integer!. The Taylor approximation works fine. However, a 7th order Taylor approximation of Sin x is only accurate between -pi and -pi. I have to be able to calculate the sine of any number. The way I tried to shift any number (I will call it Y) into a number between -pi and pi (call it X) is that I use the following equation:
X = pi * (modulo(Y/pi)).

The program does not work though. Any ideas on how I should shift Y so it will be a number between -pi and pi?

Sine PROC

fldpi
fld Z
fprem
fldpi
fmul
fstp X
finit
fld X
mTaylor_Order 1
fld X
mTaylor_Order 5
fld X
mTaylor_Order 3
fsub
fld X
mTaylor_Order 7
fsub
fstp X
mov eax, X
call WriteHex
call Crlf
ret
Sine ENDP

Joined May 1, 2007
19
hey agent

if i understand right (which may well fail to be the case) you're asking how, given an arbitrary real number y, you can produce a number x with abs(x)<pi that is congruent to y modulo 2*pi. i gather the language you're using lets you calculate the residue of a given number modulo a given modulus. if i'm right about both those points then you need something like this:

def f(x):
x = y modulo 2*pi
if 0<=x<=pi: return x
else: return 2*pi-x

hope it helps

peace
stm

#### agentofdarkness

Joined Oct 9, 2007
42
How can I do modulo division in MASM using the FPU? There is an instruction FPREM but it does not work, keeps returning a value >1. I tried to write a modulo macro using the instruction FRNDINT but that can round up or down.

Joined May 1, 2007
19
How can I do modulo division in MASM using the FPU? There is an instruction FPREM but it does not work, keeps returning a value >1. I tried to write a modulo macro using the instruction FRNDINT but that can round up or down.
how about something along these lines?:

def f(x):
if abs(x)<=pi: return x
sgn = (-1,+1)[x>0]
return f(x)-2*pi*sgn

the problem is that i don't know assembly language. i'm willing to try to help you with this but it's very possible that my help isn't worth anything.