Introduction

Cherubin is my C++ library for computing very large numbers represented as strings. One of the hardest functions to implement efficiently in this context is the exponential exp(x). A naïve implementation using the Taylor series converges slowly, requires repeated big-number multiplications and divisions, and becomes impractical very quickly.

With chexp3, I managed to dramatically reduce the execution time of the exponential function by splitting the input into its integer part and fractional part, and computing them separately. This way, the function avoids huge Taylor expansions and only uses lightweight operations when needed.

The Optimization Strategy

Here’s the core idea of chexp3:

Why This Works

Instead of expanding everything with a Taylor series (which would require dozens of terms and huge factorial divisions for large x), chexp3 reduces the computation to two much smaller problems:

This drastically reduces the number of big-number operations while keeping the result accurate enough for Cherubin’s goals.

The Implementation

std::string chexp3(std::string &x, std::string base = "2.7182818") {
  if (x == "0") return "1";
  // ...
  // Split integer and fractional part
  // Compute exp(integer) via multiplications of base
  // Compute exp(fractional) via double exp()
  // Combine results
}

Results

By using this integer+fractional decomposition, the runtime of exp(x) in Cherubin is reduced by an order of magnitude compared to a naïve Taylor expansion. Large inputs that previously took an impractical amount of time can now be computed much faster.

Conclusion

This optimization shows how mathematical decomposition can turn an otherwise infeasible big-number calculation into a practical one. chexp3 doesn’t try to compute exp(x) purely symbolically with strings — instead, it mixes strategies: string-based multiplications for the integer part, and a fast floating-point exponential for the fractional part.

The result: a vastly faster exponential function inside Cherubin, proving once again that sometimes the best way to handle big numbers is to combine clever math tricks with pragmatic shortcuts.

👉 Check out the full library here: Cherubin on GitHub



Comment


Not that much comments



Next