Board index » delphi » Cos and Sin Function

Cos and Sin Function

Hi

How is the Sin and Cos function coded on machine level language. I think
an implemented table with values is too complicated.

Lars from Kiel ,Germany

 

Re:Cos and Sin Function


Quote
Lars Richter wrote:
> Hi

> How is the Sin and Cos function coded on machine level language. I think
> an implemented table with values is too complicated.

> Lars from Kiel ,Germany

It is a binomial approximation in Borland Pascal for the standard 6-Byte
Real type. For the 10-Byte extended Real the 8087 does the job.

Franz Glaser, Austria
http://members.eunet.at/meg-glaser

Re:Cos and Sin Function


On 3 Mar 1998 14:09:13 GMT, u2...@bwl.uni-kiel.de (Lars Richter)
wrote:

Quote
>How is the Sin and Cos function coded on machine level language. I think
>an implemented table with values is too complicated.

They are implemented as polynomials which are evaluated in the range
0..pi/4.

Regards
Horst

   *** Las orillas del Nahuel Huapi ***

Re:Cos and Sin Function


Quote
Horst Kraemer wrote:
> They are implemented as polynomials which are evaluated in the range
> 0..pi/4.

I have Chebyshev polynomial approximations for most trig functions, but the
RTL
of Borland TP is made with binomial series, as I remember. The source code is

available as part of the RTLibrary in ASM.

For another job where accuracy was not so critical I made a linear
interpolation,
it is good for circles on the screen with some 17 or 33 table entries, and
very
fast. The table entries contain offset values to an optimized straight line.

Franz Glaser, Austria
http://members.eunet.at/meg-glaser

Re:Cos and Sin Function


JRS:  In article <6dh2u9$sd...@infosrv.rz.uni-kiel.de> of Tue, 3 Mar
1998 14:09:13 in comp.lang.pascal.borland, Lars Richter

Quote
<u2...@bwl.uni-kiel.de> wrote:
>How is the Sin and Cos function coded on machine level language. I think
>an implemented table with values is too complicated.

Most, but not all, PCs currently in use have a numeric coprocessor,
probably integrated with the CPU.  Hence a single instruction
basically does the job (although the input may need to be
normalised/scaled).

For example, to implement the equivalent of FORTRAN's ATAN2(Y, X)
(from Terje Mathisen),

    Function  atan2(y : extended; x : extended): Extended; Assembler;
    asm  fld [y] ; fld [x] ; fpatan  end ;

In the absence of, and within, a coprocessor, subtle polynomials or
other functions are used, I believe.

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web <URL: http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Do not Mail News to me.    Before a reply, quote with ">" or "> " (SoRFC1036)

Re:Cos and Sin Function


Lars Richter <u2...@bwl.uni-kiel.de> skrev i artiklen
<6dh2u9$sd...@infosrv.rz.uni-kiel.de>...

Quote
> Hi

> How is the Sin and Cos function coded on machine level language. I think
> an implemented table with values is too complicated.

> Lars from Kiel ,Germany

Hi Lars

If you have an Intel x87 coprocessors it can apply the Sin or Cos functions
with a single assembler instruction. (I think they are called FSIN and
FCOS).
If not I guess that pascal uses the Taylor-polynomial for an approximation.
(Actually i guess that the x87 uses that method internally, too.)

The Taylor-polynomal for for Sin and Cos look like this:
Sin(x) = x - x^3 / 3! + x^5 / 5! - x^7 / 7! + ...
Cos(x) = 1 - x^2 / 2! + x^4 / 4! - x^6 / 6! + ...
where n! = n * (n-1) * ... * 2 * 1 (factorial).

The terms gets smaller and smaller to the right, because the denominators
grows so fast, so you just add the terms from left until they gets smaller
than you needed precision.

I hope this helps
        -Jes R. Klinke

Re:Cos and Sin Function


On 3 Mar 1998 14:09:13 GMT, u2...@bwl.uni-kiel.de (Lars Richter)
wrote:

Quote
>Hi

>How is the Sin and Cos function coded on machine level language. I think
>an implemented table with values is too complicated.

>Lars from Kiel ,Germany

You can use FSIN and FCOS opcodes but since
the internal assembler only supports 287 opcodes
you need to code them as
DB $D9 FE (SIN)  and
DB $D9 FF (COS)
The argument has to be (absolute) less than 2^63.

Soren.

Other Threads