Translated using translate google. The original language is Russian. Original page https://hi-aga.ru/index.php/homepage/for-21-30

Example twenty-one. In this problem we find the value of the exponent, a rather interesting task not only for educational purposes, but also for practical purposes, despite the fact that in Forth you can get this value simply by typing the following code:
1E FEXP F.
2.7182818  Ok
: FOR21 ( I: N>0 ->  )  \ 1/1!+1/2!+1/3!+...+1/N!
1+ 1             \ пределы параметра цикла от 1 до N
1E 1E            \ F:  -> 1 1 – первая единица – это текущая сумма ряда, вторая текущий ее член
DO
    I 0 D>F F/   \ F: S 1 –> S 1/I
    FSWAP FOVER  \ S 1/I -> 1/I S 1/I
    F+ FSWAP     \ 1/I S 1/I -> S+1/I 1/I
LOOP FDROP F. ;  \ удаляем текущий член (1/I) печатаем сумму (S+1/I)
\ Тест словом TEST-FOR21 проверим не только работу решения задачи, но и сходимость данного метода вычисления экспоненты.
: TEST-FOR21 11 1 DO I DUP . FOR21 CR LOOP ;
\ В итоге на экране получим следующий результат:
TEST-FOR21
1 2.0000000
2 2.5000000
3 2.6666667
4 2.7083333
5 2.7166667
6 2.7180556
7 2.7182540
8 2.7182788
9 2.7182815
10 2.7182818
 Ok
For N=10 we get the result accurate to seven decimal places. Example twenty two. We use the variable X, otherwise manipulations on the stack will be unreasonably complex, and there will be no clarity.
VARIABLE X
: FOR22 ( I: N>0 F: X ->  )  \ 1+X/1!+X^2/2!+X^3/3!+...+X^N/N!
X F!             \ сохраняем значение X в одноименной переменной
1+ 1             \ пределы параметра цикла от 1 до N
1E 1E            \ F:  -> 1 1 – первая единица – это текущая сумма ряда, вторая текущий ее член
DO
    X F@ F*      \ F: S 1 –> S 1*X
    I 0 D>F F/   \ F: S 1*X –> S 1*X /I
    FSWAP FOVER  \ S 1*X/I -> 1*X/I S 1*X/I
    F+ FSWAP     \ 1*X/I S 1*X/I -> S+1*X/I 1*X/I
LOOP FDROP F. ;  \ удаляем текущий член (1*X/I) печатаем сумму (S+1*X/I)
The only difference from the previous example is the addition of line one (variable X) and seven (the current member of the series in the loop is multiplied by X each time before the next summation).

Test with the word TEST-FOR22 let's check the operation of FOR22 with the parameter X=1E (a special case, we reduce the problem to the previous one, we get the same data, check it yourself).
\ Теперь проанализируем с параметром, например, два.
: TEST-FOR22 21 1 DO I DUP . 2E FOR22 CR LOOP ;
TEST-FOR22
1 3.0000000
2 5.0000000
3 6.3333333
4 7.0000000
5 7.2666667
6 7.3555556
7 7.3809524
8 7.3873016
9 7.3887125
10 7.3889947
11 7.3890460
12 7.3890546
13 7.3890559
14 7.3890561
15 7.3890561
16 7.3890561
17 7.3890561
18 7.3890561
19 7.3890561
20 7.3890561
 Ok
We see that the convergence is worse, with I=14, we get an approximate result (for some reason with rounding). Be careful with real numbers (they can be weird).

