Translated using translate google. The original language is Russian. Original page https://hi-aga.ru/index.php/homepage/begin-11-20

Example 11. Differs from example 10 with minor amendments. We simply replace the square with a module: the code “DUP *” with “ABS”.
: B11 ( A B -> {|A|+|B|} {|A|-|B|} {|A|*|B|} {|A|/|B|} )
    SWAP ABS SWAP ABS        \ A B ->|A| |B|
    2DUP +                   \ |A| |B|-> |A| |B| (|A|+|B|)
    ROT ROT 2DUP –           \ |A| |B| (|A|+|B|) -> (|A|+|B|) |A| |B| (|A|-|B|)
    ROT ROT 2DUP *           \ (+) |A| |B| (-) -> (+) (-) |A| |B| (|A|*|B|)
    ROT ROT /                \ (+) (-) |A| |B| (*)-> (+) (-) (*) (|A|/|B|)
;
In the case of real arguments:
: B11 ( A B -> {|A|+|B|} {|A|-|B|} {|A|*|B|} {|A|/|B|} )
   FSWAP FABS                 \ A B -> B |A|
    FSWAP FABS                 \ B |A| -> |A| |B|
    FOVER FOVER F+             \ |A| |B|-> |A| |B| (|A|+|B|)
    FROT FROT FOVER FOVER F-   \ |A| |B| (|A|+|B|) -> (|A|+|B|) |A| |B| (|A|-|B|)
    FROT FROT FOVER FOVER F*   \ (+) |A| |B| (-) -> (+) (-) |A| |B| (|A|*|B|)
    FROT FROT F/               \ (+) (-) |A| |B| (*)-> (+) (-) (*) (|A|/|B|)
;
In comments (brackets), the corresponding sum, difference, product and quotient are placed in curly brackets to visually highlight elements on the stack. Regular parentheses cannot be used in this case, since they denote a comment and are reserved words for Forth and the SP-Forth programming system in particular. Test yourself to see if the written words work correctly. Example 12. Calculate the hypotenuse and perimeter of a right triangle from its legs. Since we are dealing with square roots, we will immediately present the code for the case of a real argument.

: B12 ( A B -> C P ) \ C=Square_Root(A^2+B^2) P=A+B+C
   FOVER FDUP F* \ A B -> A B A^2
   FOVER FDUP F* \ A B A^2 -> A B A^2 B^2
   F+ FSQRT \ A B A^2 B^2 -> A B Square_Root(A^2+B^2)=C
   FROT FROT F+ \ A B C -> C A+B
   FOVER F+ \ C A+B -> C A+B+C=P
;

Let's check on a right triangle with legs 3 and 5:

3E 4E B12 F. F. \ call our subroutine and print the result
12.000000 5.0000000 Ok

3^2+4^2=25. Square root of 25=5. 5+3+4=12– which is true. In this case, a Pythagorean triple was specially selected for ease of verification. Let's check the general case:

3E 5E B12 F. F.
13.830952 5.8309519 Ok

You can check the validity of the test yourself.

Example 13. Find the areas of two circles (with a common center) and the ring between them. The radii R1 and R2 are given, and R1 > R2. As before, we first write a word for integer numbers. If it is not entirely clear why not immediately write a universal version for real data, then I will explain: debugging in this case is most simple for complex words and for novice programmers, since all data on the stack is visible immediately after they are entered, it is possible to check and understand the operation of the code entering command after command. Operators for working with real numbers do not have this advantage. After writing a word with integer arguments, it is not difficult to translate its code to work with real ones and get a result of the same type.

: B13 ( R1 R2 -> S1 S2 S3) \ S1=Pi*R1^2 S2= Pi*R2^2 S3=S1-S2
   SWAP DUP * 314 * \ R1 R2 -> R2 (Pi*R1^2)=S1
   SWAP DUP * 314 * \ R2 S1 -> S1 (Pi*R2^2)=S2
   2DUP - \ S1 S2 -> S1 S2 (S1-S2)=S3
;

Let's run our word using the example of two circles with radii 25 and 15, respectively.

25 15 B13
 Ok (196250 70650 125600)

