# Difference btween dot operator and operator?

**URL:** https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304
**Category:** General Usage
**Created:** [February 15, 2021, 9:54am UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304 "2021-02-15T09:54:47Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![Ravinder\_Ram](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ravinder_ram/32/14617_2.png) [@Ravinder\_Ram](https://discourse.julialang.org/u/Ravinder_Ram)
#### Post date: [February 15, 2021, 9:54am UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/1 "2021-02-15T09:54:47Z")

</div>

what is difference between thse two exprresiion?  
5 + 3 -----\> 8  
5 .+ 3 -----\> 8  
is there any benefit to put **.** before operator?

---

<div class="post-metadata">

### Author: ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)
#### Post date: [February 15, 2021, 10:01am UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/2 "2021-02-15T10:01:23Z")

</div>

the `.` is of no use for simple numbers, it’s (fusing) [broadcasting](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized), i.e. apply the function/operator element-wise.

```julia
julia> [2,4] + 4
ERROR: MethodError: no method matching +(::Array{Int64,1}, ::Int64)
For element-wise addition, use broadcasting with dot syntax: array .+ scalar
[...]

julia> [2,4] .+ 4
2-element Array{Int64,1}:
 6
 8

```

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 15, 2021, 10:45am UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/3 "2021-02-15T10:45:01Z")

</div>

and it works 3 ways:

```julia
julia> [2, 4] .+ 4
2-element Vector{Int64}:
 6
 8

julia> 4 .+ [2, 4]
2-element Vector{Int64}:
 6
 8

julia> [2, 4] .+ [1, 2] # note these are of the same length
2-element Vector{Int64}:
 3
 6

```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 15, 2021, 2:05pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/4 "2021-02-15T14:05:55Z")

</div>

I don’t mean to hijack the post, but a related question came up in my coding yesterday. Is `int1 / int2 .* vec1` any faster than `int1 .* vec1 ./ int2`? The latter looks more like the book equation I am trying to represent, but the former seemingly requires fewer operations.

---

<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: [February 15, 2021, 2:07pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/5 "2021-02-15T14:07:29Z")

</div>

It is worth noting that `3 .+ 6` works, as does `3 .+ [1, 2]` and `[1, 2] .+ 3` just because numbers themselves are zero-dimensional containers of their own values. Other data structures will not behave the same way without workarounds. And this behavior of numbers may induce to some subtle bugs:

```julia
for x in 5
    # Will not error, just loop for the number five
    # the user wanted 1:5 but forgot the first part
end
```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 15, 2021, 2:30pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/7 "2021-02-15T14:30:22Z")

</div>

Does the compiler optimize to the first method before running or is the operation just cheap enough that it doesn’t matter?

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 15, 2021, 2:48pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/8 "2021-02-15T14:48:29Z")

</div>

I was also wondering if  
`int1 ./ int2 .* vec1`  
could in some cases be slower than  
`int1 / int2 .* vec1`  
if the compiler decided to do the operations from right to left for some reason? The standard operator could maybe force the operations to be done in the right order? Anyone know how these expressions are parsed under the hood?

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 15, 2021, 3:12pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/9 "2021-02-15T15:12:57Z")

</div>

The compiler will not decide to do these operations from right to left.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [February 15, 2021, 4:00pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/10 "2021-02-15T16:00:52Z")

</div>

> [@Nathan\_Boyer](#):
>
> Is `int1 / int2 .* vec1` any faster than `int1 .* vec1 ./ int2` ?

My read of order of operation is that the first one divides int1 by int2 then multiplies the result by all the elements in vec1. The second would be multiply all the elements in vec1 by int1 then divide divide all the elements in vec1 by int2…so the first should be faster:

```julia
julia> v = rand(Int, 100_000)

julia> @btime 5 / 3 .* $v
  112.892 μs (2 allocations: 781.33 KiB)

julia> @btime 5 .* $v ./ 3
  280.847 μs (2 allocations: 781.33 KiB)

```

Additionally the results are not the exactly the same:

```julia
julia> v = rand(Float64, 10)
10-element Array{Float64,1}:
 0.6822666767203107
 0.8621218010031695
 0.08488887993041017
 0.024156214950365573
 0.6498916306553097
 0.06862022671341128
 0.33611709696877967
 0.29794115597733617
 0.023073589261145555
 0.4549859329862016

julia> (5.0 / 3.0 .* v) .- (5.0 .* v ./ 3.0)
10-element Array{Float64,1}:
 0.0
 0.0
 0.0
 6.938893903907228e-18
 0.0
 0.0
 0.0
 0.0
 0.0
 0.0

```

---

<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: [February 15, 2021, 4:38pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/11 "2021-02-15T16:38:33Z")

</div>

I tend to use parentheses for this (to ensure performance and reduce needless headscratching):

```julia
(int1 / int2) .* vec1

```

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 15, 2021, 4:39pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/12 "2021-02-15T16:39:24Z")

</div>

Thanks for the concrete performance metrics. (I get confused about the proper way to do those.) I am surprised that the results differ.

---

<div class="post-metadata">

### Author: ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)
#### Post date: [February 15, 2021, 4:54pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/13 "2021-02-15T16:54:32Z")

</div>

Okay, that probably is the best approach. I suppose my main question was, “Is it worthwhile to rearrange an equation for performance, or does Julia fix it for you at compile time?” @pixel27’s timing results prove that rearranging does make a difference.

---

<div class="post-metadata">

### Author: ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)
#### Post date: [February 15, 2021, 5:13pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/14 "2021-02-15T17:13:27Z")

</div>

yes and @pixel27, nicely done  
If I understood you correctly, the proper thing to do was to use parentheses to make clear what you wanted to apply to what.

---

<div class="post-metadata">

### Author: ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)
#### Post date: [February 15, 2021, 6:21pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/15 "2021-02-15T18:21:51Z")

</div>

> [@Nathan\_Boyer](#):
>
> I am surprised that the results differ.

Starting off in Floats keeps the differences minor. If the conversion from Int’s to Float’s happens at different times, well you get bigger differences…

```julia
julia> v = rand(Int, 10)
10-element Array{Int64,1}:
 -2203814620830076632
 -2877705152597182017
  6734421012006017801
 -1108821341357642966
  2738129021123542017
  5056953375491690984
  -283354281385024843
  1980335854149908150
  7441289658945453492
  8017928522033622324

julia> (5 / 3 .* v) .- (5 .* v ./ 3)
10-element Array{Float64,1}:
   -6.148914691236518e18
   -6.148914691236517e18
    1.2297829382473036e19
 -256.0
    6.148914691236518e18
    6.148914691236517e18
  -64.0
    6.148914691236518e18
    1.2297829382473034e19
    1.2297829382473034e19

```

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [February 15, 2021, 6:57pm UTC](https://discourse.julialang.org/t/difference-btween-dot-operator-and-operator/55304/16 "2021-02-15T18:57:41Z")

</div>

> [@Nathan\_Boyer](#):
>
> I am surprised that the results differ.

Order of operations matters because of the limited precision of floating point. Values get truncated to fit the size. Pretend you can represent decimal values to one place after the decimal and you evaluate the expression 1/3 \* 3 and also 1 \* 3 / 3. Because \* and / are evaluated left to right, in the first, 1/3 would be evaluated first and represented as 0.3, then multiplying by 3 gives 0.9. In the second 1 \* 3 gives 3.0, and dividing that by 3 gives 1.0.
