SuperForth for the Tatung Einstein, showing a SuperForth 1.12 session defining a word
← Back to Courses
module
26

Decimal Points

Introduction

SuperForth's own numbers are whole, and stop at 32767. That is enough for counting, co-ordinates and scores, and it is fast. It is no good for the area of a circle.

The FLOAT extension adds numbers with a decimal point and an enormous range. They cost memory, and a minute and a half of loading, so load them when a program needs them and not otherwise.

Loading

NEWFILE FLOAT
1 LOAD

Twelve screens go past. Be patient.

Typing One

A floating number is typed in three parts: the digits, with a decimal point; the word E; and a power of ten.

3.14 E 0

is 3.14 times ten to the power nought, which is 3.14. 1.5 E 3 is 1500. The power is a convenience for very big and very small numbers; most of the time it is 0. Type the decimal point, even in 2. E 0: a whole number is accepted too, but the point is a good habit.

Printing One

F. prints a floating number, always in the same form - one digit, the point, eight more digits, and the power of ten:

3.14 E 0 F.
3.14000000 E 0 ok

Arithmetic

Every word has an F on the front: F+ F- F* F/. They work exactly like their whole-number cousins, numbers first and sign after.

A floating number takes up three places on the stack, not one, so the ordinary stack words would tear it apart. It has its own: FDUP, FDROP, FSWAP.

S->F turns an ordinary whole number into a floating one: 7 S->F.

The Trap

This is the one thing in the section you must not skip. E only works at the keyboard. Put 2. E 0 inside a definition and SuperForth accepts the definition without complaint - and then the word fails with a bare ? when you run it.

The reason is that E reads the power of ten from whatever is typed next, at the moment it runs. Inside a word, that moment is when the word runs, and what is typed next is nothing at all.

The way round it is to give the number a name first, at the keyboard, with FCONSTANT, and use the name inside the word:

3.14159 E 0 FCONSTANT MYPI

The Code

NEWFILE FLOAT
1 LOAD
10. E 0 4. E 0 F/ F.
3.14159 E 0 FCONSTANT MYPI
: AREA FDUP F* MYPI F* F. ;
2.5 E 0 AREA

Starting from

A fresh boot, with SuperForth just started.

What you should see

10. E 0 4. E 0 F/ F.
2.50000000 E 0 ok
3.14159 E 0 FCONSTANT MYPI
ok
: AREA FDUP F* MYPI F* F. ;
ok
2.5 E 0 AREA
1.96349375 E 1 ok

A division, a constant for pi, and the area of a circle of radius 2.5

1.96349375 E 1 is 19.6349375. AREA squares the radius with FDUP F* and multiplies by pi.

Square Roots And Angles

More of FLOAT lies further down the file:

14 LOAD

brings in FSQR, the square root, and SINE, COSINE and TANGENT, which take their angles in degrees. 60. E 0 COSINE F. prints a half and 45. E 0 TANGENT F. prints one, each give or take the last few digits. 30. E 0 SINE F. prints a half - or rather, 5.00000001 E -1.

How Far To Trust It

About nine digits. 6.02 E 23 F. prints 6.01999999 E 23, and the sine of 30 degrees comes out a whisker over a half. That is the nature of these numbers on any computer, and it has two consequences.

Never test floating numbers for being exactly equal. Test whether they are close.

And be ready for an oddity. One divided by three, times three, ought to be 1. F. prints it as 0.00000000 E -2. The arithmetic has produced something a hair under 1, and the printing word does not cope with it. When a result looks absurd, print the steps that led to it before you doubt your sums.

Change One Thing

  • Type 10 E 0 F. with no decimal point. Does SuperForth mind?
  • Define : DOUBLE-BAD 2. E 0 F* F. ; and try 2.5 E 0 DOUBLE-BAD. Then fix it.
  • Work out the area of a circle of radius 100. Then of radius 1000. What would have happened to those answers in whole numbers?
  • Type 1. E 0 3. E 0 F/ F. and then the same with 3. E 0 F* before the F.

Exercises

26.1 Define ROUND-IT to print the circumference of a circle from its radius: two times pi times r.

26.2 Load the second part and define HYP to print the longest side of a right-angled triangle from the other two. Try it on 3 and 4.

26.3 Print the sines of 0, 30, 60 and 90 degrees, one to a line, using a loop. You will need S->F.

Worked solutions are in Appendix II.

When It Goes Wrong

SymptomCause
A bare ? when a word runsThere is an E inside the definition. Make an FCONSTANT at the keyboard and use that.
FSQR? or SINE?Those are in the second part. 14 LOAD.
Rubbish after a DUP or SWAPA floating number is three cells. Use FDUP, FSWAP, FDROP.
0.00000000 E -2 where you expected about 1The printing word mishandles a value a hair under 1. Check the steps.

Summary

FLOAT adds numbers typed as 3.14 E 0 and printed by F., with F+ F- F* F/, their own stack words, and nine digits of precision; 14 LOAD adds roots and trigonometry in degrees. E works only at the keyboard, so inside a word use an FCONSTANT.

Next

S27, The Function Keys - making the row of keys along the top type for you.

Get the Newsletter

New guides, disk images and community finds, roughly once a quarter. No spam, we promise, this isn't Tatung's marketing department.
Your subscription could not be saved. Please try again.
Your subscription has been successful.

Newsletter

Subscribe to our newsletter and stay updated.