Is there any idea to solve the floating-number-precision-prob in the future?

206 views Asked by At

We can not store a decimal in infinite precision, but there may be some way to represent them just like we represent infinite lists in haskell.

The first idea came to me is to represent a decimal number via something similar to Codata, so that for any given natural number k, we can calculate the decimal number precise to k digits.

But there is some obvious problem, think about the number a = 0.333... and b = 0.666... , if we plus them together, we got ans = 0.999... (a sequence of digits) , but we can never tell whether a + b == 1 in this case.

What I want is, to define the decimal number somehow, so that it support the +, -, *, /, >, == operations, and no matter what +, -, *, / operation we applied to these decimal numbers, we get new decimal numbers, which we can calculate them precise to k digits given any natural number k.

I'm wondering: is there any idea which may work this out?

1

There are 1 answers

7
Daniel Wagner On

Haskell offers Rational, Cyclotomic, and CReal for doing exact arithmetic.

CReal, the computable reals, comes about as close as it's possible to come to representing real numbers on a machine; just about any stupid real number you can think up and describe can be stuffed into a CReal. The tradeoff of being able to represent so many things is that your power of observation is severely limited. Checking for equality, checking whether one is bigger than another, even knowing for sure what the first digit should be are technically undecidable problems; though the package provided on Hackage provides approximation algorithms for all of these observations.

Cyclotomic can represent a much smaller chunk of the reals, and can represent a fair chunk of the complex numbers, all while retaining exact computation, and supports many more observations in a decidable way. Much of the stuff you want to do can be done with cyclotomic numbers.

Rational represents all the numbers that can be written as fractions. This is a significantly smaller chunk of the reals than CReal or Cyclotomic: square roots (and other roots) are pretty much out, trigonometric functions are pretty much out, pi and e are out, etc. But the observations are correspondingly more efficient than for CReal and Cyclotomic, so they are sometimes just the ticket.

...of course, if efficiency matters, Double is going to blow all of these out of the water with today's hardware. Choose your poison carefully!