Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It is never possible to represent all the numebr in a fixed length binary string.


You can still store a symbolic representation of those numbers that you can't represent though in limited cases (such as rational division) and write your algorithms so that it doesn't truncate it in the worst possible way.


I feel compelled to mention that you could just use a language with a proper numeric tower.

If the numbers that you input to an algorithm are exact, you get an exact answer. If they are inexact, you get an inexact answer. 399 999 999 999 999 is an exact number.

    guile> (- 399999999999 399999999998)
    $1 = 1


Another intriguing possibility is to represent all required numbers exactly, even sqrt(2). It is possible with lazy evaluation: essentially a number is represented by a function that computes the digits of its decimal expansion, and arithmetic operations take two functions and return a third. Inefficient but mind-blowing.


~$ sbcl

* (- 399999999999999 399999999999998)

1

* (- 12.52 12.51)

0.010000229

* (* 850 77.1)

65535.0




Consider applying for YC's Winter 2027 batch! Applications are open till November 2.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: