Skip to content

Instantly share code, notes, and snippets.

@manvscode
Forked from JcBernack/FastExp.cs
Created April 8, 2019 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manvscode/642185fc98449a7d55914d55579f2d31 to your computer and use it in GitHub Desktop.
Save manvscode/642185fc98449a7d55914d55579f2d31 to your computer and use it in GitHub Desktop.
/// <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>
/// <returns>The number e raised to the power x.</returns>
public static float FastExp(float x)
{
var temp = (int)(12102203 * x + 1065353216);
return BitConverter.ToSingle(BitConverter.GetBytes(temp), 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment