# divisible-by operator

**URL:** <https://forums.swift.org/t/divisible-by-operator/2078>\
**Category:** Discussion\
**Created:** [April 6, 2016, 4:13pm UTC](https://forums.swift.org/t/divisible-by-operator/2078 "2016-04-06T16:13:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Milos\_Rankovic1](https://avatars.discourse-cdn.com/v4/letter/m/7ab992/32.png) [@Milos\_Rankovic1](https://forums.swift.org/u/Milos_Rankovic1)\
**Post date:** [April 6, 2016, 4:13pm UTC](https://forums.swift.org/t/divisible-by-operator/2078/1 "2016-04-06T16:13:41Z")

</div>

Checking for divisibility is very common:

&nbsp;&nbsp;&nbsp;&nbsp;21 % 3 == 0 // true

In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:

&nbsp;&nbsp;&nbsp;&nbsp;21 %== 3 // true

… which also allows me to overload it for sequences:

&nbsp;&nbsp;&nbsp;&nbsp;21 %== [7, 3] // true

(If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)

milos

---

<div class="post-metadata">

**Author:** ![Felix\_Cloutier1](https://avatars.discourse-cdn.com/v4/letter/f/e47c2d/32.png) [@Felix\_Cloutier1](https://forums.swift.org/u/Felix_Cloutier1)\
**Post date:** [April 7, 2016, 6:07am UTC](https://forums.swift.org/t/divisible-by-operator/2078/2 "2016-04-07T06:07:50Z")

</div>

This is the kind of thing that I'd let live in a library and consider importing into the standard if a lot of people use it.

Félix

> **···**
>
> > Le 6 avr. 2016 à 09:13:41, Milos Rankovic via swift-evolution \<swift-evolution@swift.org\> a écrit :
> > 
> > Checking for divisibility is very common:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 % 3 == 0 // true
> > 
> > In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== 3 // true
> > 
> > … which also allows me to overload it for sequences:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== [7, 3] // true
> > 
> > (If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)
> > 
> > milos  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Erica\_Sadun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/erica_sadun/32/243_2.png) [@Erica\_Sadun](https://forums.swift.org/u/Erica_Sadun)\
**Post date:** [April 7, 2016, 2:13pm UTC](https://forums.swift.org/t/divisible-by-operator/2078/3 "2016-04-07T14:13:07Z")

</div>

While modulo checks are common, I don't think that your proposed solution (%==) enhances readability or saves typing \*to such extent\* that it vastly improves over the existing art:

21 % 3 == 0 reads easily from left to right, is quick to type, is understood across many languages.

21 %== 3 saves a few spaces, is less immediately understandable (due to the visual overlap with `+=` and `-=`) and would be (as far as I'm aware of) unique to Swift.

I applaud the thinking and creativity but I would not support the proposal.

-- E

> **···**
>
> > On Apr 6, 2016, at 10:13 AM, Milos Rankovic via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Checking for divisibility is very common:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 % 3 == 0 // true
> > 
> > In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== 3 // true
> > 
> > … which also allows me to overload it for sequences:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== [7, 3] // true
> > 
> > (If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)

---

<div class="post-metadata">

**Author:** ![Haravikk3](https://avatars.discourse-cdn.com/v4/letter/h/df705f/32.png) [@Haravikk3](https://forums.swift.org/u/Haravikk3)\
**Post date:** [April 7, 2016, 11:20am UTC](https://forums.swift.org/t/divisible-by-operator/2078/4 "2016-04-07T11:20:48Z")

</div>

Personally I’d prefer to use a method like:

&nbsp;&nbsp;21.isDivisibleBy(3)

As it’s clearer IMO. My problem with the operator is that it doesn’t make logical sense to have the value you’re dividing by on the right hand side when there are equals signs, it seems more like this should be the value you’re testing. Perhaps there’s a better choice of operator such as:

&nbsp;&nbsp;21 %: 3

Though that’s probably not very clear either as the colon could be easy to miss. Basically the choice of %== doesn’t suggest its purpose, which makes it one more thing to learn, which is why a method may be a better choice.

> **···**
>
> > On 6 Apr 2016, at 17:13, Milos Rankovic via swift-evolution \<swift-evolution@swift.org\> wrote:
> > 
> > Checking for divisibility is very common:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 % 3 == 0 // true
> > 
> > In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== 3 // true
> > 
> > … which also allows me to overload it for sequences:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== [7, 3] // true
> > 
> > (If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)
> > 
> > milos  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Milos\_Rankovic1](https://avatars.discourse-cdn.com/v4/letter/m/7ab992/32.png) [@Milos\_Rankovic1](https://forums.swift.org/u/Milos_Rankovic1)\
**Post date:** [April 7, 2016, 6:13pm UTC](https://forums.swift.org/t/divisible-by-operator/2078/5 "2016-04-07T18:13:41Z")

</div>

Thank you for giving this a thought!

milos

> **···**
>
> > On 7 Apr 2016, at 15:13, Erica Sadun \<erica@ericasadun.com\> wrote:
> > 
> > > On Apr 6, 2016, at 10:13 AM, Milos Rankovic via swift-evolution \<swift-evolution@swift.org \<mailto:swift-evolution@swift.org\>\> wrote:
> > > 
> > > Checking for divisibility is very common:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;21 % 3 == 0 // true
> > > 
> > > In fact, this is such a common use of the `%` operator that the `== 0` side of the expression seems distracting in this use case. For quite a while now, I’ve been using a custom operator for this, which is steadily growing on me:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;21 %== 3 // true
> > > 
> > > … which also allows me to overload it for sequences:
> > > 
> > > &nbsp;&nbsp;&nbsp;&nbsp;21 %== [7, 3] // true
> > > 
> > > (If I’m inadvertently misusing this mailing list to share such a minor idea, please tell me off so that I can learn not to do it again!)
> > 
> > While modulo checks are common, I don't think that your proposed solution (%==) enhances readability or saves typing \*to such extent\* that it vastly improves over the existing art:
> > 
> > 21 % 3 == 0 reads easily from left to right, is quick to type, is understood across many languages.
> > 
> > 21 %== 3 saves a few spaces, is less immediately understandable (due to the visual overlap with `+=` and `-=`) and would be (as far as I'm aware of) unique to Swift.
> > 
> > I applaud the thinking and creativity but I would not support the proposal.
> > 
> > -- E

---

<div class="post-metadata">

**Author:** ![jtbandes](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jtbandes/32/463_2.png) [@jtbandes](https://forums.swift.org/u/jtbandes)\
**Post date:** [April 11, 2016, 1:39am UTC](https://forums.swift.org/t/divisible-by-operator/2078/6 "2016-04-11T01:39:58Z")

</div>

I'm not suggesting this for the standard library (or anyone's production  
code), but it's fun to note that there is actually a standard mathematical  
notation for this, and it has a corresponding Unicode character U+2223  
"DIVIDES": ∣

&nbsp;&nbsp;&nbsp;&nbsp;infix operator ∣ {}  
&nbsp;&nbsp;&nbsp;&nbsp;func ∣\<I: IntegerType\>(lhs: I, rhs: I) -\> Bool {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return rhs % lhs == 0  
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;3 ∣ 12 // true  
&nbsp;&nbsp;&nbsp;&nbsp;4 ∣ 12 // true  
&nbsp;&nbsp;&nbsp;&nbsp;5 ∣ 12 // false

Jacob

> **···**
>
> On Thu, Apr 7, 2016 at 11:13 AM, Milos Rankovic via swift-evolution \< swift-evolution@swift.org\> wrote:
> 
> > Thank you for giving this a thought!
> > 
> > milos
> > 
> > On 7 Apr 2016, at 15:13, Erica Sadun \<erica@ericasadun.com\> wrote:
> > 
> > On Apr 6, 2016, at 10:13 AM, Milos Rankovic via swift-evolution \< \> swift-evolution@swift.org\> wrote:
> > 
> > Checking for divisibility is very common:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 % 3 == 0 // true
> > 
> > In fact, this is such a common use of the `%` operator that the `== 0`  
> > side of the expression seems distracting in this use case. For quite a  
> > while now, I’ve been using a custom operator for this, which is steadily  
> > growing on me:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== 3 // true
> > 
> > … which also allows me to overload it for sequences:
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;21 %== [7, 3] // true
> > 
> > (If I’m inadvertently misusing this mailing list to share such a minor  
> > idea, please tell me off so that I can learn not to do it again!)
> > 
> > While modulo checks are common, I don't think that your proposed solution  
> > (%==) enhances readability or saves typing \*\*to such extent\*\* that it  
> > vastly improves over the existing art:
> > 
> > 21 % 3 == 0 reads easily from left to right, is quick to type, is  
> > understood across many languages.
> > 
> > 21 %== 3 saves a few spaces, is less immediately understandable (due  
> > to the visual overlap with `+=` and `-=`) and would be (as far as I'm aware  
> > of) unique to Swift.
> > 
> > I applaud the thinking and creativity but I would not support the proposal.
> > 
> > -- E
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-evolution mailing list  
> > swift-evolution@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-evolution](https://lists.swift.org/mailman/listinfo/swift-evolution)

---

<div class="post-metadata">

**Author:** ![Milos\_Rankovic1](https://avatars.discourse-cdn.com/v4/letter/m/7ab992/32.png) [@Milos\_Rankovic1](https://forums.swift.org/u/Milos_Rankovic1)\
**Post date:** [April 11, 2016, 7:30pm UTC](https://forums.swift.org/t/divisible-by-operator/2078/7 "2016-04-11T19:30:06Z")

</div>

And a Unicode character too! That’s great to know, Jacob, thanks!

milos

> **···**
>
> > On 11 Apr 2016, at 02:39, Jacob Bandes-Storch \<jtbandes@gmail.com\> wrote:
> > 
> > I'm not suggesting this for the standard library (or anyone's production code), but it's fun to note that there is actually a standard mathematical notation for this, and it has a corresponding Unicode character U+2223 "DIVIDES": ∣
