-
Notifications
You must be signed in to change notification settings - Fork 37
Add ^ operator for power #26
base: master
Are you sure you want to change the base?
Conversation
quaternary() ( ::div:: #division expression() )? | ||
|
||
quaternary: | ||
term() ( ::pow:: #power expression() )? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure that ^
has not the same priority (precedence) than an other existing operator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to many sources found on Google (!!), ^
priority is lower than multiplication and division operators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since PHP5.6, we have the **
operator with the following precedence: http://php.net/manual/en/language.operators.precedence.php. It is a right associative operator. Is it the case here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As said here,
- the common
^
operator is left associative (according to my mind and my calculator (2^3^2=64)) - the PHP
**
operator is right associative (2 ** 3 ** 2 == 512).
What about Hoa\Math
^
operator ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's right associative here (2^3^2 == 512) like the **
operator.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it seem my mind and my college calculator were wrong ;)
https://en.wikipedia.org/wiki/Order_of_operations#Special_cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, do we have the same associativity than in PHP? If yes, why not using **
? We will be able to fix test suites directly too.
Are tests green? |
Thanks for the PR! |
|
||
$children[1]->accept($this, $acc, $eldnah); | ||
|
||
break; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you explain this part please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short answer, no. Longer, I have basically copy/pasted what is done for #multiplication
and updated the operation ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Are tests green?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Funny: 147a38d.
We have to talk about testing :-). We use Any proposal (I have several ones, but I would like your opinion first 😉)? |
Maybe we can have both ^ with left-associativity and ** with right associativity? |
@Savageman Ole. Nop. Stop. Nada. Too much dangerous :-p. We have the |
Let me re-phrase: I don't really understand the need for |
Then I think we should only implement |
@Savageman Agree. @osaris Thought? Is this PR left-associative? |
As said here, PR is right associative. @Savageman, I was thinking like you before read this:
|
ping? |
After reading the comment from @Metalaka, other sources and how its done in other languages, I now think it's best to implement with right-associativity. It makes more sense in the programming world. |
Ok. @osaris? |
My PR is right-associative so what should I do now ? |
@osaris Write test cases. |
@osaris Currently, how do we test the grammar? We generated expressions based on the arithmetic grammar, and then we evaluate them twice: By PHP and by our visitor. We must obtain the same results. If we don't, either there is an error in PHP or there is an error in our visitor. The problem is: We use |
Reference #25