# 1.0 vs Float64(1) versus const rone = 1.0

**URL:** <https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874>\
**Category:** Performance\
**Created:** [October 23, 2020, 11:03am UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874 "2020-10-23T11:03:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![grandemundo82](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grandemundo82/32/17667_2.png) [@grandemundo82](https://discourse.julialang.org/u/grandemundo82)\
**Post date:** [October 23, 2020, 11:03am UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874/1 "2020-10-23T11:03:20Z")

</div>

Hi all;

For the longest time, I wondered about something extremely basic. In my code where execution time is really important, I always wondered what is the best way to handle real number like 1.0 or 0.0.  
What is the fastest way to use them within a code

i) just use them as 1.0  
ii) or as Float64(1)  
iii) define them at the beginning of the code const rone = 1.0 and then use rone when you need them

Apologies if this is trivial but I am curious whether it makes any difference for execution time.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [October 23, 2020, 11:12am UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874/2 "2020-10-23T11:12:14Z")

</div>

I really can’t see a situation, where these wouldn’t produce the same code, so I really wouldn’t worry about this. If you want to be absolutely sure, you can check if `@code_llvm` of your function contains the literal `1.000000e+00`.

---

<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:** [October 23, 2020, 11:39am UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874/3 "2020-10-23T11:39:15Z")

</div>

`1.0` is a literal. It’s type is `Float64`, which is exactly the same as `Float64(1)`. Defining it as a `const` would make sense if it were something that you would at some point consider changing, or if it were something that was tedious to write out (like π) or remember, or you want the meaning of the variable to be explicit in the code.

Just writing `1.0` is what I would do. There is no performance cost to this.

There’s an exception, though. Sometimes it’s better to just write `1`. For example, if your code says `x + 1.0` then if `x` is an integer, the result becomes a float. That is something to consider. Also, `x^1` is faster than `x^1.0` (this is more relevant for numbers greater than 1, though.) I prefer to write `x + 1` or `1 * x` or `x < 1` or `x^1` etc. etc. in many situations, because integers are more versatile and sometimes faster than floats.

---

<div class="post-metadata">

**Author:** ![grandemundo82](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grandemundo82/32/17667_2.png) [@grandemundo82](https://discourse.julialang.org/u/grandemundo82)\
**Post date:** [October 23, 2020, 11:46am UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874/4 "2020-10-23T11:46:05Z")

</div>

Thank you both! You just freed some space in my brain to focus on other things 🙂

---

<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:** [October 23, 2020, 11:50am UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874/5 "2020-10-23T11:50:19Z")

</div>

> [@grandemundo82](#):
>
> handle real number like 1.0 or 0.0

Some context on what “handle” means would be useful. Eg to [avoid overly specific types](https://docs.julialang.org/en/v1/manual/style-guide/#Avoid-writing-overly-specific-types), I would try to go with `1` or `oneunit`, depending on the application. I rarely ever write out either `0.0` or `1.0` in actual code.

---

<div class="post-metadata">

**Author:** ![grandemundo82](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grandemundo82/32/17667_2.png) [@grandemundo82](https://discourse.julialang.org/u/grandemundo82)\
**Post date:** [October 23, 2020, 11:58am UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874/6 "2020-10-23T11:58:12Z")

</div>

I do a lot of numerical calculations in my code and 1.0 and 0.0 are on the right hand side of my equations very frequently. I hope this clarifies what I meant.

---

<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:** [October 23, 2020, 12:07pm UTC](https://discourse.julialang.org/t/1-0-vs-float64-1-versus-const-rone-1-0/48874/7 "2020-10-23T12:07:10Z")

</div>

It would be better to have some example code, but in 99% of cases I would just use `1` and `0` and let Julia promote. For the cases you want to avoid (and possible fixes), see [Avoid changing the type of a variable](https://docs.julialang.org/en/v1/manual/performance-tips/#Avoid-changing-the-type-of-a-variable).
