Skip to content

Instantly share code, notes, and snippets.

View JcBernack's full-sized avatar
:shipit:
The same thing we do every night, Pinky.

Jan Christoph Bernack JcBernack

:shipit:
The same thing we do every night, Pinky.
View GitHub Profile
@JcBernack
JcBernack / BigDecimal.cs
Last active April 29, 2024 09:20
BigDecimal
using System;
using System.Numerics;
namespace Common
{
/// <summary>
/// Arbitrary precision decimal.
/// All operations are exact, except for division. Division never determines more digits than the given precision.
/// Source: https://gist.github.com/JcBernack/0b4eef59ca97ee931a2f45542b9ff06d
/// Based on https://stackoverflow.com/a/4524254
/// <summary>
/// Fast exponential approximation.
/// </summary>
/// <remarks>
/// Based on "A Fast, Compact Approximation of the Exponential Function" by Nicol N.Schraudolph (1999)
/// <code>e^x ~ a*x + b
/// a = 2 ^ (mantissa bits) / ln(2) ~ 12102203
/// b = (exponent bias) * 2^(mantissa bits) ~ 1065353216</code>
/// </remarks>
/// <param name="x">A number specifying a power.</param>