# struct Complex

## Overview

A complex number is a number represented in the form a + bi. In this form, a and b are real numbers, and i is an imaginary number such as i² = -1. The a is the real part of the number, and the b is the imaginary part of  the number.

``````Complex.new(1, 0)   # => 1 + 0i
Complex.new(5, -12) #  => 5 - 12i``````

complex.cr

## Instance Method Summary

, , ,

### Instance methods inherited from class `Object`

, , , , , , , , , , , , , , , , , ,

### Class methods inherited from class `Object`

, , , , , , , , ,

## Class Method Detail

def self.zero : Complex #

Returns the number 0 in complex form

def self.new(real : Number, imag : Number) #

## Instance Method Detail

def *(other : Complex) #

Multiplies self by other

def *(other : Number) #

Multiplies self by other

def +(other : Number) #

Adds the value of self to other

def +(other : Complex) #

Adds the value of self to other

def - #

Returns the opposite of self

def -(other : Number) #

Removes the value from other to self

def -(other : Complex) #

Removes the value from other to self

def /(other : Number) #

ditto

def /(other : Complex) #

Divides self by other

def ==(other) #

Determines whether self equals other or not

def ==(other : Number) #

Determines whether self equals other or not

def ==(other : Complex) #

Determines whether self equals other or not

def abs #

Returns the absolute value of this complex number in a number form, using the Pythagorean theorem.

``````Complex.new(42, 2).abs  # => 42.0476
Complex.new(-42, 2).abs # => 42.0476``````

def abs2 #

Returns the square of absolute value in a number form.

``Complex.new(42, 2).abs2 # => 1768``

def conj #

Returns the conjugate of self

``````Complex.new(42, 2).conj  # => 42 - 2i
Complex.new(42, -2).conj # => 42 + 2i``````

def exp #

Calculates the exp of self

``Complex.new(4, 2).exp #  => -22.7208 + 49.646i``

def imag : Float64 #

Returns the image part of self

def inspect(io : IO) #

Write this complex object to an io, surrounded by parentheses.

``Complex.new(42, 2).inspect # => (42 + 2i)``

def inv #

Returns the inverse of self

def log #

Calculates the log of self

def log10 #

Calculates the log10 of self

def log2 #

Calculates the log2 of self

def phase #

Returns the phase of self

def polar #

Returns a tuple with the abs value and the phase.

``Complex.new(42, 2).polar # => {2.54311, 0.665774}``

def real : Float64 #

Returns the real part of self

def sign #

def sqrt #

Complex.sqrt was inspired by the following blog post of Pavel Panchekha on floating point precision: https://pavpanchekha.com/casio/index.html

def to_s(io : IO) #

Write this complex object to an io

``Complex.new(42, 2).to_s # => 42 + 2i``