Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Precedence of power operator #935

Open
Ionizing opened this issue Nov 26, 2024 · 8 comments
Open

Precedence of power operator #935

Ionizing opened this issue Nov 26, 2024 · 8 comments
Labels

Comments

@Ionizing
Copy link

Hi, consider the short code

print(-2**2)

I would expect it prints -4 while it gives 4, meaning that -2**2 equals (-2) ** 2, which is contradict to the common precedence of negate and power (for example, Python and Fortran would print -4).

Is it an expected behaviour?

@schungx
Copy link
Collaborator

schungx commented Nov 26, 2024

Yeah, that is because in Rhai uniary operators (such as negation and negative) bind tighter than binary operators...

It would be confusing otherwise:

-2 ** 2 = -4;
-2 * 2 = ?    // does - bind tighter than * but no as tight as **?

Binding ** tighter is a relic of math symbols where the index is written literally on top of the base in smaller type, meaning it is not written as a binary operator at all. That makes the interpretation make sense. However, in programming there is no distinction between an index and its base value. So -2 ** 2 is essentially exp(-2, 2).

@Ionizing
Copy link
Author

Okay, would it possible to add a special hint to the document (Rhai book) to clarify this feature, in case someone else would come across this behaviour in the future ?

@benatkin
Copy link
Contributor

It's documented pretty well. I can confirm it differs from popular scripting languages, but Rust doesn't have the operator so it doesn't differ from Rust.

@benatkin
Copy link
Contributor

IMO it would be better to have an operator precedence table in the Scripting Language section as well, without the numbers and maybe without the directions. They could link to each other. Otherwise the Scripting Language section is a bit incomplete as a language reference.

@schungx
Copy link
Collaborator

schungx commented Dec 28, 2024

True. It is certainly missing a section on operators...

@Ionizing
Copy link
Author

Ionizing commented Jan 3, 2025

It's documented pretty well. I can confirm it differs from popular scripting languages, but Rust doesn't have the operator so it doesn't differ from Rust.

Well, I know that Rust doesn't have it but many other languages have the power operator, and most of them follows the precedence of it in mathematics (for example, in Julia -3^2 == -9). it is not a good idea to change the precedence without explicit descriptions on the tutorials. If you don't wish it bring this surprise to users, a marked sentence to highlight this feature is required in the documents (especially the examples in the "Getting started" section, or examples in playgrounds), instead of making users searching the language dictionary after they come across this surprise.

@benatkin
Copy link
Contributor

benatkin commented Jan 8, 2025

I think it's worth making this change, because it's a better design.

I just noticed that Rhai has modulus, which is more convenient, and differs from Rust and JavaScript, in a pleasant way. You can page through things by doing n % page_count since negative numbers will return between 0 and n, unlike remainder. It's like this in python.

With the power operator, giving ** higher precedence than - is definitely less surprising.

@schungx
Copy link
Collaborator

schungx commented Jan 11, 2025

The current difficulty is thar Rhai's code structure binds unary operators tighter than binary ones...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants