What the hack

Hi,

could someone please explain me what the hack is happening here?

$ irb
irb(main):001:0> 2.718281828459045
=> 2.718281828459045
irb(main):002:0> Math.tan(2.718281828459045)
=> -0.4505495340698077
irb(main):003:0> -0.4505495340698077 ** 2.718281828459045
=> -0.11449187080996949
irb(main):004:0> Math.tan(2.718281828459045) ** 2.718281828459045
=> (-0.07250262422001937+0.08861014593587692i)
*irb(main):005:0> *

How comes that Complex value here? (Well, require “complex” isn’t
mentioned
anywhere and it’s not an accident. This is a virgin irb session, the
Complex
lib isn’t loaded.)

When I type _.class as the next line, I get Complex. But Complex isn’t
loaded
at all. If I type in require ‘complex’, I get the answer: true. That
means, the
Complex class was just loaded, right after the things happening above.

u.

$ irb -w
irb(main):001:0> require “complex”
lib/complex.rb is deprecated
=> true

“complex” and “rational” are already in core in newer versions

Hi,

2013/8/27 Nokan E. [email protected]:

=> -0.11449187080996949
irb(main):004:0> Math.tan(2.718281828459045) ** 2.718281828459045
=> (-0.07250262422001937+0.08861014593587692i)
irb(main):005:0>

It is due to the Ruby operator precedence.
http://www.techotopia.com/index.php/Ruby_Operator_Precedence#Operator_Precedence_Table

irb(main):001:0> -0.4505495340698077 ** 2.718281828459045
=> -0.11449187080996949
irb(main):002:0> (-0.4505495340698077) ** 2.718281828459045
=> (-0.07250262422001937+0.08861014593587692i)

u.

Regards,
Park H.

-0.4505495340698077 ** y is parsed as -(0.4505495340698077 ** y)

Ad the statement before the string

Please forgive the brevity/spelling errors.
This message is being sent from my iPhone
To contact me by phone please
Call
Office.: 1-212-702-4027
Mobil 1-917-653-7887
Fax (Secure Fax, no cover letter needed) 1-917-265-4744
This email may contains private and confidential information.
Unauthorized use of private and confidential information is prohibited.

Thanks, that makes sense. However, that is just one half of what I
don’t
quite get:

irb(main):001:0> x, y = -0.4505495340698077, 2.718281828459045
=> [-0.4505495340698077, 2.718281828459045]

irb(main):002:0> x ** y
=> (-0.07250262422001937+0.08861014593587692i)

irb(main):003:0> -0.4505495340698077 ** y
=> -0.11449187080996949

irb(main):004:0> -0.4505495340698077 == x
=> true

How is it that the same operation gives different answer if I use a
variable containing the value-0.4505495340698077 and if I use the
number -0.4505495340698077 directly?

u.

На 27.8.2013 г. 16:43 ч., Nokan E. написа:

irb(main):003:0> *-0.4505495340698077* ** y

u.

Look at the difference:
irb(main):010:0> -0.07250262422001937 ** y
=> -0.0007982156578935224
irb(main):011:0> (-0.07250262422001937) ** y
=> (-0.0005054745763290495+0.0006177731696920454i)
irb(main):012:0>

Ivan

"complex" and "rational" are already in core in newer versions

--
Posted via http://www.ruby-forum.com/.

Regards,

Ivan Cenov
OKTO-7 Co. Bulgaria
[email protected], [email protected]
mobile:+359888761080,
phone:+35972366161, fax:+35972366262
Let’s make better business with http://www.redmine.org