Inferno #08
30 ноября 2005

For Coderz - The calculation of trigonometric and algebraic functions in a high-level languages.

<b>For Coderz</b> - The calculation of trigonometric and algebraic functions in a high-level languages.
               Smirnov VV


      Calculating trigonometric

          and algebraic functions

   (SIN, COS, TAN, ATAN, LOG, EXP, ...)

         in high-level languages


   All of these functions in BASIC called "one hundred
ndartnymi "or" embedded. "IN FORTRAN,
Pascal and in other high-level languages ​​-
"Standard" or "library".

   To develop algorithms
these functions are everywhere, including in mikrokalkulya
tori applied theme of so-called "Series." Some have
sum of an infinite number of terms. Eg
For example, for the function sin (x) is the number of values
Alternating, odd powers of argument
NTA (argument in radians):

 sin (x) = x-x ^ 3 / 3! + x ^ 5 / 5!-x ^ 7 / 7! + x ^ 9 / 9! - 
... 

   ... + (-1) ^ N * x ^ (2 * n +1) / (2 * n +1)! + ...


   For some other functions before the series
ponent such:



     cos (x) = 1-x ^ 2 / 2! + x ^ 4 / 4!-x ^ 6 / 6! + ...

     ... + (-1) ^ N * x ^ (2 * n) / (2 * n)! + ...
t.e.znakoperemenny even powers
argument;



   e ^ x = 1 + x / 1! + x ^ 2 / 2! + x ^ 3 / 3! + x ^ 4 / 4! + 
