Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Ruby `**` operator have higher precedence than unary `-`?

This leads to the situation like:

-1 ** 0.5 #=> -1

Only parenthesis remedies it:

(-1) ** 0.5 #=> 6.123031769111886e-17+1.0i

which is less favorable then expected 1.i, but basically acceptable. Before I go to Ruby bugs to complain, I would like to know whether there is perhaps some reason for this to be so?

like image 482
Boris Stitnicky Avatar asked Dec 26 '22 14:12

Boris Stitnicky


1 Answers

Many languages define their operator precedence tables by modeling after mathematics' order of operations. In math, exponentiation does have higher precedence than multiplication, and unary negation is a multiplication, after all.

From matz in a reply to "the sign of a number is omitted when squaring it":

People with mathematical background demands precedence for ** being higher than that of unary minus. That's the reason.

like image 193
Platinum Azure Avatar answered Jan 14 '23 00:01

Platinum Azure