Example twenty three. The first line is the declaration of an auxiliary variable. The second standard beginning is the description of the word. Third, we set the limits of the cycle. The fourth, after triple duplication, we get: the first parameter is the initial value of the sum, the second term of the sum, the third square X is stored in a variable of the same name, we will need it to calculate the next term of the series. From the fifth to the eleventh cycle in which we calculate the term of the series and add it to the value of the sum of the series.
In the sixth we read the value of the variable X, where its square is stored, multiply by (An) and change the sign to the opposite, in the seventh loop parameter I we multiply by two, duplicate and increase by one, as a result we get (2*I) on the real stack and (2*I+1), which in the eighth we multiply by each other, and the result is a divisor for (–An*X^2) (as a result of these manipulations we obtain the factorial in the denominator). In the ninth and tenth, we add the resulting current term to the sum of the series, and return the original order. As a result, in the twelfth, we have the sum of the series and its last term ready, discarding which, if unnecessary, we print the final result.
: FOR23 ( F: X I: N>0 -> )  \ 1.5E 5 FOR23=SIN[1.5] X-X^3/3!+X^6/6!-...+[-1]^N*X^[2*N+1]/[2*N+1]!
1+ 1                         \ пределы параметра цикла
FDUP FDUP FDUP F* X F!       \ X -> X X , первый X – сумма ряда (S), второй ее член (An), X=X^2
DO
    X F@ F* FNEGATE          \ S An -> S –An*X^2
    I 2* DUP 1+ 0 D>F 0 D>F  \ S –An*X^2 -> S –An*X^2 2*I 2*I+1
    F* F/                    \ S –An*X^2 2*I 2*[I+1] -> S –An*X^2/{2*I*2*[I+1]}
    FSWAP FOVER              \ S -An*X^2/{2*N+1}! -> -An*X^2/{2*N+1}! S -An*X^2/{2*N+1}!
    F+ FSWAP                 \ -An S -An -> S-An -An
LOOP
FDROP F. ;                   \ удаляем текущий член (1*X/I) печатаем сумму ряда
Test at angles of 0, 1.5 and 3.14 (Pi) radians with a number of iterations of five. Experiment with other values ​​yourself.
0E 5 FOR23
0.0000000  Ok
1.5E 5 FOR23
0.9974949  Ok
FPI 5 FOR23
>-0.0004451  Ok
Example twenty-four. Twin brother of the previous one. We replace the first term with one (instead of X), and proceed similarly with the sum of the series. We use the previously declared variable, everything else remains unchanged. Using mathematics, it was possible not to write the word FOR24, but using reduction formulas to calculate COS through SIN, although I think the purpose of study is to gain experience in programming, and not in demonstrating knowledge in mathematics.
: FOR24 ( F: X I: N>0 -> )  \ 1.5E 5 FOR23=COS[1.5] 1-X^2/2!+X^4/4!-...+[-1]^N*X^[2*N]/[2*N]!
1+ 1                         \ пределы параметра цикла
FDUP F* X F! 1E 1E           \ X -> 1 1 , первый 1 – сумма ряда (S), второй ее член (An), X=X^2
DO
    X F@ F* FNEGATE          \ S An -> S –An*X^2
    I 2* DUP 1- 0 D>F 0 D>F  \ S –An*X^2 -> S –An*X^2 2*I 2*I-1
    F* F/                    \ S –An*X^2 2*I 2*I-1 -> S –An*X^2/{2*I*[2*I-1]}
    FSWAP FOVER              \ S -An*X^2/{2*I}! -> -An*X^2/{2*I}! S -An*X^2/{2*I}!
    F+ FSWAP                 \ -An S -An -> S-An -An
LOOP
FDROP F. ;                   \ удаляем текущий член (1*X/I) печатаем сумму ряда
\ Тесты на вычисление косинуса углов 0, 3,14 (Пи) и 1,5 радиан, для параметра итерации равной пяти.
0E 5 FOR24
1.0000000  Ok
FPI 5 FOR24
-1.0018291  Ok
FPI 10 FOR24
-1.0000000  Ok
1.5E 5 FOR24
0.0707369  Ok
Example twenty-five. The second line is storing the input parameter in a variable of the same name. In the third, we add zero (the initial value of the sum) and minus one (the initial value of the workpiece for calculating the “n” term) to the real stack. The fourth we prepare the cycle and from the fifth to the ninth the cycle itself, in which we read “X”, multiply it by the workpiece and change the sign to the opposite (for this we first made it minus one in order to get the first term positive, as in the problem statement). Next, we duplicate and calculate the member of the series itself, dividing by “I” (the loop iteration parameter), adding to the previous sum, and restoring the original order of the parameters. All this is so as not to spoil the preparation for calculating the next element of the series, because it will not be easy to calculate the next one using the previous one.
: FOR25 ( |X|<1 N>0 -> )  \ 0.5E 5 FOR25 = ln[1+X] , X-X^2/2+X^3/3-...+[-1]^[N-1]*X^N/N
X F!                 \ сохраняем значение X в переменную
0E -1E               \ 0 – сумма, -1 – первый член
1+ 1                 \ пределы цикла
DO
    X F@ F* FNEGATE  \ S An -> S -An*X
    FDUP I 0 D>F F/  \ S -An*X -> S -An*X -An*X/I
    FROT F+ FSWAP    \ S -An*X -An*X/I -> S-An*X/I -An*X
