# We should have a function detecting if two numbers have the same sign

**URL:** <https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 6, 2021, 3:40am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629 "2021-12-06T03:40:56Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![powerzf](https://avatars.discourse-cdn.com/v4/letter/p/8e8cbc/32.png) [@powerzf](https://discourse.julialang.org/u/powerzf)\
**Post date:** [December 6, 2021, 3:40am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/1 "2021-12-06T03:40:56Z")

</div>

We should have such a function！！  
issign(x,y)

issign(-1,1) --\>false  
issign(1,-1) --\>false  
issign(1,1) --\>true  
issign(-1,-1) --\>true

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [December 6, 2021, 3:43am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/2 "2021-12-06T03:43:07Z")

</div>

Why not just write this as `sign(x) == sign(y)`?

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [December 6, 2021, 6:01am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/3 "2021-12-06T06:01:07Z")

</div>

Any adequate solution using `signbit()`? One attempt:

```julia
 !signbit(x*y)

```

Or maybe better:

```julia
!(signbit(x) ⊻ signbit(y))

```

---

<div class="post-metadata">

**Author:** ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)\
**Post date:** [December 6, 2021, 6:42am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/4 "2021-12-06T06:42:41Z")

</div>

So what should the function return if both numbers are complex?

---

<div class="post-metadata">

**Author:** ![rfourquet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rfourquet/32/3610_2.png) [@rfourquet](https://discourse.julialang.org/u/rfourquet)\
**Post date:** [December 6, 2021, 8:36am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/5 "2021-12-06T08:36:20Z")

</div>

`flipsign(x, y) >= 0` is close to a solution, except when `x` or `y` are null.

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [December 6, 2021, 9:07am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/6 "2021-12-06T09:07:53Z")

</div>

Sign of a complex number z is defined in the same way as for a real: z/abs(z). Then of course one can compare the resulting signs as one would do for a real number as well.

That said, it’s a valid concern! 😉

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 6, 2021, 9:26am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/7 "2021-12-06T09:26:36Z")

</div>

How would that work? How do you compare for example the “signs” of `z1 = 1 + im` and `z2 = 1 + 2im` ? Dividing by the modulus doesn’t help…

(Not that it matters I think: the function would simply be undefined for complex numbers).

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [December 6, 2021, 9:42am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/8 "2021-12-06T09:42:28Z")

</div>

You could think of the sign of a complex number as the direction of the (unit) vector in the Re vs Im plane.

Therefore:

```julia
z1 = 2.0 + 2.0im
z2 = 1.0 + 1.0im
z3 = -1.0 - 1.0im
z4 = -1.0 + 1.0im

sign(z1) == sign(z2) #true
sign(z1) == sign(z3) #false
sign(z1) == sign(z4) #false

```

is the expected result and Julia provides it. And yes, that means that the sign of a complex number is not just -1, (0) or 1, but in fact still a complex number.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 6, 2021, 9:58am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/9 "2021-12-06T09:58:22Z")

</div>

So conceptually there would be infinitely many different signs, and we have for example `sign(1.0 + 1.0im) != sign(1.0 + 2.0im)`? I’m not sure that would be useful. We can already do `angle(z1) == angle(z2)` to compare the directions (with the same caveat as below).

Also, the sign is part of the representation of (real) floating point numbers, and comparisons are often meaningful. For complex numbers it would be calculated with floating-point operations, and comparing the results for two numbers would be meaningless in general: they would often be unequal just because of floating-point inaccuracies. For example:

```julia
sign(z::Complex) = z/abs(z)

z1 = 1.0 + 0.1im;
z2 = 0.2im;
z3 = 1.0 + 0.3im;

julia> sign(z1+z2) == sign(z3)
false

```

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [December 6, 2021, 10:12am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/10 "2021-12-06T10:12:22Z")

</div>

Well, it’s just the mathematical definition of sign for complex numbers (as it’s normally defined and how it’s consequently defined in Julia); as such, it’s useful, because it follows the normal convention. Whether it’s what the OP needs is another question (to me at least).

The floating point precision problem does of course exist, but that’s what we have rational numbers for?

```julia
z1 = 1 + 1//10im
z2 = 2//10im
z3 = 1 + 3//10im
sign(z1+z2) == sign(z3) # true

```

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 6, 2021, 10:24am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/11 "2021-12-06T10:24:28Z")

</div>

Ah I didn’t know this was a thing (I don’t think I’ve ever seen that used) and didn’t realized `sign` was already defined for complex numbers in Julia!

---

<div class="post-metadata">

**Author:** ![Juan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juan/32/7657_2.png) [@Juan](https://discourse.julialang.org/u/Juan)\
**Post date:** [December 6, 2021, 11:28am UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/12 "2021-12-06T11:28:06Z")

</div>

What about the number zero, 0?

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [December 6, 2021, 12:53pm UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/13 "2021-12-06T12:53:10Z")

</div>

```julia
?sign

sign(x)

  Return zero if x==0 and x/|x| otherwise (i.e., ±1 for real x).

```

followed by examples. So, sign(0) = 0 and sign(0.0 + 0.0im) = 0.0 + 0.0im.

---

<div class="post-metadata">

**Author:** ![arch.d.robison](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arch.d.robison/32/17699_2.png) [@arch.d.robison](https://discourse.julialang.org/u/arch.d.robison)\
**Post date:** [December 10, 2021, 2:55pm UTC](https://discourse.julialang.org/t/we-should-have-a-function-detecting-if-two-numbers-have-the-same-sign/72629/14 "2021-12-10T14:55:19Z")

</div>

The documentation for `sign` has a small inaccuracy. It doesn’t return `zero(x)` for negative zero:

```julia
julia> sign(-0.0)
-0.0

```

The documentation probably should say `Return x if x==0...`
