# Constants in generic code

**URL:** <https://discourse.julialang.org/t/constants-in-generic-code/18920>\
**Category:** General Usage\
**Tags:** constants\
**Created:** [December 22, 2018, 1:17pm UTC](https://discourse.julialang.org/t/constants-in-generic-code/18920 "2018-12-22T13:17:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bernard\_GODARD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bernard_godard/32/4155_2.png) [@Bernard\_GODARD](https://discourse.julialang.org/u/Bernard_GODARD)\
**Post date:** [December 22, 2018, 1:17pm UTC](https://discourse.julialang.org/t/constants-in-generic-code/18920/1 "2018-12-22T13:17:05Z")

</div>

I am wondering if there is a recommended way to deal with constants in generic code (ie supporting user types).

Example adding a rational f(x) = x+2/3

- Implementation 1:

```julia
f(x::T) where T = x + T(2)/T(3)

```

- Implementation 2:

```julia
f(x::T) where T = x .+ 2//3

```

- Implementation 3:

```julia
function f(x)
    T = typeof(x) 
    x + T(2)/T(3)
end

```

AFAIK implementation 1 and 3 are equivalent.  
Implementation 1 and 2 should have similar efficiency if the compiler can perform constant propagation.  
I would say implementation 2 is better since it works with arrays whereas implementation 1 and 3 do not.

Also for irrational. eg adding 2pi f(x) = x+2pi

Implementation 1:

- Implementation 1:

```julia
f(x::T) where T = x + 2*T(pi)

```

- Implementation 2:

```julia
f(x::T) where T = x + 2*(pi*one(T))

```

This does not work with arrays.

- Implementation 3:

```julia
f(x::T) where T = (x .+ pi) .+ pi

```

works with array but not very nice to look at.

Those are the criterions I can think of to decide on best approach:

- precision should match user type (BigFloat, Float64,…)
- ability to support extended types such as DualNumbers, ForwardDiff, Vectors and Arrays…
- efficiency
- code readability

Thank you

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 22, 2018, 1:36pm UTC](https://discourse.julialang.org/t/constants-in-generic-code/18920/2 "2018-12-22T13:36:32Z")

</div>

I think either the first or the third implementation is fine. Also, broadcasting over arrays should be an orthogonal issue, and I would not define the same method to both broadcast and work on scalars, unless there is a compelling reason.

Also, for some scalars which are composed from integers via operations that propagate type, it may be enough to just convert the first element, eg

```julia
f(x::T) where T = x + T(2)/3

```

Cf [the style guide](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-using-floats-for-numeric-literals-in-generic-code-when-possible-1).

Similarly,

```julia
f(x::T) where T = x + 2*T(pi)

```

would be my choice.
