# Strange inconsistent in complex algebra

**URL:** https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851
**Category:** General Usage
**Created:** [April 1, 2020, 2:27pm UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851 "2020-04-01T14:27:47Z")
**Posts on this page:** 7
**Page:** 3

<div class="post-metadata">

### Author: ![gangchern](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gangchern/32/13298_2.png) [@gangchern](https://discourse.julialang.org/u/gangchern)
#### Post date: [April 4, 2020, 10:18am UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851/41 "2020-04-04T10:18:13Z")

</div>

I just test in the Python3. It seems there is no such inconsistency.

```julia
complex((-0.5) * 0.5**2)**(-0.2)
(1.2262404609625575-0.8909158444501956j)

((-0.5) * complex(0.5)**2)**(-0.2)
(1.2262404609625575-0.8909158444501956j)

```

The reason is in Python, the complex function seems redefined. They always use the integral zero in the imagine part. It is not changed even under the product with a float number. The power function is also redefined.

```julia
complex((-0.5) * 0.5**2)
(-0.125+0j)

((-0.5) * complex(0.5)**2)
(-0.125+0j)

```

PS. I do not use any external package.

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [April 4, 2020, 12:08pm UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851/42 "2020-04-04T12:08:09Z")

</div>

> [@gangchern](#):
>
> They always use the integral zero in the imagine part. It is not changed even under the product with a float number.

AFAICT that’s just printing.

---

<div class="post-metadata">

### Author: ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)
#### Post date: [April 4, 2020, 1:41pm UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851/43 "2020-04-04T13:41:09Z")

</div>

It’s not just printing. python does some _weird_ things here:

```julia
In [13]: (-0.0j).imag
Out[13]: -0.0

In [14]: (1.0-0.0j).imag
Out[14]: 0.0

```

so I’m not actually sure how to check what it does for branch cuts… It behaves as if they’ve imposed no signed zeros for complex numbers.

---

<div class="post-metadata">

### Author: ![antoine-levitt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/antoine-levitt/32/4008_2.png) [@antoine-levitt](https://discourse.julialang.org/u/antoine-levitt)
#### Post date: [April 4, 2020, 1:48pm UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851/44 "2020-04-04T13:48:08Z")

</div>

OK no it’s weirdness in parsing: [Issue 22548: Bogus parsing of negative zeros in complex literals - Python tracker](https://bugs.python.org/issue22548)

It behaves correctly wrt branch cuts:

```julia
In [24]: cmath.log(complex(-.2, -0.0j))
Out[24]: (-1.6094379124341003-3.141592653589793j)

In [25]: cmath.log(complex(-.2, 0.0j))
Out[25]: (-1.6094379124341003+3.141592653589793j)

```

I’m not sure I would call the behavior more sensible though:

```julia
In [31]: -complex(.25)
Out[31]: (-0.25-0j)

In [32]: (-1)*complex(.25)
Out[32]: (-0.25+0j)

```

(and therefore a different sign in the imaginary part of the log). I’m guessing this is because `(-1)*complex(.25)` promotes the -1 to a complex number, which possibly changes the sign of the zero sign. In any case: signed zeros are a mess, and it looks like julia is more consistent here.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [April 4, 2020, 2:04pm UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851/45 "2020-04-04T14:04:06Z")

</div>

> [@gangchern](#):
>
> They always use the integral zero in the imagine part.

Python 3.8:

```julia
In [25]: z = complex(1.0)                                                       

In [26]: z                                                                      
Out[26]: (1+0j)

In [27]: z.imag                                                                 
Out[27]: 0.0

```

> [@yuyichao](#):
>
> AFAICT that’s just printing.

Yup.

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [August 5, 2020, 8:25pm UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851/46 "2020-08-05T20:25:28Z")

</div>

> [@gangchern](#):
>
> I suggest Julia devloper to add a True zero in the core math packgage.

Maybe you can define a single root choice for this zero

> **[GitHub - eschnett/ZeroRing.jl: A number type that has only a single value...](https://github.com/eschnett/ZeroRing.jl)**
>
> A number type that has only a single value (zero) and needs no storage - GitHub - eschnett/ZeroRing.jl: A number type that has only a single value (zero) and needs no storage

In history it took a long time to invent a number zero.

Maybe zero should be its own thing, like `I` for matrices.

---

<div class="post-metadata">

### Author: ![schnetter](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/schnetter/32/45_2.png) [@schnetter](https://discourse.julialang.org/u/schnetter)
#### Post date: [August 5, 2020, 8:50pm UTC](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851/47 "2020-08-05T20:50:39Z")

</div>

The “zero ring” is unfortunately not integral. (I was very disappointed when I found out.) For example, there is `0 / 0 = 0`, or `0 * x = x`. Thus `ZeroElem` is a subtype of `Number`, but not a subtype of `Integer`.

One could define a type `Int0`, and integer type with 0 bits, similar to how Julia interprets `Bool` very similar to `Int`. But that would be very similar to defining a constant `trueZero` that gets promoted to the various zeros in the various Number types. I don’t think that would be useful as part of the language standard.

[Previous page](https://discourse.julialang.org/t/strange-inconsistent-in-complex-algebra/36851.md?page=2)
