# Comparing type precision

**URL:** https://discourse.julialang.org/t/comparing-type-precision/50540
**Category:** General Usage
**Tags:** question
**Created:** [November 21, 2020, 4:50pm UTC](https://discourse.julialang.org/t/comparing-type-precision/50540 "2020-11-21T16:50:36Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![cadojo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cadojo/32/25328_2.png) [@cadojo](https://discourse.julialang.org/u/cadojo)
#### Post date: [November 21, 2020, 4:50pm UTC](https://discourse.julialang.org/t/comparing-type-precision/50540/1 "2020-11-21T16:50:36Z")

</div>

I’m building a `struct` that takes a collection of elements, and I want the inner constructor to parameterize the `struct` by the element type of the collection.

I showed what I’m looking for as the `max_precision` function in the example struct below. If all elements provided to the inner constructor **are** of the same type, then I just parameterize `System` by (the number of elements, and) the common type of the arguments. If all elements provided to the constructor **are not** of the same type, then I’d like to select the type of highest precision.

```nohighlight
using StaticArrays
struct System{N,T<:AbstractFloat}
  arr::SVector{N,T}

  # If all arguments are of the same type...
  function System(el::T...) where T<:AbstractFloat 
    return new{length(el), T}(SVector(el))
  end

  # Otherwise...
  function System(el...)
    float_type = max_precision(typeof.(el))
    new{length(el), float_type}(SVector(float_type.(el)))
  end

end

```

I actually think this is **not** possible, because how would a type know how precise it is relative to another type? But my knowledge here is limited, so I figured I’d post the question just in case.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [November 21, 2020, 5:06pm UTC](https://discourse.julialang.org/t/comparing-type-precision/50540/2 "2020-11-21T17:06:44Z")

</div>

I believe you are looking for type promotion. Take a look here: [Conversion and Promotion · The Julia Language](https://docs.julialang.org/en/v1/manual/conversion-and-promotion/#Promotion)

For example:

```julia
julia> x = promote(2, 3.1, 5//7, big(2.3))
(2.0, 3.100000000000000088817841970012523233890533447265625, 0.714285714285714285714285714285714285714285714285714285714285714285714285714282, 2.29999999999999982236431605997495353221893310546875)

```

There is also `promote_type` for working with types instead of values.
