# Arithmetic with a specified (low) number of digits

**URL:** <https://discourse.julialang.org/t/arithmetic-with-a-specified-low-number-of-digits/86713>\
**Category:** Numerics\
**Tags:** float, rounding\
**Created:** [September 3, 2022, 1:04am UTC](https://discourse.julialang.org/t/arithmetic-with-a-specified-low-number-of-digits/86713 "2022-09-03T01:04:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [September 3, 2022, 1:04am UTC](https://discourse.julialang.org/t/arithmetic-with-a-specified-low-number-of-digits/86713/1 "2022-09-03T01:04:21Z")

</div>

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
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

```julia
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.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [September 3, 2022, 1:12am UTC](https://discourse.julialang.org/t/arithmetic-with-a-specified-low-number-of-digits/86713/2 "2022-09-03T01:12:22Z")

</div>

> [@DanielVandH](#):
>
> Is it possible to perform computations using, say, five-digit arithmetic with a given rounding mode?

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.

```jl
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.)
