# Notes on numerics in Swift

**URL:** <https://forums.swift.org/t/notes-on-numerics-in-swift/15746>\
**Category:** Using Swift\
**Tags:** numerics\
**Created:** [September 2, 2018, 1:11am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746 "2018-09-02T01:11:18Z")\
**Posts on this page:** 13\
**Page:** 1

<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:** [September 2, 2018, 1:11am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/1 "2018-09-02T01:11:18Z")

</div>

In the spirit of Ted's [RFC on making swift.org a more valuable resource](https://forums.swift.org/t/rfc-making-swift-org-a-more-valuable-resource-for-the-swift-community/15303), I thought I'd share something I've been working on for a while:

A great number of questions are raised in these forums as to the functioning and design of numeric types and protocols in Swift. Sometimes, the answer can be found only by digging through the source code, and even then it's hard to place what you learn there within a larger context. Therefore, I've compiled a series of articles that delve into these topics in more detail.

_Who's it for?_ Anyone who's working intensively with Swift but less familiar with its numeric types, or anyone who's working intensively with numbers in other programming languages but less familiar with Swift. Hopefully, you'll find tidbits you didn't know but find useful:

**[NOTES ON NUMERICS IN SWIFT](https://numerics.diploid.ca/)**

**[Concrete integer types, part 1](https://numerics.diploid.ca/integers-part-1.html)**  
Introduction, integer literals, conversions among integer types

**[Concrete integer types, part 2](https://numerics.diploid.ca/integers-part-2.html)**  
Operator precedence, overflow behavior, integer remainder, bitwise operations

**[Concrete binary floating-point types, part 1](https://numerics.diploid.ca/floating-point-part-1-rev-1.html)**  
Introduction (IEEE 754, C mathematical functions, finite constants)

**[Concrete binary floating-point types, part 2](https://numerics.diploid.ca/floating-point-part-2.html)**  
Floating-point precision (striding, fused multiply-add, unit in the last place, approximating π, subnormal values on 32-bit ARM, string representation)

**[Concrete binary floating-point types, part 3](https://numerics.diploid.ca/floating-point-part-3.html)**  
Float literals, conversions between floating-point types, other initializers

**[Concrete binary floating-point types, part 4](https://numerics.diploid.ca/floating-point-part-4.html)**  
Signed zero, infinity, and NaN; floating-point remainder; significand representation

**[Numeric types in Foundation](https://numerics.diploid.ca/numeric-types-in-foundation.html)**  
Foundation.Decimal, Foundation.NSNumber

**[Numeric protocols](https://numerics.diploid.ca/numeric-protocols.html)**  
Introduction, design rationale, generic algorithms, conformance

The source repository for these documents is at [GitHub - xwu/xwu-swift-numerics: Notes on numerics in Swift](https://github.com/xwu/xwu-swift-numerics). Corrections welcome. And, of course, if the community feels that this would be the sort of thing that should be available on [swift.org](http://swift.org), I'm happy to contribute.

---

<div class="post-metadata">

**Author:** ![Panajev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/panajev/32/1227_2.png) [@Panajev](https://forums.swift.org/u/Panajev)\
**Post date:** [September 2, 2018, 6:57am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/2 "2018-09-02T06:57:44Z")

</div>

Thanks Xiaodi, lots of hard work poured into it :)! This kind of content is exactly what Swift needs (... and we need more of “teaching Swift quirks and gems from a position of practical experience)

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [September 2, 2018, 7:21am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/3 "2018-09-02T07:21:30Z")

</div>

Great work - hopefully it will be put onto [Swift.org](http://Swift.org). Java benefited from similar efforts like, e.g. the books Effective Java and Java Puzzlers.

---

<div class="post-metadata">

**Author:** ![Morten\_Bek\_Ditlevsen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/morten_bek_ditlevsen/32/28133_2.png) [@Morten\_Bek\_Ditlevsen](https://forums.swift.org/u/Morten_Bek_Ditlevsen)\
**Post date:** [September 2, 2018, 12:11pm UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/4 "2018-09-02T12:11:50Z")

</div>

Just read about Decimal and NSNumber. This is so well written and contains so many interesting details. Looking forward to reading the rest! Thanks for doing this!

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [September 2, 2018, 3:45pm UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/5 "2018-09-02T15:45:49Z")

</div>

~~Any chance you could provide one single markdown file somewhere? I‘d convert it to pdf and read in in iBooks on the go.~~ Never mind. ;)

---

<div class="post-metadata">

**Author:** ![Dante-Broggi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dante-broggi/32/7955_2.png) [@Dante-Broggi](https://forums.swift.org/u/Dante-Broggi)\
**Post date:** [September 2, 2018, 6:24pm UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/6 "2018-09-02T18:24:52Z")

</div>

One thing I have recently wondered is if `Numeric.magnitude` should actually guarantee that it will be the absolute value of `self`?

All stdlib types use it that way, but for complex values it may be better for it to be the _square_ of the absolute value (aka `x.conjugate * x`), and I don't think there is any syntactic requirements that would prohibit this usage.

Of course, `BinaryInteger` would still guarantee that `self.Magnitude` will be the absolute value of `self`.

---

<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:** [September 3, 2018, 12:29am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/7 "2018-09-03T00:29:00Z")

</div>

It is guaranteed: " For any numeric value `x` , `x.magnitude` is the absolute value of `x` ."

---

<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:** [September 3, 2018, 1:16am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/8 "2018-09-03T01:16:31Z")

</div>

Making `magnitude` be the squared absolute value is a mess. There's a bunch of undesirable fallout, but the worst is that it means that integer magnitude always requires a type with twice as many bits to represent.

---

<div class="post-metadata">

**Author:** ![Dante-Broggi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/dante-broggi/32/7955_2.png) [@Dante-Broggi](https://forums.swift.org/u/Dante-Broggi)\
**Post date:** [September 3, 2018, 2:50am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/9 "2018-09-03T02:50:16Z")

</div>

As I said above I still think `BinaryInteger`s would still require that `magnitude` be the absolute value.

* * *

However, if general `Numeric` permitted square norm `Magnitude`s, it would permit non-linear types like complex values to have `Magnitude`s that do not have to represent arbitrary sums of square roots.

A hypothetical complex number's `Magnitude` currently has to represent not just `√2` and `√3` for `(1 + .i).magnitude` and `(2 + .i).magnitude` respectively, but also `√2 + √3` for `(1 + .i).magnitude + (2 + .i).magnitude`.

I don't know how many unique values can be obtained by a sum of an (n + m) bit complex number's magnitudes, but I suspect it will be more than for a square norm `Magnitude`, and be harder to implement.

* * *

But it would be a semantics breaking change to move the guarantee of `Numeric` down to `BinaryInteger`, so it will probably not happen.

---

<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:** [September 3, 2018, 3:02am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/10 "2018-09-03T03:02:50Z")

</div>

You’re basically talking about the field norm, I think, which does not really correspond to a useful notion of magnitude outside of that domain, and certainly causes a lot of bugs in C++ complex codes.

---

<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:** [September 3, 2018, 10:31am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/11 "2018-09-03T10:31:01Z")

</div>

This is insanely useful, thank you very much.

---

<div class="post-metadata">

**Author:** ![hlovatt](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hlovatt/32/3044_2.png) [@hlovatt](https://forums.swift.org/u/hlovatt)\
**Post date:** [September 4, 2018, 1:29am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/12 "2018-09-04T01:29:30Z")

</div>

In most engineering code magnitude/abs of a complex number, `x + i y`, is `sqrt(x^2 + y^2)`. Similarly vectors.

---

<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:** [September 4, 2018, 10:34am UTC](https://forums.swift.org/t/notes-on-numerics-in-swift/15746/13 "2018-09-04T10:34:48Z")

</div>

> [@hlovatt](#):
>
> In most engineering code magnitude/abs of a complex number, `x + i y` , is `sqrt(x^2 + y^2)` . Similarly vectors.

It's the same in complex analysis really.