... 

             ... + X ^ n / n! + ...


         denoting (x-1) / (x +1) = z,

           write the following series:

     ln (x) = 2 (z + z ^ 3 / 3 + z ^ 5 / 5 + z ^ 7 / 7 + z ^ 9 
/ 9 + ... 

       ... + Z ^ (2 * n +1) / (2 * n +1) + ...


   arcsin (x) = x + x ^ 3 / (2 * 3) +1 * 3 * x ^ 5 / (2 * 4 * 
5) + 

         1 * 3 * 5 * x ^ 7 / (2 * 4 * 6 * 7) + ...

     ... + (1 * 3 * 5 *...*( 2 * n-1) * x ^ (2 * n +1) /

      (2 * 4 * 6 *...*( 2 * n) * (2 * n +1 ))+...



   (The series of certain functions, such as the tango
NSA and secant contain as coefficients
coefficients of special numbers (here - the number of
Bernoulli and Euler numbers, respectively).
To calculate these numbers need to
sponding algorithm, which will, consequently,
Consequently, part of the algorithm for computing the series
let the corresponding function.)


   These representations of series of various functional
tions are given in mathematical handbooks
framework of, in mathematical dictionaries, mathematical
cally encyclopedia.

   Initially the series prepared and examined in
university courses of mathematical analysis
(Aka - a course in differential and integral
lnogo calculus) whose composition (chickens
owls) subjects "series" is a mandatory
Mr. OK - can look in high school
numerous volumes of "Mata". In "series" of
directories should be kept in mind for so
lneyshego that powered them, the expression
so-called "general term of series (which allows the floor
chit by number term in the expression of this
term) can begin as early as from the ground up
and with the unit - the choice of the drafters
handbook. This should be the starting number
clarify, in case substituting n = 1
and assessed, whether obtained in the full representation
lenii first term of the series, or the second. (In
the latter case, therefore, the numbers on
begins from zero.)


   In the calculation of these "standard" function
tions are widely used function "factor
al "meaning" an exclamation point. "
We recall here: integer function integral
numerical argument, "en factorial" (n!)
means the product of all integers from
 1 to n.

   (Ed.: 0! Assumed to be 1. For the fractional
factorial of the argument can be extended
through the gamma function.)


   Thus, the calculation of the trigonometric and
algebraic functions.

   For practical implementation of the algorithm
Obviously, one should calculate and sum
vat a finite number of terms
Mykh - MEMBERS RYaDA.Dlya construct an algorithm
calculating, say, the sine must ask
precision of calculations - say, 0.00001.
This value will be used in a unique
vennom program operator conditional transition
transition: when the next term modulo
will be less than the specified precision (changes
Naya T) should stop the accumulation of the sum
we go to print the result.

   The algorithm is first assigned to changes
nye used in the program: X - the argument
ment; N - current value of the exponent of the
penalties (and the argument of the factorial); S - re
mennaya, which accumulates the sum, and in
Ultimately, which is calculated
numerically the sine (respectively, C
- The variable of summation of the cosine); H -
another term for the summation.

   The algorithm can be constructed from the calculation
Niemi next term of (term):
how to complete the formula, "the general term of"
and using for the calculations obviously
anterior term, the result of calculations
the previous term (ie, redefining
leniem variable, where its new value
defined in terms of old value). This
the second way better, simpler, shorter, with t.k.ne
hoditsya separately evaluate the "factors
rial. "Furthermore, the second method, apparently
shorter. And most importantly, direct
method can be denied due to overflow
as factorial is growing even faster than
kazatelnaya function.


   Here is the complete algorithm for computing the function
"Sine":

 (A)

     10 LET X = 1.2345

     20 LET T = 0.00001

     30 LET N = 1

     40 LET S = X

     1950 LET H = X

     1960 LET H = H * (-1) * X ^ 2 / (N +1) / (N +2)

     70 LET S = S + H

     80 LET N = N +2

     1990 IF ABS (H)> T THEN GOTO 1960

   100 PRINT "S ="; S


   In lines 10 - 50 specifies the initial value
radiation corresponding variables. (Actually
accumulation amounts are beginning from scratch, but
here is more convenient from the first term of the series, with 
isho dnogo X).


   In line 60 is evaluated obvious
anterior member of the series with its
"Old" values. "Old" - this expression
of the previous term of the series. Formula row
60 is obtained by comparing any two
Successive terms of the series. Say, the 2 nd and third.
How do they differ? On "X squared" -
in the numerator, the following two new orders
kovyh numbers - in the denominator, as well as familiar
Plus-minus. " All this is reflected in the line
60.

   On line 70 is accumulated sum
We, ie "The main computation. Line 80
calculate a new value for N
subsequent terms in the series if they are you
computed.

   On line 90 as determined by the times: the "pre
will stop computing "or" continue "if
accuracy has not yet been reached.


   The program calculations on the first versions
that (using the expression of general terms
in the series) is given below:

 (B)

     10 LET X = 1.23456

     20 LET N = 1

     30 LET T = 0.00001

     40 LET S = 0

     50 LET A = 1

     1960 LET F = 1

     70 FOR K = 1 TO (2 * N +1): LET F = F * K: NEXT K

     1980 LET H = A * X ^ (2 * N +1) / F

     90 LET S = S + H

    100 LET A =- A

    110 LET N = N +1

    120 IF ABS (H)> T THEN GOTO 1960

   130 PRINT "S ="; S


   Here, in lines 60, 70 calculated fundamental
ktsiya "factorial": in the loop accumulates
product of numbers from one to the current
parametra.Ispolzovanie variable A - a
"Tricks" in connection with what is needed to
formula of general term of computation of "knowledge
koizmenyayuschego "factor AT FIRST PRO
DURING for "minus one in degree zero,
may be false (or posts
eat a bug) on ​​different machines and different
versions Beysika.Nuzhno noted that the show
is positive function with a negative base
not defined. See that this program
more cumbersome than the previous one. In addition,
has drawbacks mentioned above. Therefore,
with this type of program (B), no further work
it.


                  *


   If the program (a) require
issuance of function values ​​for the argument, we introduce
dimogo from the keyboard ("directory Brady
sa "), in line 10, instead of assigning ap
argument of the operator will be INPUT.

   Or, say, instead of printing solutions
result can be transferred to another program
metal, and this will, thus, routine - for
those versions of BASIC or another language, where
initially there is no trigonometry, for example,
"BASIC-micro (J." Radio ", the 1980's.)
or in some / designed by you! /
the simplest high-level language. In the "self-
ignition lines must "HLL this way allows us to obtain
trigonometry simpler means
the very same language, rather than the more labor-intensive 
small shinnokodovoy procedure. This saves na

rumple (ROM), but, for example, leads to losses in
time.


   To calculate the values ​​of "cosine
Nous "is easy to make a similar prog
Rummy. If need be, and the sine and cosine
cone, you can modify the source program
adding in (A) the following lines:



     45 LET C = 1

     75 LET C = C + H / X * (N +2)

   120 PRINT "C ="; C


   Here in line 45 is assigned changes
C such that will accrue value
chenie cosine series. This variable Ac
voeno, respectively, the initial value
cosine series.

   Single line calculations here
(75) uses computed for this moment
that the value of another (with the same order
vym number) term of a sinus. In this
line accumulates a number of cosine
ca, with the corrected value of H. (Pe
H belt this line does not change.) Cor
Correction is multiplied by the factor
Tel (N +2) / X. Form factor obtained from
comparison of "same name" of the series inverter
and cosine.


   Typically, the practical realization of you
numerically, using the periodicity of trigonometric
metric functions are translated into arguments
element, subtracting the integer number of periods (multiple
2 * pi = 6.283185307179586476925286766559 ...).
Otherwise, a number of "worse is convergent, ie required
much larger number of iterations (see below).
In addition, in the same order after argument
it still decrease: using "school"
reduction formulas lead to the first quad
welt. Then the calculation is very short.


                  *


   Further, developing and changing the program (A)
a little research. (Far
the neck has a general character for the numerical methods
todov.T.e.prilozhimo not only to the calculation of
standard functions HLL).

   Any problem, then solve it, requires
CHECKS.

   Here you can check two
 ways:

  a) can be calculated from the known functions
known arguments, such as the sine of
 pi / 6;

  b) calculating the "integral" functions SIN,
