Announcing `dashu`: yet another arbitrary precision library

Greetings fellow rustaceans! I'm excited to announce dashu, a new collection of libraries for arbitrary precision numbers! You can find it here at crates.io or github. It's intended to be a pure Rust alternative to GNU GMP + MPFR (and MPC in future), and it supports no_std.

The focus of dashu is ergonomics (user friendly) and readability (developer friendly), and the efficiency comes at next. It won't be the fastest big number library (at least in the near term), but I hope it will be a good learning material for somebody interested in big numbers (just like the libtommath project).

I just started learning Rust last year and it's almost a year since then. Rust is a game changer to me, it lets me think of programming in different ways. And I think it's worth it to reinvent the wheels of arbitrary precision arithmetics, thanks to Rust's good performance. Currently dashu has a crate for arbitrary precision integer, and a crate for arbitrary precision float. These two are the most difficult to implement, arbitrary precision rational numbers and complex numbers can easily be, and will likely be implemented later.

dashu-int: Arbitrary precision integers

dashu-int is a fork from the amazing crate ibig by tczajka. ibig has a well-organized code structure and decent performance. Based on this crate, I have done some rework and implemented some functions that is necessary for other dependent libraries. Compared with ibig 0.3.5 (the forked version), dashu-int now has efficient support for GCD, extended GCD, integer logarithm and modular inverse. The next improvement that I'm planning to implement is the integer square root and cubic root.

In terms of performance, dashu-int is almost the same as ibig because the implementations of most algorithms are the same. However, dashu-int has special optimization of memory consumption, so that a signed big integer only takes a stack size of three usizes.

dashu-float: Arbitrary precision floating point number

dashu-float is a new arbitrary precision float library designed and implemented from scratch. What makes it special is that, it supports arbitrary base and arbitrary rounding mode. That means that you can use it as big decimals, and you can use it to implement an interval arithmetics library.

dashu-float currently only support some basic arithmetic operations (add/sub/mul/div/exp/ln). Support for sqrt and cbrt is in the todo list, but support for other functions (like trigonometric functions) is not in the plan for v1.0. They might be implemented as a separate math crate. The main task before v1.0 is to stablize the API.

I have done a simple benchmark to compare the performance of some multiple precision float crates, the benchmark reports the time to sum up exp(1) to exp(12) at certain precision (code here). Note that twofloat and num-bigfloat has fixed precision, therefore they can benefit from polynomial approximation of some functions.

twofloat (binary, 106 bits)                                                                             
                        time:   [2.4277 us 2.5028 us 2.5868 us]
rug (binary, 100 bits)                                                                             
                        time:   [15.178 us 15.573 us 16.023 us]
dashu (binary, 100 bits)                                                                            
                        time:   [480.96 us 494.67 us 509.90 us]

rug (binary, 1000 bits)                                                                            
                        time:   [107.43 us 110.81 us 114.67 us]
dashu (binary, 1000 bits)                                                                            
                        time:   [5.5848 ms 5.7174 ms 5.8734 ms]

num-bigfloat (decimal, 40 digits)                                                                             
                        time:   [13.964 us 14.623 us 15.449 us]
dashu (decimal, 40 digits)                                                                             
                        time:   [5.7396 ms 6.0720 ms 6.4576 ms]

dashu (decimal, 100 digits)                                                                            
                        time:   [11.468 ms 11.723 ms 12.052 ms]
bigdecimal (decimal, 100 digits)                                                                            
                        time:   [41.718 ms 43.018 ms 44.435 ms]

dashu-macros: Macros for creating big numbers

dashu-macros is a utility crate to help users creating big numbers from numeric literals losslessly. For example:

use dashu_macros::*;

let a = ubig!(515377520732011331036461129765621272702107522001);
let b = ibig!(-0x4c37ceb0b2784c4ce0bf38ace408e211a7caab24308a82e8f1);
let c = fbig!(0xa54653ca_67376856_5b41f775.f00c1782_d6947d55p-33);
let d = dbig!(3.141_592_653_589_793_238_462_643_383_279_502_884);

Hope you enjoy them, and I will appreciate it if you can try them and give some feedback! Thanks!

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.