LOOP FDROP F. ;      \ удаляем текущий член печатаем сумму ряда
Парочка тестов:
0.1E 10 FOR25
0.0953101  Ok
0.2E 10 FOR25
0.1823215  Ok
0.3E 10 FOR25
0.2623641  Ok              
\ А теперь проверим вычислив данные значения стандартными средствами Форта.
1.1E FLN F.
0.0953101  Ok
1.2E FLN F.
0.1823215  Ok
1.3E FLN F.
0.2623642  Ok
If someone doesn’t like the difference in the last digit when calculating “0.3E 10 FOR25”, then you can increase the number of elements of the series so that with N=20 we get the same result. Experiment yourself on different data to see how quickly the series converges (and what value of N is sufficient in different cases).
Example twenty-six. The series converges very quickly. In line two, the first two “FDUP” give the initial value of the sum and the blank for the “n”th member of the series, which is obtained by multiplying by the square of “X” stored in the variable of the same name, and dividing by (2*I+1), the next two “ FDUP" allows you to get the square of "X" after multiplication. Everything else is standard, as before, preparing the limits of the cycle parameter and the cycle itself, in which we multiply the blank of the “n”-th term by the square “X”, duplicate and divide by (2*I+1), obtaining directly the term of the series ( since the next one cannot be obtained from the previous term, due to the fact that the denominators have different numbers), then we add the sum to the previous value and return the original order of the parameters on the stack.
: FOR26 ( |X|<1 N>0 -> )  \ 0.5E 5 FOR25 = ln[1+X] , X-X^3/3+X^5/5-...+[-1]^N*X^(2*N+1)/ (2*N+1)
FDUP FDUP FDUP F* X F!     \ X -> X X , 1-ый X сумма, второй - первый член, X=X^2
1+ 1                       \ пределы цикла
DO
    X F@ F* FNEGATE        \ S An -> S -An*X^2
    FDUP I 2* 1+ 0 D>F F/  \ S -An*X^2-> S -An*X^2 -An*X^2/(2*I+1)
    FROT F+ FSWAP          \ S -An*X^2 -An*X^2/(2*I+1) -> -An*X^2/(2*I+1)+S -An*X^2
LOOP FDROP F. ;            \ удаляем текущий член печатаем сумму ряда
\ Напоследок парочка тестов примера 26:
0.1E 5 FOR26
0.0996686  Ok
0.2E 5 FOR26
0.1973955  Ok
0.3E 5 FOR26
0.2914567  Ok
Of course, these values ​​can be calculated directly by one operation (the corresponding built-in function) of the Forth language. This is how we will check the correctness of the written word.
0.1E FATAN F.
0.0996686  Ok
0.2E FATAN F.
0.1973955  Ok
0.3E FATAN F.
0.2914567  Ok
Example twenty-seven. In the third line, we prepare the initial value of the sum of the series and the blank for the first member; we also wrote the value of its square in the variable “X”, since each member of the series is greater than the previous one by this number, we use it. Next comes the standard cycle with setting its limits. In the body of the loop, we multiply the blank for each element of the row by “X^2” (sixth line), by “2*I-1” (seventh line), divide by “2*I” (eighth line), duplicate and divide by “ 2*I+1" (ninth line), since this action is different for each member, we do not change the blank (so as not to “spoil” it), then, as before, we add it with the current amount and restore the order of the parameters on the stack. The eleventh line displays the current values ​​of the element of the series and its sum, so you can see how quickly the series converges (if you get tired of playing around, you can delete it).
: FOR27 ( |X|<1 N>0 -> )  \ 0.5E 5 FOR25
\ arcsin[X]=X+1*X^3/(2*3)+1*3*X^5/(2*4*5)+...+1*3*…*(2*N-1)*X^(2*N+1)/ (2*4*…*(2*N)*(2*N+1))
FDUP FDUP FDUP F* X F!  \ X -> X X , 1-ый X сумма, 2-ой – заготовка первого члена
1+ 1                    \ пределы цикла
DO
    X F@ F*             \ S An -> S An*X^2
    I 2* 1- 0 D>F F*    \ S An*X^2 -> S An*X^2*(2*I-1)
    I 2* 0 D>F F/ FDUP  \ S An*X^2*(2*I-1) -> S An*X^2*(2*I-1)/(2*I) An*X^2*(2*I-1)/(2*I)
    I 2* 1+ 0 D>F F/    \ -> S An*X^2*(2*I-1)/(2*I) An*X^2*(2*I-1)/(2*I*{2*I+1})
    FROT F+ FSWAP       \ -> An*X^2*(2*I-1)/(2*I*{2*I+1})+S An*X^2*(2*I-1)/(2*I)
