The VikiD Truth

Knowledge base / Computing / Lisp family

Picolisp | Picolisp Machine | pil sources

Working With Numbers

Fixed-Point vs Floats

Read About It

There are a few in built functions that help us deal effectively with Numbers

Arithmetic

Conversion / Reprsentation

format, pad, hex, oct, bin and round.

The plain idea

PicoLisp handles numbers in a way that trips up newcomers: it has no floating-point type at all. Instead it uses arbitrary-precision integers (bignums) and does “decimals” with fixed-point arithmetic, scaled integers. A price of 19.95 is stored as the integer 1995 with an understanding that there are two decimal places. The built-in helpers, format, round, */, and the base converters hex, oct, bin, pad, are how you work with and display these numbers.

Why it matters

Floating point is fast but inexact: the classic 0.1 + 0.2 does not equal 0.3 on most systems (see Mathematics on Computers). That rounding error is unacceptable for money and accounting, which is exactly what PicoLisp was built for. By using scaled integers, PicoLisp gives you exact decimal arithmetic, 19.95 + 0.05 is precisely 20.00, every time. It is a deliberate design choice that trades a little convenience for a lot of correctness.

An everyday analogy

Instead of measuring in metres and fractions of a metre (where the fractions get fuzzy), PicoLisp measures everything in whole millimetres and just remembers where the decimal point goes. There are no “half-millimetres” to round awkwardly; every quantity is a clean whole number of the smallest unit, and the display puts the point back in at the end.

A worked example: fixed-point in action

(scl 2)                 # work to 2 decimal places
(+ 1995 5)              # -> 2000  (that's 19.95 + 0.05)
(format 2000 2)         # -> "20.00"   (format inserts the point)
(format 123456 2 ".")   # -> "1234.56"
(*/ 1995 3 1)           # fixed-point multiply/divide with rounding

scl sets the scale (how many decimal places the reader applies to literals like 19.95), format turns a scaled integer back into a human string, and */ multiplies then divides with proper rounding, the workhorse for percentages and tax. For base conversions, (hex 255) gives "FF" and (bin 5) gives "101".

Common misconceptions

How it connects

The truth revealed: PicoLisp refuses floating point on purpose, and once you see that it is counting in whole smallest-units and only painting the decimal point on at the end, its arithmetic stops being odd and starts looking wiser than the default.

Last edited · 6 revisions

Tags: #Computing