parrotcode: Complex Numbers PMC Class Contents | PMCs

# NAME

classes/complex.pmc - Complex Numbers PMC Class

# DESCRIPTION

`Complex` provides a representation of complex numbers. It handles string parsing/generating and basic mathematical operations.

## Functions

`static void complex_parse_string (Parrot_Interp interp, FLOATVAL *re, FLOATVAL *im, STRING *value)`

Parses the string in `value` to produce a complex number, represented by the real (`*re`) and imaginary (`*im`) parts. Raises an exception if it cannot understand the string. The string should be of the form `a+bi` with optional spaces around `+` and before `i`. You can also use `j` instead of `i`.

`static FLOATVAL *complex_locate_keyed_num(Parrot_Interp interp, PMC *self, STRING *key)`

Interpret the string `key`; valid keys are `real` and `imag`, representing the real and imaginary parts of the complex number.

`PMC *new_extended()`

Create a new complex PMC with passed arguments according to pdd03.

`void *invoke(void *next)`

Pythonic object constructor. SELF is a Complex Class object. Return a new `complex` object according to 2.1. Built-in Functions.

## Methods

`void init()`

Initializes the complex number with the value 0+0i.

`void init_pmc (PMC *initializer)`

Initializes the complex number with the specified values. (not implemented)

`void destroy ()`

Cleans up.

`PMC *clone ()`

Creates an identical copy of the complex number.

`INTVAL get_integer ()`

Returns the modulus of the complex number as an integer.

`FLOATVAL get_number ()`

Returns the modulus of the complex number.

`STRING *get_string ()`

Returns the complex number as a string in the form `a+bi`.

`INTVAL get_bool ()`

Returns true if the complex number is non-zero.

`INTVAL get_integer_keyed (PMC *key)`

`INTVAL get_integer_keyed_str (STRING *key)`

`FLOATVAL get_number_keyed (PMC *key)`

`FLOATVAL get_number_keyed_str (STRING *key)`

`PMC *get_pmc_keyed (PMC *key)`

`PMC *get_pmc_keyed_str (STRING *key)`

Returns the requested number (real part for `real` and imaginary for `imag`).

`FLOATVAL get_number_keyed_int(INTVAL key)`

Quick hack to emulate get_real() and get_imag():

```  key = 0 ... get real part
key = 1 ... get imag part```
`void set_number_keyed_int(INTVAL key, FLOATVAL v)`

Set real or imag dependig on key

*/

```    FLOATVAL get_number_keyed_int(INTVAL key) {
switch (key) {
case 0:
return RE(SELF);
case 1:
return IM(SELF);
default:
internal_exception(1, "Complex: key must be 0 or 1");
}
return 0.0;
}

void set_number_keyed_int(INTVAL key, FLOATVAL v) {
switch (key) {
case 0:
RE(SELF) = v;
break;
case 1:
IM(SELF) = v;
break;
default:
internal_exception(1, "Complex: key must be 0 or 1");
}
}
/*```
`void set_string_native (STRING *value)`

Parses the string `value` into a complex number; raises an exception on failure.

`void set_pmc (PMC *value)`

if `value` is a Complex PMC then the complex number is set to its value; otherwise `value`'s string representation is parsed with `set_string_native()`.

`void set_integer_native (INTVAL value)`

`void set_number_native (FLOATVAL value)`

Sets the real part of the complex number to `value` and the imaginary part to `0.0`

`void set_integer_keyed (PMC *key, INTVAL value)`

`void set_integer_keyed_str (STRING *key, INTVAL value)`

`void set_number_keyed (PMC *key, FLOATVAL value)`

`void set_number_keyed_str (STRING *key, FLOATVAL value)`

`void set_pmc_keyed (PMC *key, PMC *value)`

`void set_pmc_keyed_str (STRING *key, PMC *value)`

Sets the requested number (real part for `real` and imaginary for `imag`) to `value`.

`void add (PMC *value, PMC *dest)`

`void add_int (INTVAL value, PMC *dest)`

`void add_float (FLOATVAL value, PMC *dest)`

Adds `value` to the complex number, placing the result in `dest`.

`void subtract (PMC *value, PMC *dest)`

`void subtract_int (INTVAL value, PMC *dest)`

`void subtract_float (FLOATVAL value, PMC *dest)`

Subtracts `value` from the complex number, placing the result in `dest`.

`void multiply (PMC *value, PMC *dest)`

`void multiply_int (INTVAL value, PMC *dest)`

`void multiply_float (FLOATVAL value, PMC *dest)`

Multiplies the complex number with `value`, placing the result in `dest`.

`void divide (PMC *value, PMC *dest)`

`void divide_int (INTVAL value, PMC *dest)`

`void divide_float (FLOATVAL value, PMC *dest)`

Divide the complex number by `value`, placing the result in `dest`.

`INTVAL is_equal (PMC *value)`

Compares the complex number with `value` and returs true if they are equal.

`void absolute()`

Sets `dest` to the absolute value of SELF that is the distance from (0.0).