Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Julia return different results for equivalent expressions? 6÷2(1+2) and 6÷2*(1+2)

Tags:

julia

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

like image 848
英語は苦手 Avatar asked Nov 25 '21 11:11

英語は苦手


People also ask

Can a function return more than one EXP in Julia?

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:

How to evaluate a macro in Julia?

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.

How to create expressions in Julia?

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.

How does the Julia language work?

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.


1 Answers

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 example 2^3x is parsed as 2^(3x), and 2x^3 is parsed as 2*(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, that 1 / 2im equals -0.5im and 6 // 2(2 + 1) equals 1 // 1.

like image 97
Kristoffer Carlsson Avatar answered Dec 31 '22 18:12

Kristoffer Carlsson