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

Example 31: Convert temperature in Fahrenheit to Celsius. First for integer temperature values. : B31 (TF->TC)\TC=(TF-32)*5/9 32 – 5 * 9 / ; 32 B31 \ 32 degrees Fahrenheit = 0 degrees Celsius, 32-32=0, 0*5/9=0 OK (0) 35 B31 \ 35 degrees Fahrenheit = 1 degree Celsius (35-32)=3, 3*5=15, 15/9=1 – integer part Ok (0 1) 40 B31 \ 40 degrees Fahrenheit = 4 degrees Celsius (40-32)=8, 8*5=40, 40/9=4 – integer part Ok (0 1 4) With minor changes we can rewrite the code for real temperature values. : B31 (TF->TC)\TC=(TF-32)*5/9 32E F- 5E 9E F/F* ; 32E B31 F. \ as in the first option, only the result is in real format 0.0000000 Ok 321E-1 B31 F. 0.0555555 Ok

Example 32. The reverse problem to the previous example. Convert temperature from Celsius to Fahrenheit.

: B32 ( TC -> TF ) \ TF= TC*9/5+32
    9 * 5 / 32 + ;
0 B32 \ 0 degrees Celsius = 32 degrees Fahrenheit
 OK (32)
-18 B32
 Ok ( 32 0 ) \ -18 degrees Celsius = 0 degrees Fahrenheit

For a real argument.

: B32 ( TC -> TF ) \ TF= TC*9/5+32
    9E F* 5E F/ 32E F+ ;
0E B32 F. \ 0 degrees Celsius = 32 degrees Fahrenheit
32.000000 Ok

-18E B32 F. \ -18 degrees Celsius = -0.4 degrees Fahrenheit
-0.4000000 Ok

Zero degrees Celsius is still 32 degrees Fahrenheit, the only difference is that the result is a real number, but at T=-18 the remainder is no longer discarded, as in the first case, and the answer is accurate.

Example 33. Children's task. We denote the price of 1 kg of sweets by C1, and Y kg, respectively, by CY, then it is easy to calculate C1=A/X and CY=C1*Y, and given X kg for A rubles and the quantity Y kg, the price of which needs to be calculated.

: B33 ( X A Y -> C1 CY ) \ C1=A/X CY=C1*Y
    SWAP ROT / \ X A Y -> Y A/X=C1
    SWAP OVER *\Y C1 -> C1 Y*C1= CY
;
3 9 10 B33 \ 3 kg costs 9 rubles, i.e. 3 rubles for 1 kg, and 10 kg will cost 3 * 10 = 30 rubles
 Ok (3 30)

You can practice on your own using current prices of various goods.

And now for fractional prices.

: B33 ( X A Y -> C1 CY ) \ C1=A/X CY=C1*Y
    FSWAP FROT F/ \ X A Y -> Y A/X=C1
    FSWAP FOVER F*\Y C1 -> C1 Y*C1= CY
;
3E 9E 10E B33 F. F.
30.000000 3.0000000 Ok

We checked on the same data. And again, do not forget about the reverse order when printing from the stack, no matter which one (integer or real). To start displaying the price per 1 kg, you can use the word FSWAP before the “F.” or rewrite the line

FSWAP FOVER F*\Y C1 -> C1 Y*C1= CY

Thus

FDUP F. F* \ Y C1 -> Y*C1= CY

But in this case, the code is not universal, and therefore less preferable. It is advisable to separate calculation and printing so that the written function can be used in different places and situations, and not “reinvent the wheel” every time.

Example 34. Continuation of children's puzzles.

: B34 ( X A Y B -> CX CY CX/CY ) \ CX=A/X CY=B/Y CX/CY
    SWAP / \ X A Y B -> X A B/Y=CY
    SWAP ROT / \ X A CY -> CY A/X=CX
    SWAP 2DUP / \ CY CX -> CX CY CX/CY
;
5 30 5 15 B34
 Ok (6 3 2)

The price of chocolates is 30/5 = 6 r/kg, toffees 15/5 = 3 r/kg, and accordingly chocolates are 6/3 = 2 times more expensive than toffees. Let's rewrite it for a real argument so as not to lose accuracy for “inconvenient data”.

: B34 ( X A Y B -> CX CY CX/CY ) \ CX=A/X CY=B/Y CX/CY
    FSWAP F/ \ X A Y B -> X A B/Y=CY
    FSWAP FROT F/ \ X A CY -> CY A/X=CX
    FSWAP FOVER FOVER F/ \ CY CX -> CX CY CX/CY
;
5E 30E 5E 15E B34 F. F. F.
2.0000000 3.0000000 6.0000000 Ok

And again the order is reversed, the result is identical to the first option with integer arguments.

Example 35. Children's problem on the movement of a boat in still and moving water.

