FLOATS

A float is a number with a point in it: a price, a measurement, an average. In satellite it is satellite.variable.float, and satellite.variable.double is its second spelling. You write one the way you read it: 12.34, 0.5, -2.75.

Floats you can trust

In most languages a float is stored in binary, and that makes simple sums come out slightly wrong: 0.1 + 0.2 gives something like 0.30000000000000004. A satellite float is decimal all the way down. It is two whole numbers put together, the digits before the point and the digits after it, so 0.1 + 0.2 is exactly 0.3, and comparing them says so.

prices.satlSATELLITE 004 Download
Sums on floats come out exact
satellite.include(satellite)

satellite.capsule satellite.main()
{
    satellite.variable.float price = 12.05
    satellite.variable.double half = 0.5
    satellite.console.display(price * 2)
    satellite.console.display(price + half)
    satellite.console.display(10.0 - 0.01)
    satellite.console.display(0.1 + 0.2)
    satellite.console.display(0.1 + 0.2 == 0.3)
    satellite.console.display(price < 13)
    satellite.return(satellite)
}
OUTPUT
24.1
12.55
9.99
0.3
true
true

+, -, *, /, % and ^ all work between two floats, and between a float and a whole number, either way round. The comparisons ==, !=, <, >, <= and >= work too.

When a float appears

A float appears in three ways:

  1. You write a number with a point in it.
  2. You give a whole number to a float name: satellite.variable.float f = 3 holds 3.0.
  3. An answer cannot be a whole number: 2 ^ -1 is one half, so it answers the float 0.5.

Dividing two whole numbers is still whole-number division: 4 / 3 is 1. To get the float, write one side with a point, 4.0 / 3.

dividing.satlSATELLITE 004 Download
Whole-number division, and the float beside it
satellite.include(satellite)

satellite.capsule satellite.main()
{
    satellite.console.display(4 / 3)
    satellite.console.display(4.0 / 3)
    satellite.console.display(2.0 / 3)
    satellite.console.display(2 ^ -1)
    satellite.variable.float f = 3
    satellite.console.display(f)
    satellite.console.display(12.50)
    satellite.return(satellite)
}
OUTPUT
1
1.33333333333333333333333333333333
0.66666666666666666666666666666667
0.5
3.0
12.5

How many digits

A float has no size limit, but a division like 4.0 / 3 never ends, so it has to stop somewhere. By default a float keeps 128 places after the point and 4096 digits before it. Where a result has more digits than that, the last digit kept is rounded half away from zero. That is the only time a float’s value is rounded.

When a float is shown, satl writes up to 32 places after the point, however many it holds, and rounds the last place it writes; the float itself keeps every digit. That is why 2.0 / 3 shows a 7 at the end. A float shows no zeros it does not need, so 12.50 shows as 12.5, and one that is whole keeps a single zero, so 3.0 stays 3.0.

A program can read the two limits through the arguments variable, as arguments.float.decimal and arguments.float.whole:

digits.satlSATELLITE 004 Download
Reading how many digits a float keeps
satellite.include(satellite)

satellite.capsule satellite.main(satellite.variable.arguments arguments)
{
    satellite.console.display(arguments.float.decimal)
    satellite.console.display(arguments.float.whole)
    satellite.return(satellite)
}
OUTPUT
128
4096

A program reads these and cannot change them. You set them for your own computer in ~/.satl/config.ini, one per line, and any count of 1 or more works:

config.iniTEXT Download
Keep more digits on this computer
float.decimal = 4096
float.whole = 20000

See THE ARGUMENTS VARIABLE for everything else it holds.

Floats and whole numbers

A float given to a whole-number name is refused: the message says it “holds a float”. You can turn a float into a whole number with .number, but only when there is nothing after the point: 12.0.number is 12, and 12.5.number is refused with S402, because satellite rounds nothing on its own. .string gives a float as text, so price.string + " dollars" works.

Not built yet

  • A power that is not a whole number, such as 2.0 ^ 0.5, is refused: only whole-number powers are built.
  • There is no scientific notation (1.5e3), and a float needs a digit before its point (0.5, not .5).
  • A float meeting a percentage, a fraction, a binary, a hex, a color or an infinity is refused as not built yet.

Where next

The FULL REFERENCE lists every float word. Whole numbers are in NUMBERS, and exact parts of a whole written with a slash are in FRACTIONS.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *