# Converting default Float type to Float32

**URL:** <https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326>\
**Category:** Performance\
**Tags:** question\
**Created:** [May 1, 2022, 1:12pm UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326 "2022-05-01T13:12:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)\
**Post date:** [May 1, 2022, 1:12pm UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/1 "2022-05-01T13:12:21Z")

</div>

I have noticed on my system that whenever I define CuArrays, I get Float64 as default. Is there a way to change the default to Float32, i.e. without specifying the type every time I define CuArrays? I want to get Float32, say, when I am using GPU.

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 1, 2022, 1:47pm UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/2 "2022-05-01T13:47:52Z")

</div>

Can you give an example of what you mean by default? In Julia, floating point literals are 64-bit. Is that causing the “default” behavior you observe or something else?

---

<div class="post-metadata">

**Author:** ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)\
**Post date:** [May 1, 2022, 2:43pm UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/3 "2022-05-01T14:43:56Z")

</div>

For instance, if I just define

A = CuArray([Inf for i = 1:20])  
typeof(A) gives  
CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}

unless I do  
A=CuArray{Float32}([Inf for i = 1:20])  
typeof(A) gives  
CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}

I am looking for a way to do  
A = CuArray([Inf for i = 1:20])  
so that  
typeof(A) gives  
CuArray{Float32, 1, CUDA.Mem.DeviceBuffer}

Thank you for the response. Sorry, I am new here and I am not sure how I can convert to code formats.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 1, 2022, 2:48pm UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/4 "2022-05-01T14:48:46Z")

</div>

If you want `Float32` literals you can enter them in scientific notation with `f` instead of `e`. For the special case of `Inf` and `NaN` there are 32-bit versions with a `32` suffix.

```julia
julia> typeof(3.2f0)
Float32

julia> typeof(Inf32)
Float32

julia> typeof(NaN32)
Float32

```

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [May 1, 2022, 2:58pm UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/5 "2022-05-01T14:58:14Z")

</div>

@GunnarFarneback already answered your question, but to be more explicit, the chain of events you need to understand is the following:

1. `Inf` is a Float64 value.
2. `[Inf for i = 1:20]` creates an `Array{Float64, 1}` since each item in the array is a `Float64` value.
3. `CuArray([Inf for i = 1:20])` creates a `CuArray{Float64, 1, CUDA.Mem.DeviceBuffer}` since it’s taking in an `Array{Float64, 1}`.

> Sorry, I am new here

You don’t ever need to apologize for being new.

---

<div class="post-metadata">

**Author:** ![mcabbott](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcabbott/32/6603_2.png) [@mcabbott](https://discourse.julialang.org/u/mcabbott)\
**Post date:** [May 1, 2022, 3:01pm UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/6 "2022-05-01T15:01:32Z")

</div>

You might be looking for `cu`, which copies a `Vector{Float64}` to a `CuArray{Float32, 1, ...}`, changing the element type on the assumption that this was just the default, and the GPU default should be Float32 (as Float64 is usually much much slower there).

---

<div class="post-metadata">

**Author:** ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)\
**Post date:** [May 2, 2022, 7:11am UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/7 "2022-05-02T07:11:38Z")

</div>

I am not aware of the _cu_ , I would be grateful if you have the link to the same or some minimal working example. Thank you.

---

<div class="post-metadata">

**Author:** ![Phuntsho](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phuntsho/32/46546_2.png) [@Phuntsho](https://discourse.julialang.org/u/Phuntsho)\
**Post date:** [May 2, 2022, 7:25am UTC](https://discourse.julialang.org/t/converting-default-float-type-to-float32/80326/8 "2022-05-02T07:25:52Z")

</div>

Thank you everyone for the great suggestions. Just for the reference if anyone should have a similar question in the future I just found out that a similar question was asked on [here on the stackoverflow](https://stackoverflow.com/questions/68068823/how-to-change-default-float-to-float32-in-a-local-julia-environment) with satisfactory answers.