VARIABLE T2
: B35 ( V U T1 T2 -> S ) \ S=S1+S2, S1=V*T1, S2=(V-U)*T2
    T2! \ V U T1 T2 -> V U T1
    ROT DUP ROT * \ V U T1 -> U V V*T1=S1
    SWAP ROT -\U V S1 -> S1 V-U
    T2 @ * \ S1 V-U -> S1 [V-U]*T2=S2
    +\S=S1+S1
;

Stack manipulation becomes unnecessarily complicated with four or more elements, so we will store the value of T2 in a variable of the same name. First, we calculate the path S1 in still water, then read the time T2 from the variable - path S2 and, accordingly, the speed. The answer is the sum of the two paths. Let's check the written word.

10 5 1 1 B35
 OK (15)

1 hour in still water at a speed of 10 is a distance equal to 10*1=10 and the same time at a speed of 10-5=5. As a result, the total path is 10+5=15.

With real data, the word will look like:

FVARIABLE FT2
: B35 ( V U T1 T2 -> S ) \ S=S1+S2, S1=V*T1, S2=(V-U)*T2
    FT2 F! \ V U T1 T2 -> V U T1
    FROT FDUP FROT F* \ V U T1 -> U V V*T1=S1
    FSWAP FROT F-\U V S1 -> S1 V-U
    FT2 F@ F* \ S1 V-U -> S1 [V-U]*T2=S2
    F+\S=S1+S1
;
10E 5E 1E 1E B35 F.
15.000000 Ok

Based on the previous data, as expected, it gives the same result, but with the float type.

Example 36. Without comments, we will immediately give the solution.

: B36 ( V1 V2 S0 T -> S ) \ S=S0+(V1+V2)*T
    2SWAP + * + \ V1 V2 S0 T -> S0+T*(V1+V2)
;
3 5 100 2 B36
 OK (116)
\ 3+5=8, 8*2=16, 16+100=116.
FVARIABLE FS0
: B36 ( V1 V2 S0 T -> S ) \ S=S0+(V1+V2)*T
    FSWAP FS0 F! \ V1 V2 S0 T -> V1 V2 T
    FROT FROT F+ F* \ V1 V2 T -> T*(V1+V2)
    FS0 F@ F+ \ T*(V1+V2)+S0
;
3E 5E 100E 2E B36 F.
116.00000 Ok

Based on previous experience, we introduce a new variable to reduce the number of elements on the real stack. The answer coincides with the first option, only in real format, which confirms the correctness of the test.

Example 37. Absolutely similar to the previous example, with the exception of two points - the sum is replaced by the difference, and the difference is placed in the module.

: B37 ( V1 V2 S0 T -> S ) \ S=|S0-T*(V1+V2)|
    2SWAP + * - ABS \ V1 V2 S0 T -> |S0-T*(V1+V2)|
;
3 5 100 2 B37
 OK (84)
\ 3+5=8, 8*2=16, |100-16|=84. Let's check how the module works; for this we set S0=10.
3 5 10 2 B37
 OK (6)
\ 3+5=8, 8*2=16, |10-16|=6. That's right, the code works correctly.
FVARIABLE FS0
: B37 ( V1 V2 S0 T -> S ) \ S=|T*(V1+V2)-S0|
    FSWAP FS0 F! \ V1 V2 S0 T -> V1 V2 T
    FROT FROT F+ F* \ V1 V2 T -> T*(V1+V2)
    FS0 F@ F- FABS \ |T*(V1+V2)-S0|
;
3E 5E 100E 2E B37 F.
84.000000 Ok
3E 5E 10E 2E B37 F.
6.0000000 Ok

For the real argument, we received the same answers in the appropriate format.

Example 38. A trivial school problem that we will solve immediately in real form. If necessary, anyone can write the code for the entire argument themselves.

: B38 ( A B -> X ) \ X=-B/A
    -1E F* FSWAP F/ \ A B -> -B/A
;
10E 3E B38 F.
-0.3000000 Ok

Example 39. Solving a quadratic equation. Note that the "F" prefixes are omitted from the names of real variables, since these words only use variables of one type. If you are confused, you can easily add them. If you add all the words to one file, then this problem will be relevant, you can also add the prefix I (Integer) to the names of words, for functions with integer arguments and F, with real ones, otherwise SP-Forth will display the messages “B39 isn't unique()", which is not an error, but a warning that when calling a word, only the specific last word will be called. Hidden errors may occur.
So, if you define an integer word, then a real one, and then execute the words alternately for different arguments, then, as was said, only the last implementation will be called, which will inevitably lead to an error due to the fact that the words will work with arguments on different stacks, as a result, there will be either a lack of arguments or corruption of other data not intended for this. By changing the names using prefixes, you will get rid of these problems.

