# Middle(x,y) for DateTime, Char, and others

**URL:** <https://discourse.julialang.org/t/middle-x-y-for-datetime-char-and-others/96220>\
**Category:** Internals & Design\
**Created:** [March 17, 2023, 2:50am UTC](https://discourse.julialang.org/t/middle-x-y-for-datetime-char-and-others/96220 "2023-03-17T02:50:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 17, 2023, 2:50am UTC](https://discourse.julialang.org/t/middle-x-y-for-datetime-char-and-others/96220/1 "2023-03-17T02:50:56Z")

</div>

Should these methods exist?

```julia
  middle(x, y)

  Compute the middle of two numbers x and y, which is equivalent in both value and
  type to computing their mean ((x + y) / 2).

julia> middle(now(), now())
ERROR: MethodError: no method matching middle(::DateTime, ::DateTime)

julia> middle('a','c')
ERROR: MethodError: no method matching middle(::Char, ::Char)

```

- `Char` seems likely.
- For `DateTime`, which has 1 ms precision, I’m not sure what the rounding rules are.
- For `Date`, which has 1 day precision, likewise.
- Are there other types that should have these methods?

---

<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:** [March 17, 2023, 7:01pm UTC](https://discourse.julialang.org/t/middle-x-y-for-datetime-char-and-others/96220/2 "2023-03-17T19:01:58Z")

</div>

What would `middle('a','b')` return?

Considering that this function is in the Statistics module, I’m not sure it makes sense to define it for non-numeric types like `Char`.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [March 17, 2023, 7:28pm UTC](https://discourse.julialang.org/t/middle-x-y-for-datetime-char-and-others/96220/3 "2023-03-17T19:28:26Z")

</div>

> [@stevengj](#):
>
> Considering that this function is in the Statistics module, I’m not sure it makes sense to define it for non-numeric types like `Char`.

I don’t see that statistics applies only to numbers. Affine quantities like Dates and Fahrenheits can theoretically all compute midpoints by something like (though not exactly)

```julia
middle(x,y) = (x - y) / 2 + y

```

instead of

```julia
(x + y) / 2

```

> [@stevengj](#):
>
> What would `middle('a','b')` return?

I propose it would give an InexactError, as would `middle(today, tomorrow)`.

On the other hand, `middle(yesterday, tomorrow) == today` and `middle(32.0°F, 33.0°F) == 32.5°F` where currently it fails on division.

---

<div class="post-metadata">

**Author:** ![nalimilan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nalimilan/32/147_2.png) [@nalimilan](https://discourse.julialang.org/u/nalimilan)\
**Post date:** [March 28, 2023, 9:50am UTC](https://discourse.julialang.org/t/middle-x-y-for-datetime-char-and-others/96220/4 "2023-03-28T09:50:01Z")

</div>

For reference, see [middle is undefined for Date and DateTime (so median does not work) · Issue #47 · JuliaStats/Statistics.jl · GitHub](https://github.com/JuliaStats/Statistics.jl/issues/47).
