Skip to content
This repository has been archived by the owner on Sep 20, 2021. It is now read-only.

Add ^ operator for power #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

osaris
Copy link
Member

@osaris osaris commented Jul 12, 2015

Reference #25

quaternary() ( ::div:: #division expression() )?

quaternary:
term() ( ::pow:: #power expression() )?
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member

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?

Copy link
Member

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 ?

Copy link
Member Author

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.

Copy link
Member

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

Copy link
Member

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.

@Hywan Hywan self-assigned this Jul 13, 2015
@Hywan
Copy link
Member

Hywan commented Jul 13, 2015

Are tests green?

@Hywan
Copy link
Member

Hywan commented Jul 13, 2015

Thanks for the PR!


$children[1]->accept($this, $acc, $eldnah);

break;
Copy link
Member

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?

Copy link
Member Author

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 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Are tests green?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Funny: 147a38d.

@Hywan
Copy link
Member

Hywan commented Jul 22, 2015

We have to talk about testing :-). We use ^ while PHP uses ** and we don't use the same associativity. So the problem is the following: To test Hoa\Math\Arithmetic, we generate expressions and then evaluate them against PHP and Hoa\Math\Visitor\Arithmetic (our implementation). If the result is the same, then we consider our implementation correct. However, PHP uses **, not ^, so its more difficult to test. And yes, the associativity is not the same too. And ** has been introduced in PHP5.6, while 5.4 is the lowest required PHP version for Hoa so we cannot test this feature on all platforms.

Any proposal (I have several ones, but I would like your opinion first 😉)?

@Savageman
Copy link
Member

Maybe we can have both ^ with left-associativity and ** with right associativity?

@Hywan
Copy link
Member

Hywan commented Sep 29, 2015

@Savageman Ole. Nop. Stop. Nada. Too much dangerous :-p. We have the pow function, which is unambiguous. I don't really understand the need for ^ or ** to be frank :-/.

@Hywan
Copy link
Member

Hywan commented Sep 29, 2015

Let me re-phrase: I don't really understand the need for ^ or ** but I am ok to implement on of them. Not both however.

@Savageman
Copy link
Member

Then I think we should only implement ^ with left-associativity. When writing math on a paper sheet (you know, with a pen), everyone knows what ^ is and how to calculate it.

@Hywan
Copy link
Member

Hywan commented Sep 30, 2015

@Savageman Agree. @osaris Thought? Is this PR left-associative?

@Metalaka
Copy link
Member

As said here, PR is right associative.

@Savageman, I was thinking like you before read this:

… addition, subtraction, multiplication, and division operators are usually left-associative while an exponentiation operator is right-associative.
https://en.wikipedia.org/wiki/Operator_associativity#Examples

@Hywan
Copy link
Member

Hywan commented Nov 9, 2015

ping?

@Savageman
Copy link
Member

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.

@Hywan
Copy link
Member

Hywan commented Nov 9, 2015

Ok. @osaris?

@osaris
Copy link
Member Author

osaris commented Nov 24, 2015

My PR is right-associative so what should I do now ?

@Hywan
Copy link
Member

Hywan commented Nov 25, 2015

@osaris Write test cases.

@Hywan
Copy link
Member

Hywan commented Nov 25, 2015

@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 ^ and PHP use **. So we are going to generate expressions with ^ while PHP expects **. Hopefully for us, both operators are right-associatives. So I reckon we could simply replace ^ by ** and it might work. Give it a try.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging this pull request may close these issues.

4 participants