# Standardizing vector of Decimals

**URL:** <https://discourse.julialang.org/t/standardizing-vector-of-decimals/18142>\
**Category:** General Usage\
**Created:** [November 29, 2018, 2:51pm UTC](https://discourse.julialang.org/t/standardizing-vector-of-decimals/18142 "2018-11-29T14:51:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [November 29, 2018, 2:51pm UTC](https://discourse.julialang.org/t/standardizing-vector-of-decimals/18142/1 "2018-11-29T14:51:25Z")

</div>

I’m trying to add `Decimal` support to my database package; in order to load decimals into a table, this requires having a fixed Decimal definition such as the following:

`create table test (col1 Decimal(7,4));`

Within Julia, you can have `Decimal[]` where all of the exponents are different:

```julia
julia> decvec = Decimal[decimal(1.4549875498754), decimal(1.7), decimal(145498.75498754)]
3-element Array{Decimal,1}:
 Decimal(0, 14549875498754, -13)
 Decimal(0, 17, -1)
 Decimal(0, 14549875498754, -8)

```

Is there an already implemented way of standardizing the precision and scale? If this doesn’t exist and I do this myself, do I have to worry about floating-point error? I’m generally not a `Decimal` user, so I don’t want to make an erroneous assumption for those of you who care about exactness.

---

<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:** [November 29, 2018, 4:19pm UTC](https://discourse.julialang.org/t/standardizing-vector-of-decimals/18142/2 "2018-11-29T16:19:46Z")

</div>

> [@randyzwitch](#):
>
> Is there an already implemented way of standardizing the precision and scale? If this doesn’t exist and I do this myself, do I have to worry about floating-point error?

If you can bound the number of decimal digits you need, you can use decimal floating-point, e.g. with the DecFP.jl package. For example, the `Dec128` type can represent any decimal number with up to 34 significant digits exactly (within a huge range of exponents).

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [November 29, 2018, 4:40pm UTC](https://discourse.julialang.org/t/standardizing-vector-of-decimals/18142/3 "2018-11-29T16:40:59Z")

</div>

Thanks for pointing out the DecFP.jl package, I wasn’t aware of that.

For my problem, I’m trying to build support for what _other people_ might want to load, so I’m not sure I can restrict things too much (other than building types for each of `Decimal`, `Dec32`, `Dec64` and `Dec128`). For example, I anticipate a user having a DataFrame where some of the columns could be Decimal. So I’m imagining that I need to check all the values of the entire Decimal column to figure out the widest precision and scale, then set that in my `create table` statement.

Ultimately in the end I do need to pass an `Int64` to our database backend though, which is what drew me to the Decimals.jl package in the first place.

---

<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:** [November 29, 2018, 6:51pm UTC](https://discourse.julialang.org/t/standardizing-vector-of-decimals/18142/4 "2018-11-29T18:51:27Z")

</div>

Can you anticipate a user input with more than 34 accurate significant digits? Unless your users are doing number theory that would seem surprising.

---

<div class="post-metadata">

**Author:** ![randyzwitch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/randyzwitch/32/15955_2.png) [@randyzwitch](https://discourse.julialang.org/u/randyzwitch)\
**Post date:** [November 29, 2018, 7:20pm UTC](https://discourse.julialang.org/t/standardizing-vector-of-decimals/18142/5 "2018-11-29T19:20:54Z")

</div>

> [@stevengj](#):
>
> Unless your users are doing number theory that would seem surprising.

That’s my point though, I don’t know what people might try to do (as long as it’s possible in Julia). But I think we’re talking about two different things.

From what I see from the DecFP package, arrays are auto-promoted to the widest type, which is great and makes the problem described in the original question moot:

```julia
julia> a = [Dec32(1.4567), Dec64(1.3456767645), Dec128(1000.459696)]
3-element Array{Dec128,1}:
    1.4567
    1.3456767645
 1000.459696000000008098140824586153

```

But if someone passes an arbitrary precision `Decimals[]` vector, am I correct that I have to inspect every value and scale them all to the same precision? If so, does that actually work or will I introduce a numerical error (and thereby ruining the original Decimal value)?
