Results 1 to 3 of 3

Thread: 360 Degree Sine, Cosine and Arctangent

  1. #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


    ' Using Machine Code with ThinBasic
    ' Intel x86 Architecture Ref Manual Vol 2
    ' 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 '
    ' all other words are read as hexadecimal bytes.
    ' 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_
    +"Radians: "+vv(6)+$crlf_
    +"Degrees: "+vv(5)


  2. #2
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Brno - Czech Republic
    Rep Power

    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?

    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  3. #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.

Similar Threads

  1. canvas sine curve
    By zak in forum UI (User Interface)
    Replies: 6
    Last Post: 19-08-2010, 21:05

Members who have read this thread: 0

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