Arithmetic with a specified (low) number of digits

Is it possible to perform computations using, say, five-digit arithmetic with a given rounding mode? For example, can I easily compute x/10^{-5}, x = 0.44446, in 5-digit arithmetic with rounding to the nearest number? I could easily do

julia> x = 0.44446;
julia> round(x/1e-5, digits = 5)
44446.0

but this would not be so reasonable for more involved computations. I also want to do things like compute the Cholesky factor of a matrix in four-digit arithmetic with chopped rounding, like the matrix

H = [1.000 0.5000 0.3333 0.2500
    0.5000 0.3333 0.2500 0.2000
    0.3333 0.2500 0.2000 0.1666
    0.2500 0.2000 0.1666 0.1428]

Is this possible?

I tried using ArbNumerics, although this seems to only allow for a high number of digits when I try to change the working precision.

Sure, you can set the precision of BigFloat to whatever you want, including very low numbers of digits. However, note that BigFloat is a fixed number of binary digits because it uses binary floating point.

julia> setprecision(BigFloat, 5, base=10); # set to roughly 5 decimal digits

julia> precision(BigFloat) # get actual number of binary digits
17

julia> H = BigFloat[1.000 0.5000 0.3333 0.2500
           0.5000 0.3333 0.2500 0.2000
           0.3333 0.2500 0.2000 0.1666
           0.2500 0.2000 0.1666 0.1428]

julia> cholesky(H)
Cholesky{BigFloat, Matrix{BigFloat}}
U factor:
4×4 UpperTriangular{BigFloat, Matrix{BigFloat}}:
 1.0  0.5      0.333302   0.25
  ⋅   0.28862  0.288784   0.259857
  ⋅    ⋅       0.0742636  0.110837
  ⋅    ⋅        ⋅         0.0221052

(You would need an arbitrary-precision decimal floating-point package to specify a precise number of decimal digits.)

3 Likes