I typed the following in Julia's REPL:
julia> 6÷2(1+2)
1
julia> 6÷2*(1+2)
9
Why are the different results output?
Presh Talwalkar says 9
is correct in the movie
6÷2(1+2) = ? Mathematician Explains The Correct Answer - YouTube
We have already seen one example of a function returning Expr objects: the parse function, which takes a string of Julia code and returns the corresponding Expr. A function can also take one or more Expr objects as arguments, and return another Expr. Here is a simple, motivating example:
Macro is one of the ways to evaluate the input expression and gives the resultant output expression. The process in the Julia language works as follows: it first parses and evaluates the macro, and the processed code produced by the macro is eventually evaluated like an ordinary expression. Macro can be referred to as a code of code.
There are various ways to create expressions in Julia, some of them are listed below: Julia provides a pre-defined function Expr () that can be used to create expressions of user’s choice. Here, my_exp = Expr (: (=), :x, 10) is the prefix notation of the syntax, the first argument is head and the remaining are arguments of the expression.
The process in the Julia language works as follows: it first parses and evaluates the macro, and the processed code produced by the macro is eventually evaluated like an ordinary expression. Macro can be referred to as a code of code.
YouTube notwithstanding, there is no correct answer. Which answer you get depends on what precedence convention you use to interpret the problem. Many of these viral "riddles" that go around periodically are contentious precisely because they are intentionally ambiguous. Not a math puzzle really, it's just a parsing problem. It's no deeper than someone saying a sentence with two interpretations. What do you do in that case in real life? You just ask which one they meant. This is no different. For this very reason, the ÷
symbol isn't often used in real mathematical notation—fraction notation is used instead, which clearly disambiguates this as either:
6
- (1 + 2) = 9
2
or as
6
--------- = 1
2 (1 + 2)
Regarding Julia specifically, this precedence behavior is documented here:
https://docs.julialang.org/en/v1/manual/integers-and-floating-point-numbers/#man-numeric-literal-coefficients
Specifically:
The precedence of numeric literal coefficients is slightly lower than that of unary operators such as negation. So
-2x
is parsed as(-2) * x
and√2x
is parsed as(√2) * x
. However, numeric literal coefficients parse similarly to unary operators when combined with exponentiation. For example2^3x
is parsed as2^(3x)
, and2x^3
is parsed as2*(x^3)
.
and the note:
The precedence of numeric literal coefficients used for implicit multiplication is higher than other binary operators such as multiplication (
*
), and division (/
,\
, and//
). This means, for example, that1 / 2im
equals-0.5im
and6 // 2(2 + 1)
equals1 // 1
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With