The Crystal Programming Language Forum

Exponentiation operator has unexpected behaviour

Hi
I was exploring BigInt support and discovered a possible inconsistency in the exponentiation operator.
Here is a code snippet to see what i mean.

puts 2**2**2**2
expected output 65536 actual output 256

I think its more intuitive to interpret this as (2**(2**(2**2))) that is also how
Ruby, Python and Javascript behave.

Thanks

found this.
https://codeplea.com/exponentiation-associativity-options

Looks like there is no agreement on the associativity of exponentiation operator. But right-associative seems to be more common.

I sent a PR to fix this. Let’s hope it goes in before 1.0

4 Likes

Thank you. One less surprise for people coming to Crystal