# Fraction operations

# in Forth

### 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. *

# Fraction operators

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.

## Fractional operators

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

The word `PGCD`

looks for the greatest common divisor of two numbers 16-bit integers

:`PGCD`

( n1 n2 --- pgcd)

` `**2DUP** <

` `**IF** **SWAP** **THEN**

` `**BEGIN**

` `**SWAP** **OVER** **MOD** **DUP** **0=**

` `**UNTIL**

` `**DROP** **;**

*\ 11 121 PGCD . affiche 11*

*\ 30 42 PGCD . affiche 6*

### LCM search

The word `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.

The word `FRINV`

executes a simple SWAP.

### Fraction reduction

Fraction reduction transforms a fraction by removing common divisors:

:`REDUIT`

( n d --- n2 d2)

` `**2DUP** PGCD

` `**DUP** **ROT** **SWAP** **/** **>R** **/** **R>** **SWAP** **;**

*\ 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)

` `**SWAP** **>R** *****

` `**SWAP** **R>** ***** **SWAP** REDUIT **;**

:`FR/`

( fr1 fr2 --- fr1/fr2)

` FRINV FR* `**;**

Exemple:

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.

Example:

` `*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.

Example:

### 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)

` `**>R** **OVER** *****

` `**ROT** **R@** ***** **+**

` `**SWAP** **R>** ***** REDUIT **;**

:`FRMINUS`

( fr --- -fr)

` `**SWAP** **NEGATE** **SWAP** **;**

:`FR-`

( fr1 fr2 --- fr1-fr2)

` FRMINUS FR+ `**;**

Exemple:

` `*3* *4* *4* *5* FR+ **.** **.** *\ affiche 31 20 (3/4 + 4/5 = 31/20)*

` `*3* *4* *4* *5* FR- **.** **.** *\ affiche 1 -20 (3/4 - 4/5 = -1/20)*

### Variable processing and fractional constants

*\ gestion de variables et constantes fractionnaires*

:`FR@`

( adr --- n d)

` `**2@** **;**

:`FR!`

( n d adr ---)

` `**2!** **;**

:`FRVAR`

( --- <mot>)

` `**2VARIABLE** **;**

:`FRCONST`

( n d --- <mot>)

` `**2CONSTANT** **;**

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*

`FR-PI `*3* *4* FR* **.** **.** *\ affiche 1065 452 (PI * 3/4) *

`FR-PI `*2* **1** FR* **.** **.** *\ affiche 710 113 (PI * 2 ou PI * 2/1)*

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**

## Conclusion

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