The Crystal Programming Language Forum

Results of "a-b -c" and "a-b-c" different

Hello!

I found a-b-c and a-b -c give different result. Anyone knows if this
is following the language specification or not?

a = 20
b = 19
c=3
pp! a-b -c
pp! a-b-c

gives

a - (b - c) # => 4
(a - b) - c # => -2

Wow. First case definitely looks like a bug. Any language where a - b - c would become a - (b - c)?

The problem is that a-b -c becomes a - (b - c) while a-b-c becomes (a - b) - c. Spaces shouldn’t change the result.

BTW

a = 20
b = 19
c=3
pp! a&-b &-c
pp! a&-b&-c
(a &- b) &- c # => -2
(a &- b) &- c # => -2

It’s a bug, please report it in GitHub.

It’s hard to fix.

Meh, it’s very easy to fix.

5 Likes

Fix here: https://github.com/crystal-lang/crystal/pull/9652

3 Likes

Great! I hope this will be included soon!

The language specification for these things is “Try it in Ruby. If you get a different result than Ruby then it’s a bug”.

1 Like

Classic Ary :laughing:

I ended up closing the PR because I’m not sure it’s the best way to fix it.

You’ll have to hope someone else will fix it then.