# "Best practice" for constant expressions (1/2 vs. 1.0/2.0)?

**URL:** https://discourse.julialang.org/t/best-practice-for-constant-expressions-1-2-vs-1-0-2-0/17522
**Category:** New to Julia
**Created:** [November 14, 2018, 4:25pm UTC](https://discourse.julialang.org/t/best-practice-for-constant-expressions-1-2-vs-1-0-2-0/17522 "2018-11-14T16:25:25Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![adamslc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adamslc/32/3452_2.png) [@adamslc](https://discourse.julialang.org/u/adamslc)
#### Post date: [November 14, 2018, 4:39pm UTC](https://discourse.julialang.org/t/best-practice-for-constant-expressions-1-2-vs-1-0-2-0/17522/2 "2018-11-14T16:39:40Z")

</div>

Pretty easy to do some tests and find out:

```julia-repl
julia> f1(x) = 1/2 * sin(2/3 * x)
f1 (generic function with 1 method)

julia> f2(x) = 1.0/2.0 * sin(2.0/3.0 * x)
f2 (generic function with 1 method)

julia> f3(x) = 1//2 * sin(2//3 * x)
f3 (generic function with 1 method)

julia> using BenchmarkTools

julia> @btime f1(0.5);
  1.989 ns (0 allocations: 0 bytes)

julia> @btime f2(0.5);
  2.288 ns (0 allocations: 0 bytes)

julia> @btime f3(0.5);
  57.565 ns (0 allocations: 0 bytes)

julia> x = rand(10000);

julia> @btime f1.($x);
  89.042 μs (2 allocations: 78.20 KiB)

julia> @btime f2.($x);
  88.785 μs (2 allocations: 78.20 KiB)

julia> @btime f3.($x);
  507.082 μs (2 allocations: 78.20 KiB)

```

So it looks like the only hard-and-fast rule is to not use rational numbers. That makes sense because there aren’t hardware instructions for multiplying rationals.

---

_[View the full topic](https://discourse.julialang.org/t/best-practice-for-constant-expressions-1-2-vs-1-0-2-0/17522)._
