# Simplest Sign type

**URL:** <https://discourse.julialang.org/t/simplest-sign-type/4254>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 13, 2017, 9:25pm UTC](https://discourse.julialang.org/t/simplest-sign-type/4254 "2017-06-13T21:25:20Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 13, 2017, 9:25pm UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/1 "2017-06-13T21:25:20Z")

</div>

What would be the simplest/best way to encode `sign`, as in +1 or -1, as a `type`?

Here’s what I can think of:

1. A type with an `Int` field which I then set to either +1 or -1 when setting an instance of said type, and then multiply the value of that field with whatever I want to change the sign of.
2. A type with a `Function` field which is a function that multiplies its argument with either +1 or -1.
3. Or somehow with “Value types”?

Am I over-thinking this…?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 13, 2017, 10:29pm UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/2 "2017-06-13T22:29:02Z")

</div>

You could use a Bool.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 13, 2017, 10:29pm UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/3 "2017-06-13T22:29:20Z")

</div>

Or an enum.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 13, 2017, 11:31pm UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/4 "2017-06-13T23:31:54Z")

</div>

It may depend on what other fields you have, how important it is to keep space to a minimum.  
For example, the BigInt type encodes the sign of the number in the Int32 field that holds the size (i.e. if the size if negative, the number is negative)

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 14, 2017, 8:04am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/5 "2017-06-14T08:04:42Z")

</div>

All great ideas! OK, so there’s no one ultimate way.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 14, 2017, 8:06am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/6 "2017-06-14T08:06:22Z")

</div>

Trying to understand the differences between value types and Enum…

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 14, 2017, 8:06am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/7 "2017-06-14T08:06:53Z")

</div>

What do you mean by value types?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 14, 2017, 8:08am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/8 "2017-06-14T08:08:09Z")

</div>

this:  
[https://docs.julialang.org/en/stable/manual/types/#value-types](https://docs.julialang.org/en/stable/manual/types/#value-types)

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 14, 2017, 8:21am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/9 "2017-06-14T08:21:11Z")

</div>

I’ll just add that in the vast and rich ecosystem of types that Julia has, it seems like maybe there should be some native Sign type…? This really seems like a fringe case that might not be worth it.  
It would be similar to Bool, but function differently…  
Just a thought.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [June 14, 2017, 8:22am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/10 "2017-06-14T08:22:36Z")

</div>

What do you need to use the sign for?

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 14, 2017, 8:26am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/11 "2017-06-14T08:26:54Z")

</div>

In my specific case, I need to flip a normal vector to point to the opposite direction depending on a bunch of things.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [June 14, 2017, 10:00am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/12 "2017-06-14T10:00:52Z")

</div>

Using a Bool like @dpsanders suggests looks like the best solution, and in many respects a Bool already acts like a “sign” type. Specifically:

1. `(-1)^boolvar` is -1 / 1 according to boolean and has a specialized `^` method to make it fast.
2. `ifelse(boolvar,expr,-expr)` could be used for negating.

It’s true sometimes a specialized type would help, but it can also burden (requiring specialized methods). The `-1/1` \<==\> `true/false` \<==\> `0/1` correspondence is too strong to separate these values into different types.

I would consider having a Unit{T} type (or struct) which would represent the units of type T i.e. Sign for Reals, and unit circle for complex and 1/i/-1/-i for integer complex (Gaussian numbers). This type would satisfy `abs(v) = v/unit(v)` and other such relations which come in useful sometime.

---

<div class="post-metadata">

**Author:** ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)\
**Post date:** [June 14, 2017, 11:18am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/13 "2017-06-14T11:18:01Z")

</div>

Wah! Cool stuff, didn’t know the specialized `^` nor the `ifelse` function…

Thanks!

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [June 14, 2017, 11:56am UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/14 "2017-06-14T11:56:21Z")

</div>

With the amount of new things being added, in base, and in packages, there’s always something new to learn in Julia! (I knew about `ifelse`, but also didn’t know that `^` had been specialized for `^boolval`)  
`ifelse` works very well by not causing a branch.

---

<div class="post-metadata">

**Author:** ![brainandforce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brainandforce/32/211054_2.png) [@brainandforce](https://discourse.julialang.org/u/brainandforce)\
**Post date:** [July 18, 2024, 5:48pm UTC](https://discourse.julialang.org/t/simplest-sign-type/4254/15 "2024-07-18T17:48:10Z")

</div>

I actually think there is a genuine case for a `Sign` type that is separate from `Bool`. The reason for this is that in a package I’m working on, I use Boolean values for two purposes: as strong zeros for multiplication operations, and as signs for indicating negation of a result. Keeping track of the semantics of a Boolean value using types is a good idea from the perspective of code readability: rather than dealing with different Boolean operators to manually implement multiplication, multiplications are always `*`, defined for different combinations of arguments.

Performance penalties are a good point. For my use case, type stability guarantees and heavy inlining prevent dynamic dispatch from being an issue, but in general I hope that the language implementation has made this less of an issue since this thread was last active.

I implemented a package, [SignType.jl](https://github.com/brainandforce/SignType.jl) (not registered), which provides `Sign <: Signed` as an abstraction of a sign bit, for those who may be interested.
