# Thread: 360 Degree Sine, Cosine and Arctangent

1. ## 360 Degree Sine, Cosine and Arctangent

This shows the conversion of an angle in degrees to radians to Sine and Cosine to radians to degrees.
The final answer is expressed by the FPU within +/- 180 degree range.

By retaining sine and cosine data - the quadrant info is never lost, as happens with a tangent.
Also, tangent infinity problems are avoided.

Note that the FPU can produce the Sine and Cosine with a single instruction. FSINCOS

[code=thinbasic]

' Using Machine Code with ThinBasic
'---------------------------------------------------------------------------
'---Reference:
' Intel x86 Architecture Ref Manual Vol 2
'---http://developer.intel.com/design/pentiumii/manuals/243191.htm
'---------------------------------------------------------------------------
' Syntax rules:
' #Variable patches in long address of Variable (4 bytes)
' NLn patches in long decimal value n (4 bytes)
' comments are indicated by a quote mark '
' An error during MC_Eval\$ will produce a string containing &hc3 (ret) only.

'----------------------------------------------
' arithmentic on the Floating Point Processor (FPU)
' 360 degree sine cosine and arctangent
'----------------------------------------------
dim vv( as double
dim sMC as string = MC_Eval\$ "
b8 #vv ' ' vv base address
dd 40 08 ' fld qword [eax+08] ' angle degrees in vv2
c7 00 NL180 ' mov dword [eax],180 ' temp store vv0 integer 180
da 30 ' fidiv dword [eax] ' angle in degrees
d9 eb ' fldpi ' pi
de c9 ' fmulp st(1) ' multiply by pi
d9 fb ' fsincos ' produce sine st(0) and cosine st(1)
dd 50 38 ' fst qword ptr [eax+56] ' sin in vv8
d9 c9 ' fxch st(1) ' swap to save cos
dd 50 30 ' fst qword ptr [eax+48] ' cos in vv7
d9 c9 ' fxch st(1) ' swap back again
d9 f3 ' fpatan ' arctangent pops 1 then result in st(0)
dd 50 28 ' fst qword ptr [eax+40] ' store arctan radians in vv6
d9 eb ' fldpi ' load pi
de f9 ' fdivp st(1) ' divide angle by pi
da 08 ' fimul dword ptr [eax] ' multiply by 180
dd 58 20 ' fstp qword ptr [eax+32] 'store angle degrees in vv5
c3 ' ret '
"
'-------------------------------'

vv(2)=300 ' test angle

MC_Exec sMC
msgbox 0, "Initial angle: "+vv(2)+\$crlf_
+"Cosine: "+vv(+\$crlf_
+"Sine: "+vv(7)+\$crlf_
+"Degrees: "+vv(5)

[/code]

2. ## Re: 360 Degree Sine, Cosine and Arctangent

Thanks Charles!,

very nice and very fast again
Are digits loaded by d9 eb the same on AMD/Intel/VIA, or can it happen some of them will provide just 3.1415 for (extreme ) example?

Thanks,
Petr

3. ## Re: 360 Degree Sine, Cosine and Arctangent

As far as I know, the FPU instructions are the same for all Pentium-like processors.

In fact there have been very few enhancements since the 8087 about 25 years ago. FSIN FCOS FSINCOS are more recent. Also in the original chip, the condition codes could not be passed directly to the EFLAGs register on the CPU - the new instructions for doing FPU comparisons are FCOMI FCOMIP FUCOMI and FUCOMIP allowing the CPU to make conditional jumps, immediately following these single instructions.

There are no members to list at the moment.

#### Posting Permissions

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