Skip to content

Commit

Permalink
Increased MathHelper.ApproximateAcos accuracy.
Browse files Browse the repository at this point in the history
  • Loading branch information
RossNordby committed Dec 31, 2020
1 parent af6991e commit 4b72cd6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion BepuPhysics/BepuPhysics.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.3.0-beta10</Version>
<Version>2.3.0-beta11</Version>
<Company>Bepu Entertainment LLC</Company>
<Authors>Ross Nordby</Authors>
<Description>Speedy real time physics simulation library.</Description>
Expand Down
2 changes: 1 addition & 1 deletion BepuUtilities/BepuUtilities.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<AssemblyName>BepuUtilities</AssemblyName>
<RootNamespace>BepuUtilities</RootNamespace>
<TargetFramework>netstandard2.0</TargetFramework>
<Version>2.3.0-beta10</Version>
<Version>2.3.0-beta11</Version>
<Company>Bepu Entertainment LLC</Company>
<Authors>Ross Nordby</Authors>
<Description>Supporting utilities library for BEPUphysics v2.</Description>
Expand Down
21 changes: 14 additions & 7 deletions BepuUtilities/MathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,22 @@ public static void Sin(in Vector<float> x, out Vector<float> result)
}


/// <summary>
/// Computes an approximation of arccos. Maximum error less than 6.8e-5.
/// </summary>
/// <param name="x">Input value to the arccos function.</param>
/// <param name="acos">Result of the arccos function.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void ApproximateAcos(in Vector<float> x, out Vector<float> acos)
public static void ApproximateAcos(Vector<float> x, out Vector<float> acos)
{
//TODO: Could probably do better than this. Definitely don't need to use a square root.
//acos(x) ~= (pi / (2 * sqrt(2))) * sqrt(2 - 2 * x), for 0<=x<=1
//acos(x) ~= pi - (pi / (2 * sqrt(2))) * sqrt(2 + 2 * x), for -1<=x<=0
var two = new Vector<float>(2f);
acos = new Vector<float>(1.11072073454f) * Vector.SquareRoot(Vector.Max(Vector<float>.Zero, two - two * Vector.Abs(x)));
acos = Vector.ConditionalSelect(Vector.LessThan(x, Vector<float>.Zero), new Vector<float>(Pi) - acos, acos);
//Adapted from Handbook of Mathematical Functions by Milton Abramowitz and Irene A. Stegun.
var negate = Vector.ConditionalSelect(Vector.LessThan(x, Vector<float>.Zero), Vector<float>.One, Vector<float>.Zero);
x = Vector.Abs(x);
acos = new Vector<float>(-0.0187293f) * x + new Vector<float>(0.0742610f);
acos = (acos * x - new Vector<float>(0.2121144f)) * x + new Vector<float>(1.5707288f);
acos *= Vector.SquareRoot(Vector.Max(Vector<float>.Zero, Vector<float>.One - x));
acos -= new Vector<float>(2) * negate * acos;
acos = negate * new Vector<float>(3.14159265358979f) + acos;
}


Expand Down

0 comments on commit 4b72cd6

Please sign in to comment.