Search code examples
factor-lang

Floating point rounding in Factor


I stumbled on the Factor language and got curious of stack based programming. Factor looks concise, uses a minimun of syntax and punctuation and offers an almost full-featured library for real world problem solving. At least enough for me to give it a try.

Before even getting started, I have a basic question on floating point implementation. How can I avoid this rounding issue when I want to output e.g. 8.12 in the Listener? I use a 32-bit Debian box.

IN: scratchpad 8.12

--- Data stack:
8.119999999999999

And why does it not show the same effect for other floats as well?

IN: scratchpad 8.23

--- Data stack:
8.23

Solution

  • It is because in binary floating point, the value 8.12 is not representable. It is an infinitely long decimal expansion. Similar to how the rational 1/3 is 0.333... as a decimal, 203/25 has an infinite number of digits in binary. So when you convert it back from binary to decimal for printing, you get rounding errors.

    It's a known issue, see: https://github.com/slavapestov/factor/issues/1158. But it is only a cosmetic one and doesn't affect calculations.