Above is a version of the code with integer arguments, and all 3 areas are 100 times larger due to the fact that we took Pi equal to 314. Let's rewrite the example for the case of real arguments.

: B13 ( R1 R2 -> S1 S2 S3) \ S1=Pi*R1^2 S2= Pi*R2^2 S3=S1-S2
   FSWAP FDUP F* 314E-2 F*\R1 R2 -> R2 (Pi*R1^2)=S1
   FSWAP FDUP F* 314E-2 F*\R2 (Pi*R1^2)=S1 -> (Pi*R1^2)=S1 (Pi*R2^2)=S2
   FOVER FOVER F-\S1 S2 -> S1 S2 (S1-S2)=S3
;

Read more 13:

25E-1 15E-1 B13 F.F.F.
12.560000 7.0650000 19.625000 Ok

S1 = 19.625 = 3.14*2.5^2; S2 = 7.065 = 3.14*1.5^2; S3=S1-S2=12.56=19.625-7.065. Testing was successful. Don't forget about the reverse order of printing from the stack. The written word works correctly according to the stack notation. If you need a different order of output, you can adjust the word yourself by adding the code after calling “B13” and before outputting “F. F.F."

Example 14. Determine the radius of the circle and the area of ​​the circle using its length. Let’s immediately compile a program for the real argument, because integer coarsening will give an unacceptable result for small values ​​of the circumference.

: B14 ( L -> R S ) \ R=L/(2*Pi) S=Pi*R^2
   628e-2 F/ \ L -> R=L/6.28 where 6.28=2*Pi=D
   FDUP FDUP F* 314e-2 F* \ R -> R Pi*R^2
;

Let's calculate R and S for L=25.37

2537E-2 B14 F. F.
51.244976 4.0398089 Ok

R=25.37/6.28= 4.0398 and S=3.14* 4.0398^2= 51.244. The test was successful.

Example 15. Knowing the area of ​​a circle, calculate its diameter and length.

: B15 ( S -> D L ) \ D=Square_Root(4*S/Pi) L=Pi*D
   4E F*\S -> 4*S
   314E-2 F/ \ 4*S -> 4*S/Pi
   FSQRT \ 4*S/Pi -> Square_Root(4*S/Pi)=D
   FDUP 314E-2 F* \ D -> D D*Pi=L
;

Let's calculate the diameter and length of a circle with an area equal to 12.345.

12345E-3 B15 F. F.
12.452036 3.9656166 Ok

The square root of (12.345*4/3.14) is equal to 3.965616, and 3.965616*3.14=12.4520, that is, TRUE. The example is quite simple and there is no other reason to write code for the integer version of the arguments. If necessary, it is easy to solve this problem yourself.

Example 16. Let's calculate the distance between two points on the number axis, knowing the coordinates.

: B16 ( X1 X2 -> |X1-X2| )
   - ABS\X1 X2 -> |X1-X2|
;

For real arguments.

: B16 ( X1 X2 -> |X1-X2| )
   F-FABS\X1 X2 -> |X1-X2|
;
31E-1 -12E1 B16 F.
123.10000 Ok \ |3.1-(-120)|=123.1

Example 17. Using three coordinates on the number axis (X1, X2, X3), calculate the following distances: |x1-x3|, |x2-x3| and their amount. First for integers.

: B17 ( X1 X2 X3 -> |x1-x3| |x2-x3| {|x1-x3|+|x2-x3|} )
   SWAP OVER \ X1 X2 X3 -> X1 X3 X2 X3
   - ABS\X1 X3 X2 X3 -> X1 X3 |X2-X3|
   ROT ROT – ABS SWAP \ X1 X3 |X2-X3| -> | X1-X3| |X2-X3|
   2DUP + \ | X1-X3| |X2-X3|-> | X1-X3| |X2-X3| (|X1-X3|+|X2-X3|)
;

For real ones.

: B17 ( X1 X2 X3 -> |x1-x3| |x2-x3| {|x1-x3|+|x2-x3|} )
   FSWAP FOVER \ X1 X2 X3 -> X1 X3 X2 X3
   F- FABS\X1 X3 X2 X3 -> X1 X3 |X2-X3|
   FROT FROT F– FABS FSWAP \ X1 X3 |X2-X3| -> | X1-X3| |X2-X3|
   FOVER FOVER F+ \ | X1-X3| |X2-X3|-> | X1-X3| |X2-X3| (|X1-X3|+|X2-X3|)
