# A puzzling result with a simple calculation

**URL:** <https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118>\
**Category:** General Usage\
**Created:** [September 6, 2024, 11:49am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118 "2024-09-06T11:49:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![grahamstark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamstark/32/5200_2.png) [@grahamstark](https://discourse.julialang.org/u/grahamstark)\
**Post date:** [September 6, 2024, 11:49am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/1 "2024-09-06T11:49:22Z")

</div>

Hi,

is this an anomaly?

```julia
julia> 6/2(2+1)
1.0

```

against, with an explicit times operator:

```julia
julia> 6/2*(2+1)
9.0

```

(this was prompted by a video on [Dave Cutler’s channel](https://youtu.be/IaD3kGSxaVs?si=gqdj05QqSEACz96n)).

Using Julia 1.10.3.

Graham

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 6, 2024, 12:01pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/2 "2024-09-06T12:01:31Z")

</div>

> [@grahamstark](#):
>
> ```julia
> julia> 6/2(2+1)
> 1.0
> 
> ```

I would call this a bug.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 6, 2024, 12:06pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/3 "2024-09-06T12:06:58Z")

</div>

[From the manual](https://docs.julialang.org/en/v1/manual/mathematical-operations/#Operator-Precedence-and-Associativity):

> [Numeric literal coefficients](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#man-numeric-literal-coefficients), e.g. `2x`, are treated as multiplications with higher precedence than any other binary operation, with the exception of `^` where they have higher precedence only as the exponent.

See also the section on numeric literal coefficients:

[https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#man-numeric-literal-coefficients](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#man-numeric-literal-coefficients)

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [September 6, 2024, 12:08pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/4 "2024-09-06T12:08:56Z")

</div>

" [Numeric literal coefficients](https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#man-numeric-literal-coefficients), e.g. `2x`, are treated as multiplications with higher precedence than any other binary operation, with the exception of `^` where they have higher precedence only as the exponent."

[https://docs.julialang.org/en/v1/manual/mathematical-operations/](https://docs.julialang.org/en/v1/manual/mathematical-operations/)

2(2+1) is treated as 2(3)

---

<div class="post-metadata">

**Author:** ![grahamstark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/grahamstark/32/5200_2.png) [@grahamstark](https://discourse.julialang.org/u/grahamstark)\
**Post date:** [September 6, 2024, 12:23pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/5 "2024-09-06T12:23:07Z")

</div>

I get it that it’s not an implementation error, but is this not a bit weird and counter-intuitive? What’s the thinking here?

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [September 6, 2024, 12:30pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/6 "2024-09-06T12:30:59Z")

</div>

> [@grahamstark](#):
>
> What’s the thinking here?

I’m not a huge fan of juxtaposition myself, but it enables things like

```julia-repl
julia> 1 / 2pi
0.15915494309189535

```

which some people find neat.

---

<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:** [September 6, 2024, 12:32pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/7 "2024-09-06T12:32:08Z")

</div>

> [@grahamstark](#):
>
> but is this not a bit weird and counter-intuitive?

It makes sense for expressions like

```julia
1/2x

```

where it matches the intuition, cf.

1/2x

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 6, 2024, 1:16pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/8 "2024-09-06T13:16:12Z")

</div>

Yes, we are used to that sort of notation. But I have already faced confusion by mathematicians when showed them the Boltzmann equation e^{-E/kT}, and they readily interpreted that as e^{-(E/k)\times T}. It might seem intuitive, but formally I think it is wrong. I would prefer not to have that ambiguity.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [September 6, 2024, 1:46pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/9 "2024-09-06T13:46:12Z")

</div>

It’s a choice for which there’s no clear or definitive answer — perfect fodder for limitless internet discourse and gathering comments for “engagement” metrics.

The previous time this made the rounds, it was `8/2(2+2)`: [Poorly constructed math equation prompts Twitter debate – can you solve it? – New York Daily News](https://www.nydailynews.com/2019/08/01/poorly-constructed-math-equation-prompts-twitter-debate-can-you-solve-it/) (or this [wayback archive to avoid the paywall](https://web.archive.org/web/20240906134013/https://www.nydailynews.com/2019/08/01/poorly-constructed-math-equation-prompts-twitter-debate-can-you-solve-it/)).

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 6, 2024, 1:54pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/10 "2024-09-06T13:54:29Z")

</div>

I do like the juxtaposition notation, but for a programming language it might have been better to avoid the ambiguity since it can lead to bugs.

(I mean, it’s not ambiguous to the parser, it’s ambiguous to human readers and programmers [unless they study the precedence rules].)

---

<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:** [September 6, 2024, 2:59pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/11 "2024-09-06T14:59:06Z")

</div>

It _really_ helps legibility in complicated expressions, and I don’t understand how there’s any ambiguity.

There’s a world of difference between reading lots of `2x` and lots of `(2 * x)`, which is the alternative.

This is a matter for coding standards or style guides. Don’t take this away.

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 6, 2024, 3:05pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/12 "2024-09-06T15:05:16Z")

</div>

> [@DNF](#):
>
> Don’t take this away.

It’s obviously not going away since that would be a breaking change.

> [@DNF](#):
>
> I don’t understand how there’s any ambiguity

There’s ambiguity because folks are used to the usual precedence rules for arithmetic operators in other programming languages. At the end of the day, Julia is not math, it’s a programming language.

But anyways, like I said, I like juxtaposition, but we have to acknowledge the downside.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [September 6, 2024, 6:17pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/13 "2024-09-06T18:17:20Z")

</div>

> [@CameronBieganek](#):
>
> (I mean, it’s not ambiguous to the parser, it’s ambiguous to human readers and programmers [unless they study the precedence rules].)

Any infix notation requires (implicit) knowledge of precedence rules and associativity to resolve ambiguities when parsing. The juxtaposition notation is not better or worse, but just less conventional.  
(Lisp syntax is unambiguous, but less popular as a notation)

---

<div class="post-metadata">

**Author:** ![DoktorMike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/doktormike/32/2736_2.png) [@DoktorMike](https://discourse.julialang.org/u/DoktorMike)\
**Post date:** [September 6, 2024, 6:40pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/14 "2024-09-06T18:40:53Z")

</div>

As a theoretical physicist I have to say that I like this way of writing and reading math in code. Parentheses really obfuscate the math in imho. But indeed this splits the waters and I doubt there is a correct approach. Luckily in julia we get to decide as it’s by no means enforced. 😊

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [September 6, 2024, 6:41pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/15 "2024-09-06T18:41:13Z")

</div>

> [@bertschi](#):
>
> just less conventional.

This is exactly what I meant. Because it’s less conventional, programmers are more likely to make mistakes that lead to bugs.

---

<div class="post-metadata">

**Author:** ![bertschi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bertschi/32/33462_2.png) [@bertschi](https://discourse.julialang.org/u/bertschi)\
**Post date:** [September 6, 2024, 9:05pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/16 "2024-09-06T21:05:18Z")

</div>

Sure, but there is always a choice/convention involved: While `1:end-1` is nice, I regularly forget that `1 .+ 1:10` does not work as intended … in the end, I decided to use `1:(end-1)` and `1 .+ (1:10)` just to be sure.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [September 6, 2024, 9:58pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/17 "2024-09-06T21:58:15Z")

</div>

> [@DNF](#):
>
> `2x` and lots of `(2 * x)`,

True, but at the price of confusing examples like the one in the OP

---

<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:** [September 6, 2024, 10:24pm UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/18 "2024-09-06T22:24:00Z")

</div>

This: `2(2+1)` is actually function call syntax, but applied to a literal number. It wouldn’t be inconsistent, imo, to prohibit this, but allow `2x`. After all, `22` isn’t parsed as `2*2`.

But anyway, I don’t see a large risk of misunderstanding. If you see `1/2(2+1)`, you either already know what it means, or you are completely baffled, and need to look it up.

---

<div class="post-metadata">

**Author:** ![sgaure](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sgaure/32/14779_2.png) [@sgaure](https://discourse.julialang.org/u/sgaure)\
**Post date:** [September 7, 2024, 7:05am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/19 "2024-09-07T07:05:17Z")

</div>

Note that under _Physical Review_ rules, when writing fractions with a slash, multiplication has higher precedence than division. That is, implicit multiplication. So this rule has been formalized to some extent, and even mathematicians will read 2x/2x as 1, not x^2.  
(p. 20, [https://cdn.journals.aps.org/files/styleguide-pr.pdf](https://cdn.journals.aps.org/files/styleguide-pr.pdf))

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 7, 2024, 7:16am UTC](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118/20 "2024-09-07T07:16:47Z")

</div>

> [@DNF](#):
>
> `2(2+1)` is actually function call syntax, but applied to a literal number.

I think for a number literal, juxtaposition is multiplication syntax not function-call syntax.

```julia
julia> Meta.@lower 2(2+1)
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─ %1 = 2 + 1
│ %2 = 2 * %1
└── return %2
))))

```

[Next page](https://discourse.julialang.org/t/a-puzzling-result-with-a-simple-calculation/119118.md?page=2)