COS, ... which, of course, exist in all
modern versions of Java.


   To test the second method should
 add the line:

    110 PRINT "SIN ="; SIN (X)

   It is useful to adjust the formula
mat output the previous line (100) to
it was convenient to compare the level of the same name
(There - add 2 spaces inside the quotation marks).


    Next. Add in (A) the line:

     78 PRINT S; ""; C; ""; H

   As a result of the fact that this stamp set
To repeated parts of the program in about
process of solving the machine prints out a three hundred
lbtsa current values ​​of variable names.
From these data, it can be seen as occurs
Dilo approximation to this value system
cone (cosine). (Real - calculated
built the corresponding function.) So
also seen a decrease of the module H to a value
below a set of T. Moreover, the smaller T -
single line before printing the final
rezultata.Iz of these three columns should be
exists and has some information: how many times
machine was on the repeat participants
Re programmy.A: the number of lines - one hundred
lko and repetitions.


   To estimate the number of passes on the types
toryayuschemusya portion of the program usually
Cyclic her site is added counter:



     23 LET K = 0

     65 LET K = K +1

   133 PRINT "K ="; K


   Changing the required accuracy - by asking
0.0001 or 0.000001 - will receive once
finite number of rows in stolbtsah.Chem more points
In particular, so, obviously, the more the number of passes
(Iterations), and therefore more steam
meter K and a large number of auxiliary lines
Noah print (here - the variables S, C, H interior
ri cycle).


   Next. Say, with an accuracy of "ten to the
negative heel "of the final result is identical
FLS (with built-in) to five digits
decimal (Ed.: rather, Reza
ltaty will differ by no more than
one 5-th digit after the decimal point. Below
similarly).

   When exactly "ten to the minus six" -
match six numbers.

   If you assign the accuracy of "ten to the minus
eighth, then match all the numbers, and thus
, the result of this algorithm turns
said to be exactly the same as that of integrated
receding function SIN (X), COS (X).

   Therefore, their program you can
surpass the accuracy of your language version: up to
empty if it does not laid down "a double
chnost "(16 digits) - set dos
sufficiently small value of T.


   If you set the value of the argument over
Pi / 2 "or even more -" X +2 * pi "- then we can
but to see how dramatically slows the convergence
ryada.Prichem first appear strong to
oscillations module summy.Iz all this and the following
feasibility of converting the argument follows
in the first quadrant.

SWW (A.E.-2)




Other articles:

Inferno - The authors of the magazine.

Inferno - Entered from the editor.

Inferno - Errors in the previous numbers.

Inferno - On the shell.

Iron - CD-ROM - a device and Tricks connection.

For Coderz - CD video on the ZX. How to write a video player with CD-ROM.

Softinka - Technical Reference disk system DISCiPLE / + D.

For Coderz - Small programmers' tricks.

Mathematics - The history of the solution of Fermat's theorem.

Gameland - Game Shestnashki.

Softinka - a description and history of the program to work with hard disk HDDoctor v0.9.

Secrets - The X button in different applications: Wolfenstein'2004, IG # 5, ACEdit, DNA OS, Wild Disk Copier.

Softinka - Improved graphics converter Gigascreen.

Sound - tube amplifier. Stereo lampochnik 2x5 Tues of old TVs.

Iron - Overview of cassette players.

Inferno - Letters to the Editor.

Gamedev - The history of the game Pang.

Gamedev - Tips for writing an arcade game.

Advertising - Ads by Roman Chuunin.

Advertising - Ads by Alexei Zhabina.

Advertising - Ads by V. Bogdanovich.

Interview - Interview with Nikolay Rodionov, author of popular books for the ZX Spectrum

For Coderz - The calculation of trigonometric and algebraic functions in a high-level languages.

DIY - scheme of 16-color video mode v1.1 for the Pentagon.

Iron - Description of Products K555TL2.

For Coderz - Programming device sound generation Turbo Sound.

Softinka - 384x304 viewer. program allows you to view color pictures, is larger than the screen.

Softinka - ZX Spectrum emulator for ZX Spectrum.


Темы: Игры, Программное обеспечение, Пресса, Аппаратное обеспечение, Сеть, Демосцена, Люди, Программирование

Similar articles:
Story - Letter from afar.
attempt at writing - Many Adventures of Winnie the Pooh (Part 3).

В этот день...   21 November