[GSoC2021] Decimal64 in Swift

Hello everyone. My name is Song Zhi.

I am a second-year Software Engineering student from Xi’an University of Post & Telecommunications, China.

I have a strong curiosity to implement Decimal64 in Swift with @scanon after knowing relevant knowledge. I have algorithm competition experience and I enjoy math, numbers computing, and all kinds of data structures. In the numerical area, I learned several codes like Base64, MD5, Huffman Code and their applications and implementations in programming languages and there seems to be something magical behind numbers that attracts me a lot. With experience in C/C++ and JavaScript, I implemented Canonical Huffman encoder and decoder with C++, chatroom base on network programming skill, my ls command and my_shell in UNIX environment.

After roughly looking at the introduction and implementation in Java (BigDecimal), C#, and Python of decimal floating-point, I have some ideas on it.

  1. Swift has its Decimal structure, which bridges to the NSDecimalNumber class(Apple Developer Documentation). An NSDecimalNumber instance can be expressed as mantissa x 10^exponent where mantissa is a decimal integer up to 38 digits long, and the exponent is an integer from –128 through 127. If we represent 99999999999999999999999999999999999999(max length digits) to binary, it will take 128bits. Therefore, an NSDecimalNumber instance takes more than 128 bits.


Decimal in Swift is similar to the decimal module in CSharpe.

I don’t know too much on Swift, so 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?

  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?

  2. To implement Decimal64, could you tell me the rough thoughts of the whole process and basic theory?
    What functions I should implement?

I believe the power of open source and cooperation with people from different backgrounds can split problems into small pieces like the decimal module in Python was accomplished by people's cooperation and open source.
Looking forward to your reply :)

5 Likes

Thanks for the interest and welcome!

Paging the mentor for this project: @scanon

Okay, thanks :)

I made a single thread with some more details and to address any questions here: Notes on GSoC Decimal arithmetic. Please ask away.

1 Like