From: XrXr@...
Date: 2020-03-07T17:05:23+00:00
Subject: [ruby-core:97407] [Ruby master Bug#16677] Negative integer powered (**) to a float number results in a complex

Issue #16677 has been updated by alanwu (Alan Wu).


> So it looks like there's something special about how negative integers are parsed?

Negative integers are atomic tokens where as the expression `-x` applies the unary `-` operator to `x`.

`-x.to_i` is parsed as `-(x.to_i)`:
```
$ ruby --dump=parsetree -e '-x.to_i'
###########################################################
## Do NOT use this node dump for any purpose other than  ##
## debug and research.  Compatibility is not guaranteed. ##
###########################################################

# @ NODE_SCOPE (line: 1, location: (1,0)-(1,7))
# +- nd_tbl: (empty)
# +- nd_args:
# |   (null node)
# +- nd_body:
#     @ NODE_OPCALL (line: 1, location: (1,0)-(1,7))*
#     +- nd_mid: :-@
#     +- nd_recv:
#     |   @ NODE_CALL (line: 1, location: (1,1)-(1,7))
#     |   +- nd_mid: :to_i
#     |   +- nd_recv:
#     |   |   @ NODE_VCALL (line: 1, location: (1,1)-(1,2))
#     |   |   +- nd_mid: :x
#     |   +- nd_args:
#     |       (null node)
#     +- nd_args:
#         (null node)
```
while `-2.to_i` is parsed as `(-2).to_i`. I guess it's the typical "things that look the same are not always the same" thing in programming languages :)
Side note, it's a bit surprising to me that [the doc for operator precedence](https://ruby-doc.org/core-2.7.0/doc/syntax/precedence_rdoc.html) does not mention the method call operator (the dot), but I suppose it's not really an operator?

----------------------------------------
Bug #16677: Negative integer powered (**) to a float number results in a complex 
https://bugs.ruby-lang.org/issues/16677#change-84539

* Author: CamilleDrapier (Camille Drapier)
* Status: Open
* Priority: Normal
* 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 # => -4.59479341998814
```

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

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

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

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



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

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