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.
Here’s the core idea of chexp3
:
x
(as a string) is split into partint
and partflt
.
Example: x = "12.34"
→ partint = "12"
, partflt = "34"
.exp(integer)
by repeated multiplication:
The base e
(~2.7182818) is multiplied by itself partint
times.
This avoids unnecessary series expansions and directly builds up the integer power of e
.exp(fractional)
with floating-point:
The fractional part is converted to a double
, and exp()
from the standard library is used.
This gives a fast and accurate result for the fractional contribution.multflt2
.
exp(x) = exp(integer) × exp(fractional)
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:
e
repeatedly)This drastically reduces the number of big-number operations while keeping the result accurate enough for Cherubin’s goals.
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
}
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.
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