Skip to content

Instantly share code, notes, and snippets.

@IronWarrior
Created August 12, 2022 23:37
Show Gist options
  • Save IronWarrior/cf3a522001695a0f1e7e61d5f8ff1a7d to your computer and use it in GitHub Desktop.
Save IronWarrior/cf3a522001695a0f1e7e61d5f8ff1a7d to your computer and use it in GitHub Desktop.
Managed implementation of various System.Math functions.
static class MathM
{
public const float PI = (float)System.Math.PI;
public const float TwoPI = PI * 2;
public const float HalfPI = PI * 0.5f;
public static float Sin(float x)
{
float x3 = x * x * x;
float x5 = x3 * x * x;
float x7 = x5 * x * x;
// 2! 3! 4!
return x - x3 / 6 + x5 / 120 - x7 / 5040;
}
public static float Cos(float x)
{
return Sin(x + HalfPI);
}
public static float Tan(float x)
{
return Sin(x) / Cos(x);
}
public static float Asin(float x)
{
float x3 = x * x * x;
float x5 = x3 * x * x;
float x7 = x5 * x * x;
// 3 / 40 5 / 112
return x + x3 / 6 + x5 * 0.075f + x7 * 0.044642857f;
}
public static float Acos(float x)
{
return HalfPI - Asin(x);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment