Issue #16677 has been updated by Dan0042 (Daniel DeLorme).


In math exponentation is expressed as superscript; there's no exponentation=
 "operator" per se, afaik. So `-2=B2` is `-(2=B2)` according to mathematica=
l rules, and it feels quite obvious to me. It doesn't feel quite as right w=
hen written with an operator though; `-2**2` doesn't have that same obvious=
ness, and `-2 ** 2` is downright deceptive.

But a quick search in gems shows things like `Time.at(-2**63)` where it's c=
learly intended as `-(2**63)`. I think those precedence rules are ok, espec=
ially given that most languages work the same way (see table below). But in=
 that case `-2.to_i ** 2` should obey expected rules and parse as `-(2.to_i=
 ** 2)`. Although a quick search in gems shows a few things like `-28.upto(=
28)` or `-5.hash` that would break (mostly in tests/specs).

For reference, here's some other languages' precedence rules for exponentat=
ion and unary operators: (from high to low precedence)

| language     |         | exp. |           |     | note                   =
      |
|--------------|---------|------|-----------|-----|------------------------=
------|
| Ruby         | ! ~ +   | **   | -         |     | quite unique...        =
      |
| Perl         |         | **   | ! ~ \ + - |     |                        =
      |
| Python       |         | **   | ~         | + - |                        =
      |
| Javascript   | ! ~ + - | **   |           |     | but `-2**2` is a Syntax=
Error |
| Excel, Basic | + -     | ^    |           |     |                        =
      |
| F#           | + -     | **   |           |     |                        =
      |
| Lua          |         | ^    | -         |     |                        =
      |
| R            |         | ^    | + -       |     |                        =
      |

I kinda like how Javascript does it; just force people to use parentheses! =
:-)

----------------------------------------
Bug #16677: Negative integer powered (**) to a float number results in a co=
mplex =

https://bugs.ruby-lang.org/issues/16677#change-84606

* Author: CamilleDrapier (Camille Drapier)
* Status: Open
* Priority: Normal
* Assignee: matz (Yukihiro Matsumoto)
* ruby -v: 2.5.7, 2.6.5, 2.7.0
* Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN
----------------------------------------
Not sure if this is an unexpected behavior.

This works as I expect:

```
-2 ** 2.2 # =3D> -4.59479341998814
```

But when I change the code a bit, it gives me a complex:

```ruby
-2.to_i ** 2.2 # =3D> (3.717265962412589+2.7007518095995273i)

a =3D -2; a ** 2.2 # =3D> (3.717265962412589+2.7007518095995273i)
```

This seems to happen only with negative numbers and float powers. I think i=
t might be related to how `Fixnum` is treated differently from other classe=
s by the power function.



-- =

https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=3Dunsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>