# Ruby math parenthesis wierdness

**URL:** <https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609>\
**Category:** Ruby\
**Created:** [May 25, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609 "2008-05-25T20:51:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [May 25, 2008, 8:51pm UTC](https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609/1 "2008-05-25T20:51:05Z")

</div>

All,

The code below:

m = 5\*(  
(1.0)  
- (1.0/2.0)  
)  
puts m  
m = 5\*(  
(1.0) - (1.0/2.0)  
)  
puts m

prints out:

bash-3.2$ ./run.rb  
-2.5  
2.5

The second result is what I expect, but I don’t understand the first  
result.

I usually put operands at the end of the line instead of the  
beginning, so I never ran into this before, but this was originally  
Python code that I converted into ruby.

Any insights?

Thanks,  
Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [May 25, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609/2 "2008-05-25T21:41:05Z")

</div>

I’m getting this type of issue too. It looks like any number you put  
after the first paren ‘(’ is ignored.

All these examples produce -20

m = 10\*(123  
-2)  
puts m

* * *

m = 10\*(10000000  
-2)  
puts m

* * *

m = 10\*(-10000000  
-2)  
puts m

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [May 25, 2008, 9:43pm UTC](https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609/3 "2008-05-25T21:43:25Z")

</div>

On May 25, 2008, at 20:49, Tom Verbeure wrote:

> All,
> 
> The code below:
> 
> m = 5\*(  
> (1.0)

This is the cause of the unexpected behavior. From what I can see,  
Ruby is interpreting the newline after (1.0) as a statement separator.  
Compare:

puts 5\*(  
(1.0);

- (1.0/2.0)  
)

What’s actually happening is that the third line (- (1.0/2.0)) is seen  
as a separate statement, not as a continuation on the second line. The  
minus sign in the beginning of the line is interpreted as the unary  
negation operator, not the binary (as in relating to two numbers, not  
one) subtraction operator.

This works, however:

puts 5\*(  
(1.0) -  
(1.0/2.0)  
)

A quick irb session also illustrates my point:

> > 1.0  
> > =\> 1.0
> 
> > -1.0/2.0  
> > =\> -0.5
> 
> > 1.0 -  
> > ?\> 1.0/2.0

The fact that the last prompt is “?\>”, not “\>\>” is the key point here.

Hope this helps  
Mikael Høilund

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [May 25, 2008, 11:52pm UTC](https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609/4 "2008-05-25T23:52:25Z")

</div>

If you make the line continuations explicit by adding backslashes, Ruby  
gives the correct answer:

irb(main):005:0\> m = 5\*(   
irb(main):006:1\* (1.0)   
irb(main):007:1\* - (1.0/2.0)   
irb(main):008:1\* )  
=\> 2.5

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [May 25, 2008, 9:51pm UTC](https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609/5 "2008-05-25T21:51:04Z")

</div>

> Hope this helps

Yes, definitely.

Thanks All!  
Tom

---

<div class="post-metadata">

**Author:** ![Tom\_Verbeure](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/848f3c/32.png) [@Tom\_Verbeure](https://www.ruby-forum.com/u/Tom_Verbeure)\
**Post date:** [May 26, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/ruby-math-parenthesis-wierdness/138609/6 "2008-05-26T14:48:18Z")

</div>

I see. Thanks Phlip!
