Using Complex Numbers : Complex Numbers « Number « Ruby






Using Complex Numbers


require 'complex'

Complex::I                                # => Complex(0, 1)

a = Complex(1, 4)                         # => Complex(1, 4)
a.real                                    # => 1
a.image                                   # => 4

b = Complex(1.5, 4.25)                    # => Complex(1.5, 4.25)
b + 1.5                                   # => Complex(3.0, 4.25)
b + 1.5*Complex::I                        # => Complex(1.5, 5.75)

a - b                                     # => Complex(-0.5, -0.25)
a * b                                     # => Complex(-15.5, 10.25)
b.conjugate                               # => Complex(1.5, -4.25)
Math::sin(b)
# => Complex(34.9720129257216, 2.47902583958724)

 








Related examples in the same category