# Float64 is typecasted to float16

**URL:** <https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441>\
**Category:** New to Julia\
**Tags:** type\
**Created:** [July 21, 2020, 6:18pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441 "2020-07-21T18:18:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![sudo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sudo/32/16342_2.png) [@sudo](https://discourse.julialang.org/u/sudo)\
**Post date:** [July 21, 2020, 6:18pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/1 "2020-07-21T18:18:04Z")

</div>

I am trying to calculate mean of some values. These values are of type Float16. When performing mean operation, I see the value to be Inf16. I tried to assign the result to a variable of type float64 as well, but it didn’t work out and I still see the type of the final mean as Float16. Can you suggest how to handle the result going out of the range of float16 here?

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [July 21, 2020, 6:19pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/2 "2020-07-21T18:19:17Z")

</div>

you need to convert before the calculation happens; convert the result wont work because the ‘true value’ is already lost.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 21, 2020, 6:42pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/3 "2020-07-21T18:42:12Z")

</div>

Or, if you do not want to convert the whole array to `Float64`, and you do not mind losing some performance of `sum`, you can just define your “own sum” that uses a Float64 as accumulator:

```julia
julia> a = rand(Float16, 1000);
julia> r = sum(a)
Float16(496.8)
julia> r2 = foldl(+, a; init = zero(Float64))
497.798828125

```

---

<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:** [July 21, 2020, 7:07pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/4 "2020-07-21T19:07:34Z")

</div>

> [@Henrique\_Becker](#):
>
> you can just define your “own sum” that uses a Float64 as accumulator:

`sum(Float64, a)` also works.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 21, 2020, 7:25pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/5 "2020-07-21T19:25:39Z")

</div>

Oh, ok. I did not know that `sum` could take a function as first parameter, it makes sense.

If the smaller type is always promoted before addition then the `sum(Float64, a)` seems the way to go.

---

<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:** [July 22, 2020, 11:44pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/6 "2020-07-22T23:44:26Z")

</div>

> [@sudo](#):
>
> I tried to assign the result to a variable of type float64 as well, but it didn’t work out and I still see the type of the final mean as Float16.

I think there’s some misunderstanding here. Variables don’t have a type, so you cannot assign anything to a “variable of type Float64”. If you assign a value of type Float16 to a variable, that will not change the type.

---

<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:** [July 23, 2020, 1:03am UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/7 "2020-07-23T01:03:37Z")

</div>

> [@DNF](#):
>
> Variables don’t have a type,

It is possible to [assign a type to a variable](https://docs.julialang.org/en/latest/manual/types/#Type-Declarations-1), in which case any assignment to that variable converts the right-hand side to that type.

---

<div class="post-metadata">

**Author:** ![ffevotte](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffevotte/32/6587_2.png) [@ffevotte](https://discourse.julialang.org/u/ffevotte)\
**Post date:** [July 23, 2020, 7:45pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/8 "2020-07-23T19:45:27Z")

</div>

> [@stevengj](#):
>
> `sum(Float64, a)` also works.

It looks like using `sum` as a higher-order function allocates in this case. If performance matters, a variant of this approach would be to use `reduce`, taking special care of initializing the accumulator to a `Float64` zero. This does not allocate and should be faster for not-too-large arrays:

```julia
julia> x = rand(Float16, 10_000);

# standard use of sum, everything in Float16
julia> @btime sum($x)
  198.709 μs (0 allocations: 0 bytes)
Float16(4.972e3)

# using sum as a higher-order function to convert each element to Float64
julia> s1(x) = sum(Float64, x)
julia> @btime s1($x)
  930.281 μs (29999 allocations: 468.73 KiB)
4976.8818359375

# using reduce with the accumulator initialized to a Float64 zero
julia> s2(x) = reduce(+, x, init=0.)
julia> @btime s2($x)
  32.444 μs (0 allocations: 0 bytes)
4976.8818359375

```

---

<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:** [July 23, 2020, 8:53pm UTC](https://discourse.julialang.org/t/float64-is-typecasted-to-float16/43441/9 "2020-07-23T20:53:34Z")

</div>

> [@ffevotte](#):
>
> It looks like using `sum` as a higher-order function allocates in this case.

It seems like this is a inference bug of some kind. If I pass a function instead of the `Float64` constructor, I get no allocations:

```julia
julia> s1(x) = sum(y -> Float64(y), x)
s1 (generic function with 1 method)

julia> @btime s1($x);
  41.182 μs (0 allocations: 0 bytes)

```

Filed as [julia#36783](https://github.com/JuliaLang/julia/issues/36783).
