# Notes on GSoC Decimal arithmetic

**URL:** <https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789>\
**Category:** Swift Numerics\
**Tags:** gsoc-2021\
**Created:** [March 21, 2021, 3:01pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789 "2021-03-21T15:01:15Z")\
**Posts on this page:** 9\
**Page:** 1

<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 21, 2021, 3:01pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/1 "2021-03-21T15:01:16Z")

</div>

Hi all --

A few people have reached out about the Decimal64 GSoC project, so I thought I would write up _one_ set of notes and have a single thread to answer any questions.

This project is about implementing arithmetic and basic operations in the IEEE 754 Decimal 64 format ([decimal64 floating-point format - Wikipedia](https://en.wikipedia.org/wiki/Decimal64_floating-point_format)). This is distinct from Foundation.Decimal provided by Foundation, and from "big decimal" arbitrary-precision formats provided in some languages. The Wikipedia page (and links from there) is a good resource to familiarize yourself with the basics. The formal reference is the IEEE 754 standard, but that document costs money, so I would not expect you to have a copy while writing a proposal. I will arrange for you to have access if your proposal is funded.

Decimal64 will be delivered in a new module within Swift Numerics; following a period of some polish, I would expect it to be merged into the standard library at some future point (this will require a Swift Evolution proposal, which you might choose to do or help with, but that is outside the scope of this GSoC project).

There are a few implementations of Decimal64 in other languages. The most widely used open-source implementation that I know of is Intel's ([misc/intel](http://www.netlib.org/misc/intel/)), written in C and BSD-licensed. It is useful as a reference (especially for testing purposes), but we will probably make some different choices in the course of our own implementation.

The Decimal64 type should end up conforming to the following protocols:

- FloatingPoint ([Apple Developer Documentation](https://developer.apple.com/documentation/swift/floatingpoint)).
- A new DecimalFloatingPoint protocol mirroring BinaryFloatingPoint ([Apple Developer Documentation](https://developer.apple.com/documentation/swift/binaryfloatingpoint)).

Essentially all of the API surface of the type flows from these two protocols (and the protocols they refine), so there is little or no API design to be done as part of this project\*, and the semantics of these operations are almost fully specified by the IEEE 754 bindings, so this is in some sense "pure" implementation--there is a nearly complete specification, all that is to be done is to make it work.

(\*) A follow-on / stretch task would be to define _new_ API that handles rounding control, which Swift doesn't currently model for binary floating-point.

---

<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 21, 2021, 3:07pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/2 "2021-03-21T15:07:22Z")

</div>

Questions from other threads:

> [@](#):
>
> 1. I am curious about what different between Decimal64 and NSDecimalNumber. NSDecimalNumber seems to have all Decimal functions, why we should implement Decimal64 and Decimal128 additionally?

`Foundation.Decimal` (a.k.a `NSDecimal`) is fine, but pre-dates the IEEE 754 standard for decimal arithmetic, and makes some different choices than the standard does. For the purposes of interoperation and data interchange with other languages, it is more desirable to have an implementation of the standardized arithmetic in Swift.

> [@](#):
>
> 1. In some languages like Python and Java, they can adjust precision freely. Why we don’t implement the same kind of function? For performance or other reasons?

That's a perfectly useful feature, but it's a fundamentally different feature. Fixed-size types are especially useful for data interchange, but also provide opportunities for much better performance. Just like we have Float and Double and Int, we want to have fixed-width decimal as well.

> [@](#):
>
> 1. To implement Decimal64, could you tell me the rough thoughts of the whole process and basic theory? What functions I should implement?

This is addressed in my first post; the initial API is entirely determined by the protocols to which the type would conform and the semantics determined by the bindings of those requirements to the IEEE 754 standard.

---

<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 21, 2021, 3:51pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/3 "2021-03-21T15:51:26Z")

</div>

Probably useful to link to your earlier reply as well with a bunch of useful information:

> [@\[GSoC 2021\] Swift Numerics: Decimal64](https://forums.swift.org/t/gsoc-2021-swift-numerics-decimal64/45510/2):
>
> Decimal128 isn't really harder than 64, but it requires more machinery be built to support it. We already have 64 bit integer arithmetic (including double-width multiply and divide) in Swift, which makes building Decimal64 relatively straightforward, once you implement some basic primitives for packing and unpacking the representation and doing rounding. Decimal128 requires building a bunch of primitives for Int128 first. I will probably have already done this by the time a GSoC project gets s…

---

<div class="post-metadata">

**Author:** ![briansun](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/briansun/32/15901_2.png) [@briansun](https://forums.swift.org/u/briansun)\
**Post date:** [April 6, 2021, 2:37pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/4 "2021-04-06T14:37:01Z")

</div>

Hi, all  
As [Swift.org - Project Ideas for GSoC 2021](https://swift.org/gsoc2021/#swift-numerics-decimal64) mentioned, we can accomplish the project in C or Swift. (I know there's a way to execute C code in Swift by the bridging header file.) However, I found all implementations are written in Swift when I take a look into the swift-numerics repository. Can we implement Decimal Floating-Point in C because of lack of familiarity with Swift? Does it matter whether to implement it in C or Swift?

---

<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:** [April 6, 2021, 2:49pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/5 "2021-04-06T14:49:50Z")

</div>

Part of the motivation for the project is to exercise parts of the Swift compiler and optimizer that are relatively less-used. Therefore this should be implemented in Swift (also, if we were going to do a C implementation, we would simply wrap Intel's existing BSD-licensed library instead).

That said, this does not require deep familiarity with Swift, because it will necessarily mainly use the more "C-like" parts of the Swift language.

---

<div class="post-metadata">

**Author:** ![Terje](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Terje](https://forums.swift.org/u/Terje)\
**Post date:** [November 30, 2021, 10:07am UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/6 "2021-11-30T10:07:22Z")

</div>

Was this GSoC proposal accepted / did this move forward?

Not finding much about it beyond this post and those from potential implementers.

---

<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:** [November 30, 2021, 4:40pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/7 "2021-11-30T16:40:49Z")

</div>

No. I am planning to do an implementation myself sometime in the middle-future, unless someone decides to give it a go.

---

<div class="post-metadata">

**Author:** ![Terje](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Terje](https://forums.swift.org/u/Terje)\
**Post date:** [November 30, 2021, 6:45pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/8 "2021-11-30T18:45:12Z")

</div>

Is it a pure swift implementation or is c / c++ required?  
How much time would this feature take?

---

<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:** [November 30, 2021, 8:11pm UTC](https://forums.swift.org/t/notes-on-gsoc-decimal-arithmetic/46789/9 "2021-11-30T20:11:46Z")

</div>

For Swift Numerics, I would like to have a pure-swift implementation¹. It would be a fairly simple hobby project to write Swift wrappers for [Intel's BSD-licensed library](http://www.netlib.org/misc/intel/) in another package, though.

* * *

¹ I've been not very secretly landing some of the [integer primitives](https://github.com/apple/swift-numerics/pull/203) that make building it easier already.
