Written by Mr. Bientor Laquille http://www.arduino-forth.com
In a few words, with FORTH, you can do arithmetic operations on fractions and have the result as a fraction. A possibility that offers almost no calculator or spreadsheet.
Fractional operations are on the program of average courses one and two of the primary school. We propose to define words allowing to operate on fractions and to restore fractional results.
Everyone knows that
1/3 is more accurate than 0.33333333 .... So
3 * (1/3) gives 1. But using a conventional calculator does never gives an exact result, because the intermediate calculus will restore an infinite development.
If the calculation of certain fractions gives a limited development during the division of the two members of the fraction, the results having infinite development remain the the beast of purists. In FORTH, a gifted language for the treatment of integers, the definition of fractional operators is performed in about thirty program lines.
Finding the GCD
PGCD looks for the greatest common divisor of two numbers 16-bit integers
PGCD ( n1 n2 --- pgcd)
\ 11 121 PGCD . affiche 11
\ 30 42 PGCD . affiche 6
PPCM looks for the smallest common multiple of two integer numbers
The LCM makes it possible to determine the first multiple common to two numbers. This routine is not used in fractional operators.
Inversion of fraction
The inverse of a fraction consists in permuting numerator and denominator.
FRINV executes a simple SWAP.
Fraction reduction transforms a fraction by removing common divisors:
REDUIT ( n d --- n2 d2)
\ 11 121 REDUIT . . affiche 1 11 ( 11/121 = 1/11 )
\ 30 42 REDUIT . . affiche 5 7 ( 30/42 = 5/7 )
Multiplication and division of two fractions
The multiplication of fraction consists in multiplying member to member elements of each fraction:
FR* ( fr1 fr2 --- fr1*fr2)
FR/ ( fr1 fr2 --- fr1/fr2)
FRINV FR* ;
The product result of two fractions is automatically reduced. In the case of the product of two fractions consisting of numerators and significant denominators, it is strongly recommended to operate a reduction of fraction before operating the product of the fractions.
11 121 REDUIT 30 43 REDUIT FR*
The division of two fractions consists in making the product of a fraction by the inverse of the second fraction.
Addition and subtraction of two fractions
The sum of two fractions with the same denominator is equal to the sum of the numerators on the denominator. If in a calculation the two denominators are multiples, multiply the fraction with the smallest denominator by the number needed to have two fractions of the same denominator.
addition of fractions
FR+ ( f1 f2 --- fr1+fr2)
FRMINUS ( fr --- -fr)
FR- ( fr1 fr2 --- fr1-fr2)
FRMINUS FR+ ;
Variable processing and fractional constants
\ gestion de variables et constantes fractionnaires
FR@ ( adr --- n d)
FR! ( n d adr ---)
FRVAR ( --- <mot>)
FRCONST ( n d --- <mot>)
These words are used to store fractions in variables or constants. Example:
355 113 FRCONST FR-PI \ constante de PI sous forme fractionnaire
\ approchée à 6 décimales
Here is what would give an angular value of a fraction of the PI constant of 1 degree:
FR-PI 1 180 FR* . . display 71 4068 (PI/180 = 71/4068 = 0.017453294)
On a calculator, using the preprogrammed PI constant, we obtain PI/180 = 0.01745329251
The exploration of fractions is a very interesting branch of arithmetic. Calculators classics will be frustrating to analyze the very interesting properties of this universe.
We see, through the last example, that it is possible to work with an approximate value of PI and obtain results with amazing precision.
original document : -- Mr. Bientor Laquille -- 2019