I have a project I would like to do in Swift, but I need 32 digit precision for my floating point arithmetic and properties. Can anyone tell me what would be the simplest way to get quad precision in Swift?
Asked
Active
Viewed 117 times
1 Answers
0
Try swift-bignum.
From their docs:
import BigNum
BigRat.sqrt(2, precision: 128) // 240615969168004511545033772477625056927/170141183460469231731687303715884105728
BigFloat.exp(1, precision: 128) // 2.718281828459045235360287471352662497759

Tamás Sengel
- 55,884
- 29
- 169
- 223
-
Thank you. Such arbitrary precision packages are quite slow, but seem to be my only option for now. – Max Sep 06 '22 at 02:46
-
1BigNum and similar packages are slow because they use arrays to store digits. All that dynamic memory allocation/deallocation is really awful for performance. I wrote my own package (https://github.com/chipjarred/BigMath). The `master` branch just has integers, the floating point stuff is still in a separate branch, `FloatingPoint`. It uses a binary floating point format similar to `Float` and `Double`, and it's fast. The one slow point, and it's a very, very slow point, is converting to decimal for output, and that was a killer for my use, so development is on hold. – Chip Jarred Sep 11 '22 at 07:25
-
1However apart from conversion to decimal, it's fairly complete, so it may be worth taking a look at. Be warned though, I do all sorts of ugly stuff to get the performance that I do... lots of "unsafe" pointer stuff that Swift really tries hard to prevent you from doing precisely because it's not safe. It's the kind of stuff that should raise huge red flags in a code review, but it made measurable gains in performance. – Chip Jarred Sep 11 '22 at 07:29
-
@ChipJarred Feel free to leave this as a separate answer, this is useful info, thank you for sharing! – Tamás Sengel Sep 11 '22 at 09:29
-
@TamásSengel, by chance, not long after posting that comment, my need for high precision floating point came back to me, so I'm taking another stab at it, but not with `BigMath` package. Since conversion from binary to decimal was such a huge deal (mainly because I allow enormous exponents), I decided that I need to do it in a base that is easily convertible to decimal. I decided against BCD, because it would be too slow for my needs without suitable Swift compiler intrinsics or a way to do inline assembler to access the BCD instructions without a real function call to C or the like. – Chip Jarred Sep 19 '22 at 14:26
-
1I'm doing it in base 10^18, the largest integer power of 10 that will fit into a 64-bit integer. It won't be as fast as the binary format for computation, but hopefully being able to work on 8 bytes of data at a time will help - and maybe I can figure a way to use SIMD to speed it up more. Anyway, when I get to the point of having some decent floating point support, I'll try to remember to post an answer then. – Chip Jarred Sep 19 '22 at 14:29