;

Test on coordinates

-1E1 1E-1 3E2 B17 F. F. F.
609.90000 299.90000 310.00000 Ok

|X1-X3|=|-10-300|=310; |X2-X3|=|0.1-300|=299.9; (|X1-X3|+|X2-X3|)=310+299.9=609.9.

Example 18. Similar to the previous task. The sum is replaced by the product.

: B18 ( X1 X2 X3 -> {|x1-x3|*|x2-x3|} )
   SWAP OVER \ X1 X2 X3 -> X1 X3 X2 X3
   - ABS\X1 X3 X2 X3 -> X1 X3 |X2-X3|
   ROT ROT – ABS *\X1 X3 |X2-X3| -> {|x1-x3|*|x2-x3|}
;
-5 2 7 B18
 OK (60)

|-5-7|*|2-7|= 12*5=60

For real numbers.

: B18 ( X1 X2 X3 -> {|x1-x3|*|x2-x3|} )
   FSWAP FOVER \ X1 X2 X3 -> X1 X3 X2 X3
   F- FABS\X1 X3 X2 X3 -> X1 X3 |X2-X3|
   FROT FROT F– FABS F* \ X1 X3 |X2-X3| -> {|x1-x3|*|x2-x3|}
;
-1E1 2E-1 23E1 B18 F.
55152.000 Ok

|-10-230|*|0.2-230|=240*229.8=55152

Example 19. Using the coordinates of the opposite vertices of a rectangle, calculate its perimeter and area, the sides are parallel to the coordinate axes.

: B19 ( X1 Y1 X2 Y2 -> P S ) \ P=2*[A+B] S=A*B
    ROT - ABS \ X1 Y1 X2 Y2 -> X1 X2 |Y2-Y1|
    SWAP ROT – ABS \ X1 X2 |Y2-Y1| -> |Y2-Y1|=A |X2-X1|=B
    2DUP + 2* \ A B -> A B 2*(A+B)=P
    ROT ROT * \ A B P -> P A*B=S
 ;
 1 3 7 8 B19 . .
 30 22 Ok

A=|1-7|=6 B=|3-8|=5. P=2*(A+B)=2*(6+5)=22. S=A*B=6*5=30.

The variant with real arguments is not much different from the integer one.

: B19 ( X1 Y1 X2 Y2 -> P S ) \ P=2*[A+B] S=A*B
    FROT F- FABS \ X1 Y1 X2 Y2 -> X1 X2 |Y2-Y1|
    FSWAP FROT F– FABS \ X1 X2 |Y2-Y1| -> |Y2-Y1|=A |X2-X1|=B
    FOVER FOVER F+ 2E F* \ A B -> A B 2*(A+B)=P
    FROT FROT F* \ A B P -> P A*B=S
 ;
 11E-1 15E-1 73E-1 62E-1 B19 F. F.
 29.140000 21.800000 Ok

A=|1.5-6.2|=4.7; B=|1.1-7.3|=6.2; P=2*(4.7+6.2)= 21.8; S=A*B=4.7*6.2= 29.14.

Example 20. Calculate the distance between two points on a plane using their coordinates. Since we will have to extract the square root, we skip the option with integer coordinates.

: B20 ( X1 Y1 X2 Y2-> R ) \ R= Square_Root((X2-X1)^2+(Y2-Y1)^2)
   FROT F- FDUP F* \ X1 Y1 X2 Y2-> X1 X2 (Y2-Y1)^2
   FSWAP FROT F- FDUP F* \ X1 X2 (Y2-Y1)^2 -> (Y2-Y1)^2 (X2-X1)^2
   F+ FSQRT \ (Y2-Y1)^2 (X2-X1)^2 -> R
 ;
 11E-1 15E-1 73E-1 62E-1 B20 F.
 7.7801028 Ok

A=|1.5-6.2|=4.7; B=|1.1-7.3|=6.2; R= Square_Root(A^2+B^2)= Square_Root(22.09+ 38.44)= 7.7801.