DevNotes #1. Multiplicative Operations with Complex Infinity

by Pavel Holoborodko on October 19, 2015

Every day we learn something new, gain experience and insights, collect interesting facts and tips. This process is especially intensive for software development when you deal with various algorithms, libraries, cross-platfrom development, performance optimization, etc.

To collect the summaries, ideas and facts I have been keeping development notes on a paper for a long time. But old style notebooks started to piled up and I have decided to write short posts for the blog instead. Hopefully it will be interesting for someone beside myself. Today will be the first attempt.
***

Different programming languages follow different rules in complex multiplicative operations when one of the operands is infinity. This came quite a surprise (thanks to Ciprian Necula who has drawn my attention to the issue).

All main mathematical packages (MATLAB, Mathematica and Maple) agreed on the following:

>> 1/(Inf+1i)                  % 1 
ans =
     0
 
>> Inf*(0+1i)
ans =
            0 +        Inf*i    % 2

Results are reasonable, even though it might look contradictory from the arithmetic point of view (we get NaN if we use the naive multiplication formula since Inf*0=NaN).

Things start to get interesting with other languages commonly used for numerics – namely C, C++ and FORTRAN.

C,C++ (C99/C11 standards, Annex G 5.6.1):

1/(Inf+1i) = 0              % 1
Inf*(0+1i) = NaN+Inf*i      % 2

FORTRAN (ifort, /fp:strict):

1/(Inf+1i) = NaN+Nan*i     % 1 (implementation defined)
Inf*(0+1i) = NaN+Inf*i     % 2 (by standard)

For the first test FORTRAN’s behavior is not even defined by standard (see discussion on Intel forum).

This discrepancy makes difficult full compatibility with MATLAB. Basically it means that we have to overload (= introduce overhead) the standard arithmetic operators in FORTRAN/C to conform with MATLAB. Not very pleasant news for us, as toolbox uses both of them.

Be aware of this if you port algorithms from/to MATLAB.

{ 0 comments… add one now }

Leave a Comment

Previous post:

Next post: