# Multiplication with Complex numbers and Inf give inconsistent answers - bug?

**URL:** <https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026>\
**Category:** General Usage\
**Tags:** question, complex-numbers\
**Created:** [May 20, 2018, 4:07pm UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026 "2018-05-20T16:07:46Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)\
**Post date:** [May 20, 2018, 4:07pm UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/1 "2018-05-20T16:07:46Z")

</div>

Dear All,

I expected that this

```julia
julia> 1*(im*Inf)
0.0 + Inf*im

```

would give the same answer as this

```julia
julia> 1*im*Inf
NaN + Inf*im

```

but it didn’t! Is this a bug?

My version:

```julia
julia> versioninfo()
Julia Version 0.6.3-pre.1
Commit 41143e875d* (2018-04-18 22:58 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin17.5.0)
  CPU: Intel(R) Core(TM) i7-5557U CPU @ 3.10GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, broadwell)

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 20, 2018, 4:24pm UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/2 "2018-05-20T16:24:43Z")

</div>

It is an effect of

```julia
julia> 0*Inf
NaN

```

Now you might say that complex numbers should actually work on the one-point compactification (such that `Complex(Inf) == Complex(0,Inf)==-Complex(Inf)` and `Complex(0*Inf) === Complex(NaN) === Complex(Inf)+Complex(Inf)`) but this breaks transitivity of `Inf == Complex(Inf) == Complex(-Inf) == -Inf != Inf` and would probably be quite annoying/slow to code.

---

<div class="post-metadata">

**Author:** ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)\
**Post date:** [May 20, 2018, 6:32pm UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/3 "2018-05-20T18:32:54Z")

</div>

I’m afraid I don’t understand much of what you wrote @foobar_lv2.

Does that explain the below?

```julia
julia> Complex{Bool}(im)*Float64(Inf)
0.0 + Inf*im

julia> Complex{Float64}(im)*Float64(Inf)
NaN + Inf*im

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [May 20, 2018, 8:11pm UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/4 "2018-05-20T20:11:13Z")

</div>

There is a special case for

```julia
julia> false*Inf
0.0

```

This is the case even though 0==false. Arithmetic in julia is almost never well defined modulo `isequal` or `==`. You need to compare `typeof(x)==typeof(y) && isequal(x,y)` in order to get well-definedness. Excepting weird guys like `BigInt` and `BigFloat`, you can also check `===`. User-defined notions of equality that allow objects of different types to be equal are common in many languages, e.g. php or javascript (ok, I have some beef with this core decision, but for better or worse `1==1.0` is gonna stay and we have to live with it).

Let’s see what the two versions you had boil down to:

```julia
1*(im*Inf) = 1* Complex(0.0,Inf) = Complex(1*0.0, 1*Inf) = Complex(0.0,Inf)

(1*im)*Inf = Complex(0,1)*Inf = Complex(0*Inf, 1*Inf)=Complex(NaN,Inf)

```

See also

```julia
julia> Complex(1)*(im*Inf)
NaN + Inf*im

julia> Complex(true)*(im*Inf)
0.0 + Inf*im

```

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [May 21, 2018, 6:28am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/5 "2018-05-21T06:28:28Z")

</div>

The definition of `*(::Bool, ::Number)` has the following comment attached to it:

```julia
# make `false` a "strong zero": false*NaN == 0.0

```

Since `im` is a `Complex{Bool}`, its real part is a “strong zero”, so the product `im*Inf` will have a zero in the real part. However, if `im` is converted to any other `Complex` type before the multiplication with `Inf`, then the real part will be NaN.

```julia
julia> im*Inf
0.0 + Inf*im

julia> (UInt8(1)*im)*Inf
NaN + Inf*im

julia> (im+0)*Inf
NaN + Inf*im

julia> Complex{Float64}(im)*Inf
NaN + Inf*im

```

One could re-define

```julia
Base.:*(z::Complex{Bool}, x::T) where T<:Real = Complex(T(real(z)) * x, T(imag(z)) * x)

```

which would make `im*Inf` return the same result as the other three examples…

---

<div class="post-metadata">

**Author:** ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)\
**Post date:** [May 21, 2018, 7:23am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/6 "2018-05-21T07:23:47Z")

</div>

My original gut feeling is that `im` is just the imaginary unit, by which I mean purely imaginary without even a zero real part. Just as a float has no concept of an imaginary component, or complex has no concept of quarternions etc. If it’s purely the imaginary unit there is no zero for multiplication by `Inf`, and hence there is no `NaN`.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [May 21, 2018, 7:55am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/7 "2018-05-21T07:55:59Z")

</div>

Right. But, implementing an imaginary data type has apparently not been found practically and broadly useful in general numerical computer languages.

---

<div class="post-metadata">

**Author:** ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)\
**Post date:** [May 21, 2018, 8:36am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/8 "2018-05-21T08:36:59Z")

</div>

I suppose I’m just drawing the distinction between an imaginary number and a complex number. Perhaps an improved description for the [im](https://docs.julialang.org/en/stable/stdlib/numbers/#Base.im) documentation might be:

> The imaginary unit as a complex number with zero real part.

Maybe the [c++](http://en.cppreference.com/w/c/numeric/complex/Imaginary_I) macro is why I think this. But I haven’t checked it’s behaviour associated with `Inf` and `NaN`.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [May 21, 2018, 8:50am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/9 "2018-05-21T08:50:48Z")

</div>

The imaginary unit _is_ a complex number. Don’t you think mathematics and implementation align here? Also I actually like that `im` has a strong real zero, making it behave closer to a mathematical constant than to a measured number.

---

<div class="post-metadata">

**Author:** ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)\
**Post date:** [May 21, 2018, 9:25am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/10 "2018-05-21T09:25:27Z")

</div>

Yes, I agree it could be more clear. “the imaginary unit” might mean that there is a type `Imaginary`. Here is the definition

```julia
const im = Complex(false, true)

```

---

<div class="post-metadata">

**Author:** ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)\
**Post date:** [May 21, 2018, 9:29am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/11 "2018-05-21T09:29:15Z")

</div>

I’m not a trained mathematician, which might be where there is a difference in terminology.

`i` is the imaginary _unit_, which is the unit vector in the imaginary direction. Only tacitly is it an imaginary _number_. It’s the (non-existent?) difference between `0+1i` and just `i`.

---

<div class="post-metadata">

**Author:** ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)\
**Post date:** [May 21, 2018, 9:36am UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/12 "2018-05-21T09:36:55Z")

</div>

I’m a complete layman on these matters, but your post reminded me of the [RiemannComplexNumbers.jl](https://github.com/scheinerman/RiemannComplexNumbers.jl) package – I thought I’d mention it in case it helps.

edit: and maybe some of the comments on [#20924](https://github.com/JuliaLang/julia/issues/20924) could be related as well.

---

<div class="post-metadata">

**Author:** ![jcook](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcook/32/18211_2.png) [@jcook](https://discourse.julialang.org/u/jcook)\
**Post date:** [May 21, 2018, 9:38pm UTC](https://discourse.julialang.org/t/multiplication-with-complex-numbers-and-inf-give-inconsistent-answers-bug/11026/13 "2018-05-21T21:38:49Z")

</div>

Great! That’ll do the trick.