FVARIABLE A
: B39 ( A B C -> X1 X2) \ X1,X2=(-B+-SQRT(D))/2*A
    FROT FDUP A F! F*\A B C -> B C*A
    FOVER FDUP F* \ B C*A -> B C*A B^2
    FSWAP 4E F* F- FSQRT \ B C*A B^2 -> B SQRT{B^2-4*C*A=D}
    FSWAP -1E F* FSWAP \ B D^0.5 -> -B D^0.5
    FOVER FOVER F+ A F@ \ -B D^0.5 -> -B D^0.5 –B+D^0.5 A
    2E F* F/ \ -B D^0.5 –B+D^0.5 A -> -B D^0.5 [–B+D^0.5]/[A*2]=X1
    FROT FROT F- A F@ \ -B D^0.5 X1 -> X1 –B-D^0.5 A
    2E F* F/ \ X1 –B-D^0.5 A -> X1 [–B-D^0.5]/[A*2]=X2
    FOVER FOVER F< \ X1 X2 X1<X2?
    IF FSWAP THEN
;
1E 4E 1E B39 F. F.
-3.7320508 -0.2679491 Ok

X^2+4*X+1=0, D=4^2-4*1*1=16-4=12. X1,X2=(-4+-12^0.5)/(2*1)=-2+-SQRT(3)

X1=-2- 1.732= -3.732, X2=-2+1.732=-0.268.

But here is the case when D=0

1E 2E 1E B39 F. F.
-1.0000000 -1.0000000 Ok

If manipulations with the stack are not clear to you, then we can rewrite the last word using 3-variables A, B, C, according to the classical scheme.

FVARIABLE A
FVARIABLE B
FVARIABLE C
: B39 ( A B C -> X1 X2) \ X1,X2=(-B+-SQRT(D))/2*A, X1,X2
    C F! B F! A F! \A B C ->
    B F@ FDUP -1E F* FSWAP FDUP F* \ -B B^2
    A F@ C F@ F* -4E F* F+ FSQRT \ -B B^2 -> -B {B^2+A*C*(-4)}^0.5=D^0.5
    FOVER FOVER F+ \ -B D^0.5 -> -B D^0.5 -B+D^0.5
    A F@ 2E F* F/ \ -B D^0.5 -B+D^0.5-> -B D^0.5 [-B+D^0.5]/[A*2]=X1
    FROT FROT F- \ -B D^0.5 X1 -> X1 -B-D^0.5
    A F@ 2E F* F/ \ X1 [-B-D^0.5]/[A*2]=X2
    FOVER FOVER F< \ X1 X2 X1<X2?
    IF FSWAP THEN
;
1E 4E 1E B39 F. F.
-3.7320508 -0.2679491 Ok

The answer is the same as in the first case. Decide for yourself which one is simpler and clearer.

Example 40. The problem of solving a system of two linear equations in two unknowns. Let's skip the option with integer arguments and move on to the general case.

FVARIABLE A1 FVARIABLE B1 FVARIABLE C1
FVARIABLE A2 FVARIABLE B2 FVARIABLE C2
: B40 ( A1 B1 C1 A2 B2 C2 -> X Y ) \ X=(C1*B2-C2*B1)/D, Y=(A1*C2-A2*C1)/D, D=A1*B2-A2* B1
    C2 F! B2F! A2 F! C1 F! B1 F! A1 F! \ A1 B1 C1 A2 B2 C2 ->
    A1 F@ B2 F@ F* A2 F@ B1 F@ F* F- \ -> A1*B2-A2*B1=D
    FDUP S” D= “ TYPE FDUP F. CR \ D -> D D
    C1 F@ B2 F@ F* C2 F@ B1 F@ F* F- FSWAP F/ \ D D -> D (C1*B2-C2*B1)/D=X
    A1 F@ C2 F@ F* A2 F@ C1 F@ F* F- FROT F/ \ D X -> X (A1*C2-A2*C1)/D=Y
;

Here we created 6 new real variables so as not to get confused in the logic of the coefficients.

The first line is a standard description of the logic of how the word works.
The second is saving values ​​on the stack into the corresponding variables. At the top is the value of the last entered one, which means we save it first.
Calculation D.
We duplicate D, since we will need it twice, and we also copy and print it to check intermediate calculations. After debugging, the code “S” D= “TYPE FDUP F. CR” can be deleted.
Calculation X
Y calculation
Let's check the correctness of the word using the following data:

   3x+5y=7 (A1=3 B1=5 C1=7)

   6x+y=4 (A2=6 B2=1 C2=4)

3E 5E 7E 6E 1E 4E B40 F. F.
D= -27.000000
1.1111111 0.4814814 Ok

D=3*1-6*5=3-30=-27, coincides with the result given by the word. X=(7*1-4*5)/(-27)=13/27=(0.481). Y=(3*4-6*7)/(-27)=(12-42)/(-27)=30/27=1,(1). We see that the word works correctly.