FDUP F. FOVER F. CR     \ выводим текущие значения «An» и суммы
LOOP FDROP F. ;         \ удаляем текущий член печатаем сумму ряда
\ Для «Пи/4» мы получим следующее:
FPI 4E F/ 30 FOR27
0.2422365 0.8661436
0.1120677 0.8885572
0.0576075 0.8967868
0.0310933 0.9002416
0.0172619 0.9018109
0.0097606 0.9025617
0.0055908 0.9029345
0.0032331 0.9031246
0.0018835 0.9032238
0.0011037 0.9032763
0.0006499 0.9033046
0.0003842 0.9033200
0.0002278 0.9033284
0.0001355 0.9033331
0.0000808 0.9033357
0.0000482 0.9033371
0.0000289 0.9033380
0.0000173 0.9033384
0.0000104 0.9033387
0.0000062 0.9033389
0.0000037 0.9033389
0.0000022 0.9033390
0.0000013 0.9033390
0.0000008 0.9033390
0.0000005 0.9033391
0.0000003 0.9033391
0.0000001 0.9033391
0.0000001 0.9033391
0.0000000 0.9033391
0.0000000 0.9033391
0.9033391  Ok
After a little research, we can easily determine where the next element of the sum becomes zero and does not affect the further sum. Now let's calculate several arcsin values ​​for the following angles:
0.1E 30 FOR27
0.1001674  Ok
0.2E 30 FOR27
0.2013579  Ok
0.3E 30 FOR27
0.3046926  Ok
Let's calculate the same values ​​using standard Forth tools and prove the correctness of our (easy to write) word:
0.1E FASIN F.
0.1001674  Ok
0.2E FASIN F.
0.2013579  Ok
0.3E FASIN F.
0.3046926  Ok
Example twenty-eight. In the third line we save X, enter two units as the sum and the first term. Next is the already familiar cycle. Each time we multiply a member of the series by “X” and alternately change the sign to the opposite one, multiply by “2*I-3” and divide by “2*I”. We restore order after receiving the current amount. The eleventh line contains commented out code that displayed the current values ​​of the sum and element of the series in a loop (you can see for yourself how poorly the series converges and how many iterations are needed for acceptable accuracy when X approaches one). It is usually not recommended to comment on code; some programmers may consider it bad manners, but during debugging it is a very convenient trick (turning individual functions on and off).
: FOR28 ( |X|<1 N>0 -> )  \ 1E 10 FOR28
\ SQRT[1+X]=1+X/2-1*X^2/(2*4)+1*3*X^3/(2*4*6)-...+(-1)^(N-1)1*3*…*(2*N-3)*X^(N)/ (2*4*…*(2*N))
X F! 1E 1E               \ X -> 1 1 , 1-ый сумма, 2-ой – заготовка первого члена
1+ 1                     \ пределы цикла
DO
    X F@ F* FNEGATE      \ S An -> S -An*X
    I 2* 3 – S>D D>F F*  \ S -An*X -> S -An*X*(2*I-3)
    I 2* S>D D>F F/      \ S -An*X*(2*I-3) -> S -An*X*(2*I-3)/(2*I)
    FSWAP FOVER          \ S -An*X*(2*I-3)/(2*I) -> -An*X*(2*I-3)/(2*I) S -An*X*(2*I-3)/(2*I)
    F+ FSWAP             \ -> S-An*X*(2*I-3)/(2*I) -An*X*(2*I-3)/(2*I)
