From: sawadatsuyoshi@... Date: 2020-03-15T04:59:52+00:00 Subject: [ruby-core:97501] [Ruby master Bug#16677] Negative integer powered (**) to a float number results in a complex Issue #16677 has been updated by sawa (Tsuyoshi Sawada). The most confusing part of the current behaviour is that (it superficially looks like) the precedence relation between the three operations (i) `-`, (ii) typical method call (using a period), and (iii) `**` do not follow transitivity, but is rather in a rock-paper-scissors relation. (a) `-` has priority over a typical method call: `-2.itself # => (-2).itself`, (b) a typical method call has priority over `**`: `2.itself ** 2 # => (2.itself) ** 2`, and yet (c) `**` has priority over `-`: `-2 ** 2 # => -(2 ** 2)` After close examination, we can tell that this is only superficial, and actually not due to precedence relation. (a) is due to the fact that `-` as a part of a literal works differently from the unary method `-@`. (c) is due to the fact that `2.(itself ** 2)` does not make sense. What is problematic is that we have to do close examination whenever we get lost using them. (b) is inevitable, and (c) matches our convention in mathematics. What has room of improvement is (a). ---------------------------------------- Bug #16677: Negative integer powered (**) to a float number results in a complex https://bugs.ruby-lang.org/issues/16677#change-84649 * Author: CamilleDrapier (Camille Drapier) * Status: Closed * 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 # => -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: