# SE-0246: Generic Math(s) Functions

**URL:** <https://forums.swift.org/t/se-0246-generic-math-s-functions/21479>\
**Category:** Proposal Reviews\
**Created:** [March 11, 2019, 9:26pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479 "2019-03-11T21:26:04Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![RMJay](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rmjay/32/5037_2.png) [@RMJay](https://forums.swift.org/u/RMJay)\
**Post date:** [March 21, 2019, 12:37am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/61 "2019-03-21T00:37:03Z")

</div>

Swift already has free functions for the most common mathematical functions, they even get their own special status as operators. I am talking about plus, multiply, divide, modulo etc. There is therefore precedent for treating certain ubiquitous, unambiguous, and established functions as special and giving them more compact representation, especially seeing as they often end up getting repeated multiple times on the same line. I think to a person who writes numeric code the elementary functions are essentially "operators". Having these as free functions seems perfectly "Swifty" to me.

---

<div class="post-metadata">

**Author:** ![ahti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahti/32/604_2.png) [@ahti](https://forums.swift.org/u/ahti)\
**Post date:** [March 21, 2019, 1:03am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/62 "2019-03-21T01:03:15Z")

</div>

A key difference (imo) here is that the operators operate on two arguments, neither of which can be said to be the "main" argument. Both sides of an add, mul, ... are equally important, and `a.add(b)` wouldn't really reflect that.

But for something like a sinus, that is not the case. And if we write "the name of the user" as `user.name`, why not write "the sine of x" as `x.sin`? I'm not necessarily saying that's how it should be, but I think there are interesting alternatives to consider here.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 21, 2019, 2:38am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/63 "2019-03-21T02:38:40Z")

</div>

> [@ahti](#):
>
> "the sinus of x"

| English | Deutsch |
| --- | --- |
| the **sinus** | die Nebenhöhle |
| the sine | der **Sinus** |

---

<div class="post-metadata">

**Author:** ![yxckjhasdkjh](https://avatars.discourse-cdn.com/v4/letter/y/278dde/32.png) [@yxckjhasdkjh](https://forums.swift.org/u/yxckjhasdkjh)\
**Post date:** [March 21, 2019, 12:26pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/64 "2019-03-21T12:26:40Z")

</div>

> [@Karl](#):
>
> `ElementaryFunctions`

It's [a valid class of functions](https://en.wikipedia.org/wiki/Elementary_function) in math with some nice behaviour, although I'm not sure how consistent the definition is across different textbooks/authors. But there should be some reasonable agreement about stuff that is obviously in this class (e.g. polynomials) and stuff that obviously isn't (like `erf` and `gamma`). Of course that name might be somewhat opaque to non-mathematicians, but I'm not sure what a better name would be.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [March 21, 2019, 2:42pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/65 "2019-03-21T14:42:03Z")

</div>

> [@yxckjhasdkjh](#):
>
> Of course that name might be somewhat opaque to non-mathematicians, but I'm not sure what a better name would be.

Well, an easy alternative would be `ElementaryMathFunctions`. Just like `Real/RealNumber` - not everybody is thinking in a numerics context all the time. It's kind of rude to just gobble up such broad English words for domain-specific purposes.

If it is important to keep the exact name `ElementaryFunctions`, _that's_ the kind of thing which should be namespaced. It's a very generic term which would be confusing to non-numerics programmers.

Protocol namespacing is actually [pretty trivial](https://github.com/apple/swift/pull/23096) to implement. We could do that and call this protocol `Math.ElementaryFunctions`.

Both are much better than leaving this very vague name floating around the standard library.

---

<div class="post-metadata">

**Author:** ![Les\_Pruszynski](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/les_pruszynski/32/797_2.png) [@Les\_Pruszynski](https://forums.swift.org/u/Les_Pruszynski)\
**Post date:** [March 21, 2019, 3:39pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/66 "2019-03-21T15:39:58Z")

</div>

> [@Karl](#):
>
> It's a very generic term which would be confusing to non-numerics programmers.

Respectfully, I'm not a numeric programmer and am not confused by it.  
As I am not confused by Real. Anyone who spent a bit of time programming would know that it refers to numbers and not strings.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 21, 2019, 5:13pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/67 "2019-03-21T17:13:27Z")

</div>

I believe the authors are planning to revise this again to remove the `Math` namespace, so I'm extending this review until Monday. I'll make sure the thread is notified when those revisions are live.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 22, 2019, 12:56am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/68 "2019-03-22T00:56:30Z")

</div>

The proposal [has been updated](https://github.com/apple/swift-evolution/blob/master/proposals/0246-mathable.md).

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [March 22, 2019, 1:03am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/69 "2019-03-22T01:03:09Z")

</div>

Specifically:

> I have dropped the `.Math` pseudo-namespace associatedtype from the protocols. In earlier versions of the proposal, the static functions were spelled `Float.Math.sin(x)` . This was motivated by a desire to avoid "magic" underscore-prefixed implementation trampolines, while still grouping these functions together under a single `.Math` in autocompletion lists. Whatever stylistic benefits this might have were judged to be not worth the extra layer of machinery that would be fixed into the ABI even if we get a "real" namespace mechanism at some future point. These functions now appear simply as `Float.sin(x)` .

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [March 22, 2019, 1:35am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/70 "2019-03-22T01:35:22Z")

</div>

Looks good to me, i fully support it as is.

One question remains tho — now that the function names have been shortened to `T.sin(x)`, (or in most type contexts, `.sin(x)`), do we really think it’s still worth the effort to vend _a whole `Math` module_ just to get rid of the leading dot?

```swift
// available everywhere 
let x = .cos(phi) * .cos(theta)

// so many conciser 
let x = cos(phi) * cos(theta)

```

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [March 22, 2019, 1:43am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/71 "2019-03-22T01:43:16Z")

</div>

The dot is frankly pretty ugly.

More seriously and more importantly, the Math module lets us obsolete the concrete free functions defined by the Platform module while maintaining source stability.

Having the dotless free functions _also_ allows users more flexibility with whitespace:

```swift
2*.cos(phi) // syntax error 
2*cos(phi) // OK!

```

and, as @Nevin noted, helps autocomplete.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 22, 2019, 2:20am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/72 "2019-03-22T02:20:30Z")

</div>

> [@scanon](#):
>
> The dot is frankly pretty ugly.
> 
> More seriously and more importantly, the Math module lets us obsolete the concrete free functions defined by the Platform module while maintaining source stability.

Right, and it is to be noted that accomplishing this--maintaining source stability (without introducing pervasive ambiguities) while vending generic global functions--can _only_ be done in a first-party library through Swift Evolution. If we don't do that here, no third party can do so, because none can remove the existing free functions.

---

<div class="post-metadata">

**Author:** ![Nevin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/nevin/32/617_2.png) [@Nevin](https://forums.swift.org/u/Nevin)\
**Post date:** [March 22, 2019, 3:54am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/73 "2019-03-22T03:54:49Z")

</div>

> [@taylorswift](#):
>
> One question remains tho — now that the function names have been shortened to `T.sin(x)` , (or in most type contexts, `.sin(x)` ), do we really think it’s still worth the effort to vend _a whole `Math` module_ just to get rid of the leading dot?
> 
> ```swift
> // available everywhere 
> let x = .cos(phi) * .cos(theta)
> 
> // so many conciser 
> let x = cos(phi) * cos(theta)
> 
> ```

There’s no autocomplete while typing the dot-prefixed static functions in the line you wrote. There is for the free functions.

---

<div class="post-metadata">

**Author:** ![yxckjhasdkjh](https://avatars.discourse-cdn.com/v4/letter/y/278dde/32.png) [@yxckjhasdkjh](https://forums.swift.org/u/yxckjhasdkjh)\
**Post date:** [March 22, 2019, 11:15am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/74 "2019-03-22T11:15:22Z")

</div>

I have a slight worry that `Math` is a bit too broad as a module name. I work on a maths-related product and although we don't currently have any internal module called `Math` (and probably won't), I could imagine that we did or that other projects might. Of course, ultimately this is a namespacing problem, but I wonder if picking something more along the lines of `SwiftMath` wouldn't be slightly safer.

Apart from that minor concern, the updated proposal seems much easier to understand and quite sensible. So +1 from me.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 26, 2019, 2:38am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/75 "2019-03-26T02:38:58Z")

</div>

I don't know that Swift has a firm policy about this as a language, but I would definitely recommend against ever using generic names for non-language-provided libraries. There should always be something of a "brand" in your module names, even internal ones — that brand can be a company name or an open-source project name or even just your own given name, but there should be _something_ distinctive about the library name that makes it unlikely that anybody else would choose it. Naming a library something like `Math` or `System` or `Database` is frankly a little presumptuous for a project without community blessing (it'd be fine for, say, the server working group to pick such a name, although even they should consider whether the base language might want it eventually).

---

<div class="post-metadata">

**Author:** ![yxckjhasdkjh](https://avatars.discourse-cdn.com/v4/letter/y/278dde/32.png) [@yxckjhasdkjh](https://forums.swift.org/u/yxckjhasdkjh)\
**Post date:** [March 26, 2019, 2:36pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/76 "2019-03-26T14:36:18Z")

</div>

> [@John\_McCall](#):
>
> I don't know that Swift has a firm policy about this as a language

This is a fair point, but I wouldn't assume that such a policy exists as of yet. For example, IBM-Swift has packages with names such as [Health](https://github.com/IBM-Swift/Health) or, particularly bad, [Configuration](https://github.com/IBM-Swift/Configuration/). There is a dependency we use that is called [OrderedDictionary](https://github.com/lukaskubanek/OrderedDictionary), even though it is quite conceivable that other implementations of ordered dictionaries might exist. One can easily find dozens of examples of popular packages that use very generic names, such as [Algorithm](https://github.com/CosmicMind/Algorithm), [Presentation](https://github.com/hyperoslo/Presentation), [Cache](https://github.com/hyperoslo/Cache), etc. It would be nice if everyone abided to certain naming standards, but especially if they're not official or semi-official (like e.g. Java's reverse-DNS package naming conventions), I doubt the problem will just solve itself. (There's even [BaseMath](https://github.com/jph00/BaseMath), currently, and at least a repository that is called [Math](https://github.com/SwiftGL/Math), even though the module name itself is fortunately `SGLMath`.)

Apparently, [this problem has been discussed before](https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20160201/001033.html).

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 26, 2019, 2:54pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/77 "2019-03-26T14:54:11Z")

</div>

I'll raise it to the rest of the Core Team; we may want to make a policy statement about it.

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [March 26, 2019, 3:43pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/79 "2019-03-26T15:43:43Z")

</div>

Swift should be enhanced with better options for module aliasing and disambiguation rather than forcing the community to regress to prefix naming.

---

<div class="post-metadata">

**Author:** ![Scott](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scott/32/23832_2.png) [@Scott](https://forums.swift.org/u/Scott)\
**Post date:** [March 26, 2019, 3:47pm UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/80 "2019-03-26T15:47:44Z")

</div>

I misread john's statement, he was specifically referring to module names. deleted my post

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 28, 2019, 12:39am UTC](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479/81 "2019-03-28T00:39:59Z")

</div>

SE-0246 has been [accepted with modifications](https://forums.swift.org/t/accepted-with-modifications-se-0246-generic-math-functions/22244). Any discussion of module namespacing as a general language feature should be taken to a dedicated thread in the Evolution \> Discussion category. Any other discussion of this proposal should continue in the acceptance announcement thread.

As always, thank you for contributing to Swift.

[Previous page](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479.md?page=3)

[Next page](https://forums.swift.org/t/se-0246-generic-math-s-functions/21479.md?page=5)