\ FDUP F. FOVER F. CR    \ выводим текущие значения «An» и суммы
LOOP FDROP F. ;          \ удаляем текущий член печатаем сумму ряда
\ Стандартные тесты для корней из 1,1; 1,2; 1,5; 1,9 – чем ближе к единице, тем хуже точность при одних и тех же количествах итераций:
0.1E 10 FOR28
1.0488088  Ok
0.2E 10 FOR28
1.0954451  Ok
0.5E 10 FOR28
1.2247421  Ok
0.9E 10 FOR28
1.3769956  Ok
\ Те же корни, полученные стандартными средствами Форт:
1.1E FSQRT F.
1.0488088  Ok
1.2E FSQRT F.
1.0954451  Ok
1.5E FSQRT F.
1.2247449  Ok
1.9E FSQRT F.
1.3784049  Ok
\ Для корня из двух получаем следующие данные:
2E FSQRT F.
1.4142136  Ok
1E 10 FOR28
1.4099312  Ok
1E 50 FOR28
1.4138177  Ok
1E 100 FOR28
1.4140730  Ok
1E 500 FOR28
1.4142010  Ok
1E 1000 FOR28
1.4142091  Ok
1E 5000 FOR28
1.4142132  Ok
1E 10000 FOR28
1.4142134  Ok
1E 20000 FOR28
1.4142135  Ok
It took about 10,000 iterations to get six exact decimal places.

Example twenty nine. In line two we calculate the difference (B-A) - the length of the segment. In the third we transfer N to the real stack. In the fourth, calculating the quotient (B-A)/N, we obtain the length of each divided segment and in the fifth we display this on the screen. In the sixth we display the first point (coordinates A), then in the loop we add the length of the broken segment, we get the next point, and we display this value. After the loop, we delete the two remaining values ​​(the length of the segment and the last point).
: FOR29 ( I: N>1 F: A B [A<B] -> )  \ выводим: H A A+H ... A+N*H
FOVER F-                      \ F: A B -> A B-A
DUP S>D D>F                   \ I: N -> F: A B-A N
F/ FDUP                       \ F: A B-A N -> A (B-A)/N (B-A)/N
." Длина отрезка H = " F. CR  \ F: A (B-A)/N (B-A)/N -> A (B-A)/N
FSWAP FDUP F.                 \ A (B-A)/N -> (B-A)/N A
1+ 1                          \ I: N -> N+1 1
DO                            \ N+1 1 ->
    FOVER F+                  \ (B-A)/N A -> (B-A)/N A+(B-A)/N
    FDUP F.                   \ дублируем и печатаем «A+(B-A)/N»
LOOP FDROP FDROP ;            \ (B-A)/N A+(B-A)/N –>, очищаем вещественный стек после себя
\ Тест вышеописанного слова:
3 1.2E 1.5E FOR29
Длина отрезка H = 0.1000000
1.2000000 1.3000000 1.4000000 1.5000000  Ok
Example thirty. We write a new word called “F(X)”, which calculates the value of the function “1-sin(X)” and displays the result on the screen. The word “FOR30” itself is a twin of “FOR29”, the only difference is the replacement of the “F.” pin. (in lines six and ten on "F(X)").
: F(X) ( X -> )  \ вывести значение «1-sin(X)»
FSIN FNEGATE 1E F+ F. ;      \ X -> [1-sin(X)]
: FOR30 ( I: N>1 F: A B [A<B] -> )
FOVER F-                     \ F: A B -> A B-A
DUP S>D D>F                  \  I: N -> F: A B-A N
F/ FDUP                      \ F: A B-A N -> A (B-A)/N (B-A)/N
." Длина отрезка H= " F. CR  \ F: A (B-A)/N (B-A)/N -> F: A (B-A)/N
FSWAP FDUP F(X)              \ F: A (B-A)/N -> F: (B-A)/N A
1+ 1                         \ I: N -> N+1 1
DO                           \ N+1 1 ->
    FOVER F+                 \ (B-A)/N A -> (B-A)/N A+(B-A)/N
    FDUP F(X)                \ дублируем и обрабатываем «A+(B-A)/N»
LOOP FDROP FDROP ;           \ (B-A)/N A+(B-A)/N –>, очищаем вещественный стек после себя