# Why can't complex integer arithmetic be done?

27 views (last 30 days)

Show older comments

##### 3 Comments

### Answers (3)

James Tursa
on 14 May 2021

Edited: James Tursa
on 14 May 2021

I think the main reason for disallowing integer array arithmetic in MATLAB is because of the ambiguity caused by the max(min( )) behavior of integer arithmetic. Unlike other languages such as C/C++ that typically do modulo arithmetic for overflow situations, MATLAB does extreme value clipping. E.g., take this operation:

uint8(200) + uint8(200)

In C/C++ this will result in mod(200+200,256) = mod(400,256) = 144

In C/C++ you can string together several such operations and get a consistent answer regardless of the order of operations.

But in MATLAB this results in max(200+200,255) = max(400,255) = 255

This results in MATLAB depending on the order of operations. E.g., take this expression:

uint8(200) + uint8(200) - uint8(200)

Group the operations in two different ways:

(uint8(200) + uint8(200)) - uint8(200) = 255 - 200 = 55

uint8(200) + (uint8(200) - uint8(200)) = 200 + 0 = 200

So, depending on the order of operations you get two different answers. This becomes problematic when the operation involves anything other than real scalars because of the operation order ambiguity noted above. Depending on assumptions made for operation order of intermediate expressions when using arrays, you can get a completely different answer. To avoid this ambiguity, MATLAB simply disallows it.

My guess is that you want to treat this operation as a floating point operation, in which case converting the operands to floating point prior to the operation as Jan suggest is the way to go. You can always convert the result back to an integer type after the operation.

##### 0 Comments

Andy Bartlett
on 7 Jun 2021

Edited: Andy Bartlett
on 7 Jun 2021

The integers provided in base MATLAB were design to support image processing use cases.

Integer types are also a special case of the fi objects provided by Fixed-Point Designer. Fi objects were designed to support general purpose calculations for controls, signal processing, image processing, etc. Complex operations are supported for fi objects.

The following code will produce an error due to the complex limitations of MATLAB built-in integers.

a = complex(7,-5)

b = complex(3,2)

c = a * b

ai = int16(a)

bi = int16(b)

ci = ai * bi

Producing this output

>> complex_fi_mult

a =

7 - 5i

b =

3 + 2i

c =

31 - 1i

ai =

int16

7 - 5i

bi =

int16

3 + 2i

Error using *

Complex integer arithmetic is not supported.

Error in complex_fi_mult (line 7)

ci = ai * bi

If the integer types are first converted to their fi object equivalents, then the complex math is supported.

a = complex(7,-5)

b = complex(3,2)

c = a * b

ai = int16(a)

bi = int16(b)

af = castIntToFi(ai)

bf = castIntToFi(bi)

cf = af * bf

as shown in this output

>> complex_fi_mult2

a =

7 - 5i

b =

3 + 2i

c =

31 - 1i

ai =

int16

7 - 5i

bi =

int16

3 + 2i

af =

7 - 5i

numerictype(1,16,0)

bf =

3 + 2i

numerictype(1,16,0)

cf =

31 - 1i

numerictype(1,33,0)

##### 0 Comments

Jan
on 14 May 2021

What about converting the complex int16 value to a single?

a = int16(complex(2,3))

b = single(4 + 5i)

c = single(a) * b

##### 0 Comments

### See Also

### Products

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!