# Shortcuts and Modulo Calculations

The i*OS Shortcuts application has a surprising amount of numerical functions tucked away within its calculation actions. These include not only statistical functions, but also some arithmetic functions familiar to many programmers. One of these is the modulus function. Many of you may have used this in your mathematical studies at school. It is frequently used as the basis for time calculations on 12 and 24 hour clocks. But there’s more than one way to apply a modulus.

Let’s define a generic modulus operation and have it accept two numbers. A value, and a modulus - `mod(value,modulus)`. To find the result, we would divide the value by the modulus and look at the remainder.

For example, using a modulus of 3, we can see a pattern that forms like so.

``````mod(1,3) = 1 => 1 / 3 = 0 r 1
mod(2,3) = 2 => 2 / 3 = 0 r 2
mod(3,3) = 0 => 3 / 3 = 0 r 3
mod(4,3) = 1 => 4 / 3 = 1 r 1
mod(5,3) = 2 => 5 / 3 = 1 r 2
mod(6,3) = 0 => 6 / 3 = 1 r 3
mod(7,3) = 1 => 7 / 3 = 2 r 1
``````

Hopefully that’s relatively straight forward to follow. But what if we aren’t dealing with whole number, integer values?

If we take the broadest example above of looking at the remainder, then `mod(5.6,3) = 2.6 => 5.6 / 3 = 1 r 2.6`. But, some programming definitions of a modulus go a little further and and say that it is not simply the value divided by the modulus and look at the remainder, but rather the division should be integer division, thus meaning only the integer part of the remainder would be retained. By that definition, `mod(5.6,3) = 2 => INTEGER_PART(5.6 / 3) = 1 r 2`.

Some programming languages implement one version of modulus. Other languages implement the other. A few (such as C), implement both - one for integers and one for non-integers (such as C’s `fmod()` function for floating point (float) numbers).

The Shortcuts modulus function is based on integer division, but sometimes it can be useful to have a modulus function with a broader operation that retains the factional part. To that end I created a little shortcut to suport just that.

The shortcut takes a single list parameter. It expects the first list item to be the value anf the second list item to be the modulus. It ignores other list items, and it does not carry out any validation against the values it receives. The modulus is calculated based on the following equation.

``````value - INTEGER_PART((value / modulus) * modulus)
``````

Since Shortcuts has no integer part operation, that is derived by splitting a number on the decimal point and taking the first item from the resulting list. If you are in a country that uses a different decimal saparator such as a comma (“,”), then you would need to modify the shortcut accordingly.

Below are two shortcuts, Modulus, and Modulus Test. Modulus is the shortcut that carries out the calculation shown above. Modulus Test is an example shortcut that calls both Modulus, and performs Shortcuts’ own modulus calculation and shows the results alongside each other. It’s pre-populated with the `mod(5.6,3)` example from above and gives the following result:

Shortcuts

The Modulus shortcut’s actions break down as follows.

The moral of this story is that even when Shortcuts doesn’t provide the exact function you are looking for, there’s almost always a way to simply build it yourself.

Author: Stephen Millard
Tags: | shortcuts |

• ## Github Pages Publish Notification

22 Feb 2020

Last year I finally made my big web site move from Squarespace to Github Pages. In that post I went into some of the background of the move, and while the vast majority of things have improved in terms of my requirements, one thing has been much worse. For the site I use a static web site generator known as Jekyll. It works by converting various configuration templates and content files into a web site. Locally, on my Macbook Pro, I tend to use an incremental generation during my testing and within a few seconds it churns out the new site. With a full site generation it actually takes closer to a minute. This site is many hundreds of cross referenced pages, so it takes a while.

When I upload the content to Github Pages, which also utilises Jekyll, it takes even longer. Minutes in fact. This is where it falls down in comparison to Squarespace. I don’t get a near instant publish. This means that I could inadvertently post links too soon, or I have to check back to see if it has published yet. Sometimes, there can even be failures (very few and far between fortunately). This has been an annoyance for a few months now, and so I recently took a look at creating something to help me with this.

• ## LaunchCuts Folder Set-up

12 Jan 2020

This week saw the public release of the i*OS app, LaunchCuts. It’s an app created to fill a gap in the functionality of the i*OS Shortcuts app. Specifically the challenges of trying to organise Shortcuts. It’s something I’ve covered before in my posts on smart folders for Workflow and in passing on a post about Shortcuts, my naming conventions for shortcuts. There are a few other bells and whistkles to it, but the core function is simply that it allows you to organise shortcuts into folders for ease of launching and editing; albeit outside of the Shortcut app itself. The functionality it delivers is something I’ve been waiting on for several years, and in this post I’m going to share a little bit about how I’ve set it up.

• ## Shortcuts Collection

16 Dec 2019

The Workflow app was an amazing gateway app for automation on iOS when it first appeared, and I’ve been using it since its very first release. It built on x-callback URLs and revolutionised the way people can interact with their handheld Apple devices. After it’s acquisition by Apple, the app eveolved into Shortcuts, and in 2019 underwent major developments with the introduction of third party donations through actions and advanced intents as well as automation and parameters.

Now that the ThoughtAsylum web site update is in place, I’ve turned what attention I can spare to some addtional projects; one of which is a new and ongoing collection of shortcuts.

• ## Shortcuts: Extract Contact's Mobile Phone Number

10 Sep 2019

Recently there was a query on the Automators podcast forum, where someone wanted to know how to retrieve a mobile phone number from a contact using Shortcuts. This isn’t something that Shortcuts supports “out-of-the-box”, but it is something that Shortcuts can do with a bit of cajoling. In this post I’ll explain some of the details about one way of doing it.