Skip to content

Commit

Permalink
Changed Math to MathF
Browse files Browse the repository at this point in the history
Tried to avoid reducing precision of calculations this time.
  • Loading branch information
initram committed Jan 28, 2021
1 parent 5b040f6 commit 803f70a
Show file tree
Hide file tree
Showing 26 changed files with 277 additions and 208 deletions.
2 changes: 1 addition & 1 deletion MonoGame.Framework/Audio/Xact/Cue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public void Apply3D(AudioListener listener, AudioEmitter emitter)
direction /= distance;
var right = Vector3.Cross(listener.Up, listener.Forward);
var slope = Vector3.Dot(direction, listener.Forward);
var angle = MathHelper.ToDegrees((float)Math.Acos(slope));
var angle = MathHelper.ToDegrees(MathF.Acos(slope));
var j = FindVariable("OrientationAngle");
_variables[j].SetValue(angle);
if (_curSound != null)
Expand Down
6 changes: 3 additions & 3 deletions MonoGame.Framework/BoundingSphere.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ public static BoundingSphere CreateFromPoints(IEnumerable<Vector3> points)
float sqDist = diff.LengthSquared();
if (sqDist > sqRadius)
{
float distance = (float)Math.Sqrt(sqDist); // equal to diff.Length();
float distance = MathF.Sqrt(sqDist); // equal to diff.Length();
Vector3 direction = diff / distance;
Vector3 G = center - radius * direction;
center = (G + pt) / 2;
Expand Down Expand Up @@ -581,7 +581,7 @@ public BoundingSphere Transform(Matrix matrix)
{
BoundingSphere sphere = new BoundingSphere();
sphere.Center = Vector3.Transform(this.Center, matrix);
sphere.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33)))));
sphere.Radius = this.Radius * MathF.Sqrt(Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33))));
return sphere;
}

Expand All @@ -593,7 +593,7 @@ public BoundingSphere Transform(Matrix matrix)
public void Transform(ref Matrix matrix, out BoundingSphere result)
{
result.Center = Vector3.Transform(this.Center, matrix);
result.Radius = this.Radius * ((float)Math.Sqrt((double)Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33)))));
result.Radius = this.Radius * MathF.Sqrt(Math.Max(((matrix.M11 * matrix.M11) + (matrix.M12 * matrix.M12)) + (matrix.M13 * matrix.M13), Math.Max(((matrix.M21 * matrix.M21) + (matrix.M22 * matrix.M22)) + (matrix.M23 * matrix.M23), ((matrix.M31 * matrix.M31) + (matrix.M32 * matrix.M32)) + (matrix.M33 * matrix.M33))));
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion MonoGame.Framework/Graphics/PackedVector/Alpha8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public override int GetHashCode()

private static byte Pack(float alpha)
{
return (byte) Math.Round(
return (byte) MathF.Round(
MathHelper.Clamp(alpha, 0, 1) * 255.0f
);
}
Expand Down
6 changes: 3 additions & 3 deletions MonoGame.Framework/Graphics/PackedVector/Bgr565.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public struct Bgr565 : IPackedVector<UInt16>, IEquatable<Bgr565>, IPackedVector

private static UInt16 Pack(float x, float y, float z)
{
return (UInt16) ((((int) Math.Round(MathHelper.Clamp(x, 0, 1) * 31.0f) & 0x1F) << 11) |
(((int) Math.Round(MathHelper.Clamp(y, 0, 1) * 63.0f) & 0x3F) << 5) |
((int) Math.Round(MathHelper.Clamp(z, 0, 1) * 31.0f) & 0x1F));
return (UInt16) ((((int) MathF.Round(MathHelper.Clamp(x, 0, 1) * 31.0f) & 0x1F) << 11) |
(((int) MathF.Round(MathHelper.Clamp(y, 0, 1) * 63.0f) & 0x3F) << 5) |
((int) MathF.Round(MathHelper.Clamp(z, 0, 1) * 31.0f) & 0x1F));
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/Bgra4444.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public struct Bgra4444 : IPackedVector<UInt16>, IEquatable<Bgra4444>

private static UInt16 Pack(float x, float y, float z, float w)
{
return (UInt16) ((((int) Math.Round(MathHelper.Clamp(w, 0, 1) * 15.0f) & 0x0F) << 12) |
(((int) Math.Round(MathHelper.Clamp(x, 0, 1) * 15.0f) & 0x0F) << 8) |
(((int) Math.Round(MathHelper.Clamp(y, 0, 1) * 15.0f) & 0x0F) << 4) |
((int) Math.Round(MathHelper.Clamp(z, 0, 1) * 15.0f) & 0x0F));
return (UInt16) ((((int) MathF.Round(MathHelper.Clamp(w, 0, 1) * 15.0f) & 0x0F) << 12) |
(((int) MathF.Round(MathHelper.Clamp(x, 0, 1) * 15.0f) & 0x0F) << 8) |
(((int) MathF.Round(MathHelper.Clamp(y, 0, 1) * 15.0f) & 0x0F) << 4) |
((int) MathF.Round(MathHelper.Clamp(z, 0, 1) * 15.0f) & 0x0F));
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/Bgra5551.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public override int GetHashCode()
private static UInt16 Pack(float x, float y, float z, float w)
{
return (UInt16) (
(((int) Math.Round(MathHelper.Clamp(x, 0, 1) * 31.0f) & 0x1F) << 10) |
(((int) Math.Round(MathHelper.Clamp(y, 0, 1) * 31.0f) & 0x1F) << 5) |
(((int) Math.Round(MathHelper.Clamp(z, 0, 1) * 31.0f) & 0x1F) << 0) |
((((int) Math.Round(MathHelper.Clamp(w, 0, 1)) & 0x1) << 15))
(((int) MathF.Round(MathHelper.Clamp(x, 0, 1) * 31.0f) & 0x1F) << 10) |
(((int) MathF.Round(MathHelper.Clamp(y, 0, 1) * 31.0f) & 0x1F) << 5) |
(((int) MathF.Round(MathHelper.Clamp(z, 0, 1) * 31.0f) & 0x1F) << 0) |
((((int) MathF.Round(MathHelper.Clamp(w, 0, 1)) & 0x1) << 15))
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/Byte4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ static uint Pack(ref Vector4 vector)
const float min = 0.0f;

// clamp the value between min and max values
var byte4 = (uint) Math.Round(MathHelper.Clamp(vector.X, min, max)) & 0xFF;
var byte3 = ((uint) Math.Round(MathHelper.Clamp(vector.Y, min, max)) & 0xFF) << 0x8;
var byte2 = ((uint) Math.Round(MathHelper.Clamp(vector.Z, min, max)) & 0xFF) << 0x10;
var byte1 = ((uint) Math.Round(MathHelper.Clamp(vector.W, min, max)) & 0xFF) << 0x18;
var byte4 = (uint) MathF.Round(MathHelper.Clamp(vector.X, min, max)) & 0xFF;
var byte3 = ((uint) MathF.Round(MathHelper.Clamp(vector.Y, min, max)) & 0xFF) << 0x8;
var byte2 = ((uint) MathF.Round(MathHelper.Clamp(vector.Z, min, max)) & 0xFF) << 0x10;
var byte1 = ((uint) MathF.Round(MathHelper.Clamp(vector.W, min, max)) & 0xFF) << 0x18;

return byte4 | byte3 | byte2 | byte1;
}
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Graphics/PackedVector/NormalizedByte2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ public override string ToString()

private static ushort Pack(float x, float y)
{
var byte2 = (((ushort) Math.Round(MathHelper.Clamp(x, -1.0f, 1.0f) * 127.0f)) & 0xFF) << 0;
var byte1 = (((ushort) Math.Round(MathHelper.Clamp(y, -1.0f, 1.0f) * 127.0f)) & 0xFF) << 8;
var byte2 = (((ushort) MathF.Round(MathHelper.Clamp(x, -1.0f, 1.0f) * 127.0f)) & 0xFF) << 0;
var byte1 = (((ushort) MathF.Round(MathHelper.Clamp(y, -1.0f, 1.0f) * 127.0f)) & 0xFF) << 8;

return (ushort)(byte2 | byte1);
}
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/NormalizedByte4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public override string ToString()

private static uint Pack(float x, float y, float z, float w)
{
var byte4 = (((uint) Math.Round(MathHelper.Clamp(x, -1.0f, 1.0f) * 127.0f)) & 0xff) << 0;
var byte3 = (((uint) Math.Round(MathHelper.Clamp(y, -1.0f, 1.0f) * 127.0f)) & 0xff) << 8;
var byte2 = (((uint) Math.Round(MathHelper.Clamp(z, -1.0f, 1.0f) * 127.0f)) & 0xff) << 16;
var byte1 = (((uint) Math.Round(MathHelper.Clamp(w, -1.0f, 1.0f) * 127.0f)) & 0xff) << 24;
var byte4 = (((uint) MathF.Round(MathHelper.Clamp(x, -1.0f, 1.0f) * 127.0f)) & 0xff) << 0;
var byte3 = (((uint) MathF.Round(MathHelper.Clamp(y, -1.0f, 1.0f) * 127.0f)) & 0xff) << 8;
var byte2 = (((uint) MathF.Round(MathHelper.Clamp(z, -1.0f, 1.0f) * 127.0f)) & 0xff) << 16;
var byte1 = (((uint) MathF.Round(MathHelper.Clamp(w, -1.0f, 1.0f) * 127.0f)) & 0xff) << 24;

return byte4 | byte3 | byte2 | byte1;
}
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Graphics/PackedVector/NormalizedShort2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ private static uint PackInTwo (float vectorX, float vectorY)

// clamp the value between min and max values
// Round rather than truncate.
var word2 = (uint)((int)MathHelper.Clamp((float)Math.Round(vectorX * maxPos), minNeg, maxPos) & 0xFFFF);
var word1 = (uint)(((int)MathHelper.Clamp((float)Math.Round(vectorY * maxPos), minNeg, maxPos) & 0xFFFF) << 0x10);
var word2 = (uint)((int)MathHelper.Clamp(MathF.Round(vectorX * maxPos), minNeg, maxPos) & 0xFFFF);
var word1 = (uint)(((int)MathHelper.Clamp(MathF.Round(vectorY * maxPos), minNeg, maxPos) & 0xFFFF) << 0x10);

return (word2 | word1);
}
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/NormalizedShort4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ private static ulong PackInFour(float vectorX, float vectorY, float vectorZ, flo
const long minNeg = -maxPos;

// clamp the value between min and max values
var word4 = (ulong)((int)Math.Round(MathHelper.Clamp(vectorX * maxPos, minNeg, maxPos)) & mask);
var word3 = (ulong)((int)Math.Round(MathHelper.Clamp(vectorY * maxPos, minNeg, maxPos)) & mask) << 0x10;
var word2 = (ulong)((int)Math.Round(MathHelper.Clamp(vectorZ * maxPos, minNeg, maxPos)) & mask) << 0x20;
var word1 = (ulong)((int)Math.Round(MathHelper.Clamp(vectorW * maxPos, minNeg, maxPos)) & mask) << 0x30;
var word4 = (ulong)((int)MathF.Round(MathHelper.Clamp(vectorX * maxPos, minNeg, maxPos)) & mask);
var word3 = (ulong)((int)MathF.Round(MathHelper.Clamp(vectorY * maxPos, minNeg, maxPos)) & mask) << 0x10;
var word2 = (ulong)((int)MathF.Round(MathHelper.Clamp(vectorZ * maxPos, minNeg, maxPos)) & mask) << 0x20;
var word1 = (ulong)((int)MathF.Round(MathHelper.Clamp(vectorW * maxPos, minNeg, maxPos)) & mask) << 0x30;

return (word4 | word3 | word2 | word1);
}
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Graphics/PackedVector/Rg32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public override int GetHashCode()
private static uint Pack(float x, float y)
{
return (uint) (
(((int) Math.Round(MathHelper.Clamp(x, 0, 1) * 65535.0f) & 0xFFFF) ) |
(((int) Math.Round(MathHelper.Clamp(y, 0, 1) * 65535.0f) & 0xFFFF) << 16)
(((int) MathF.Round(MathHelper.Clamp(x, 0, 1) * 65535.0f) & 0xFFFF) ) |
(((int) MathF.Round(MathHelper.Clamp(y, 0, 1) * 65535.0f) & 0xFFFF) << 16)
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/Rgba1010102.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ public override int GetHashCode()
private static uint Pack(float x, float y, float z, float w)
{
return (uint) (
(((int) Math.Round(MathHelper.Clamp(x, 0, 1) * 1023.0f) & 0x03FF) << 0) |
(((int) Math.Round(MathHelper.Clamp(y, 0, 1) * 1023.0f) & 0x03FF) << 10) |
(((int) Math.Round(MathHelper.Clamp(z, 0, 1) * 1023.0f) & 0x03FF) << 20) |
(((int) Math.Round(MathHelper.Clamp(w, 0, 1) * 3.0f) & 0x03) << 30)
(((int) MathF.Round(MathHelper.Clamp(x, 0, 1) * 1023.0f) & 0x03FF) << 0) |
(((int) MathF.Round(MathHelper.Clamp(y, 0, 1) * 1023.0f) & 0x03FF) << 10) |
(((int) MathF.Round(MathHelper.Clamp(z, 0, 1) * 1023.0f) & 0x03FF) << 20) |
(((int) MathF.Round(MathHelper.Clamp(w, 0, 1) * 3.0f) & 0x03) << 30)
);
}
}
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/Rgba64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public override int GetHashCode()
private static ulong Pack(float x, float y, float z, float w)
{
return (ulong) (
(((ulong)Math.Round(MathHelper.Clamp(x * 0xFFFF, 0, 65535f)) ) ) |
(((ulong)Math.Round(MathHelper.Clamp(y * 0xFFFF, 0, 65535f)) ) << 16) |
(((ulong)Math.Round(MathHelper.Clamp(z * 0xFFFF, 0, 65535f)) ) << 32) |
(((ulong)Math.Round(MathHelper.Clamp(w * 0xFFFF, 0, 65535f)) ) << 48)
(((ulong)MathF.Round(MathHelper.Clamp(x * 0xFFFF, 0, 65535f)) ) ) |
(((ulong)MathF.Round(MathHelper.Clamp(y * 0xFFFF, 0, 65535f)) ) << 16) |
(((ulong)MathF.Round(MathHelper.Clamp(z * 0xFFFF, 0, 65535f)) ) << 32) |
(((ulong)MathF.Round(MathHelper.Clamp(w * 0xFFFF, 0, 65535f)) ) << 48)
);
}
}
Expand Down
4 changes: 2 additions & 2 deletions MonoGame.Framework/Graphics/PackedVector/Short2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ private static uint PackInTwo (float vectorX, float vectorY)
const float minNeg = ~(int)maxPos; // two's complement

// clamp the value between min and max values
var word2 = ((uint) Math.Round(MathHelper.Clamp(vectorX, minNeg, maxPos)) & 0xFFFF);
var word1 = (((uint) Math.Round(MathHelper.Clamp(vectorY, minNeg, maxPos)) & 0xFFFF) << 0x10);
var word2 = ((uint) MathF.Round(MathHelper.Clamp(vectorX, minNeg, maxPos)) & 0xFFFF);
var word1 = (((uint) MathF.Round(MathHelper.Clamp(vectorY, minNeg, maxPos)) & 0xFFFF) << 0x10);

return (word2 | word1);
}
Expand Down
8 changes: 4 additions & 4 deletions MonoGame.Framework/Graphics/PackedVector/Short4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ static ulong Pack(ref Vector4 vector)
const float minNeg = ~(int)maxPos; // two's complement

// clamp the value between min and max values
var word4 = ((ulong)((int) Math.Round(MathHelper.Clamp(vector.X, minNeg, maxPos))) & mask);
var word3 = ((ulong)((int) Math.Round(MathHelper.Clamp(vector.Y, minNeg, maxPos)) & mask)) << 0x10;
var word2 = ((ulong)((int) Math.Round(MathHelper.Clamp(vector.Z, minNeg, maxPos)) & mask)) << 0x20;
var word1 = ((ulong)((int) Math.Round(MathHelper.Clamp(vector.W, minNeg, maxPos)) & mask)) << 0x30;
var word4 = ((ulong)((int) MathF.Round(MathHelper.Clamp(vector.X, minNeg, maxPos))) & mask);
var word3 = ((ulong)((int) MathF.Round(MathHelper.Clamp(vector.Y, minNeg, maxPos)) & mask)) << 0x10;
var word2 = ((ulong)((int) MathF.Round(MathHelper.Clamp(vector.Z, minNeg, maxPos)) & mask)) << 0x20;
var word1 = ((ulong)((int) MathF.Round(MathHelper.Clamp(vector.W, minNeg, maxPos)) & mask)) << 0x30;

return word4 | word3 | word2 | word1;
}
Expand Down
16 changes: 8 additions & 8 deletions MonoGame.Framework/Graphics/SpriteBatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ public void Draw (Texture2D texture,
-origin.Y,
w,
h,
(float)Math.Sin(rotation),
(float)Math.Cos(rotation),
MathF.Sin(rotation),
MathF.Cos(rotation),
color,
_texCoordTL,
_texCoordBR,
Expand Down Expand Up @@ -398,8 +398,8 @@ public void Draw (Texture2D texture,
-origin.Y,
destinationRectangle.Width,
destinationRectangle.Height,
(float)Math.Sin(rotation),
(float)Math.Cos(rotation),
MathF.Sin(rotation),
MathF.Cos(rotation),
color,
_texCoordTL,
_texCoordBR,
Expand Down Expand Up @@ -734,8 +734,8 @@ public unsafe void DrawString (
}
else
{
cos = (float)Math.Cos(rotation);
sin = (float)Math.Sin(rotation);
cos = MathF.Cos(rotation);
sin = MathF.Sin(rotation);
transformation.M11 = (flippedHorz ? -scale.X : scale.X) * cos;
transformation.M12 = (flippedHorz ? -scale.X : scale.X) * sin;
transformation.M21 = (flippedVert ? -scale.Y : scale.Y) * (-sin);
Expand Down Expand Up @@ -1016,8 +1016,8 @@ public unsafe void DrawString (
}
else
{
cos = (float)Math.Cos(rotation);
sin = (float)Math.Sin(rotation);
cos = MathF.Cos(rotation);
sin = MathF.Sin(rotation);
transformation.M11 = (flippedHorz ? -scale.X : scale.X) * cos;
transformation.M12 = (flippedHorz ? -scale.X : scale.X) * sin;
transformation.M21 = (flippedVert ? -scale.Y : scale.Y) * (-sin);
Expand Down
69 changes: 69 additions & 0 deletions MonoGame.Framework/MathF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// MonoGame - Copyright (C) The MonoGame Team
// This file is subject to the terms and conditions defined in
// file 'LICENSE.txt', which is part of this source code package.

namespace System
{
#if (!NETCOREAPP && !NETSTANDARD2_1) || NETCOREAPP1_0 || NETCOREAPP1_1
internal static class MathF
{
public const float E = (float)Math.E;
public const float PI = (float)Math.PI;

public static float Sqrt(float f)
{
return (float)Math.Sqrt(f);
}

public static float Pow(float x, float y)
{
return (float)Math.Pow(x, y);
}

public static float Sin(float f)
{
return (float)Math.Sin(f);
}

public static float Cos(float f)
{
return (float)Math.Cos(f);
}

public static float Tan(float f)
{
return (float)Math.Tan(f);
}

public static float Asin(float f)
{
return (float)Math.Asin(f);
}

public static float Acos(float f)
{
return (float)Math.Acos(f);
}

public static float Atan(float f)
{
return (float)Math.Atan(f);
}

public static float Round(float f)
{
return (float)Math.Round(f);
}

public static float Ceiling(float f)
{
return (float)Math.Ceiling(f);
}

public static float Floor(float f)
{
return (float)Math.Floor(f);
}
}
#endif
}
4 changes: 2 additions & 2 deletions MonoGame.Framework/MathHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class MathHelper
/// <summary>
/// Represents the mathematical constant e(2.71828175).
/// </summary>
public const float E = (float)Math.E;
public const float E = MathF.E;

/// <summary>
/// Represents the log base ten of e(0.4342945).
Expand All @@ -29,7 +29,7 @@ public static class MathHelper
/// <summary>
/// Represents the value of pi(3.14159274).
/// </summary>
public const float Pi = (float)Math.PI;
public const float Pi = MathF.PI;

/// <summary>
/// Represents the value of pi divided by two(1.57079637).
Expand Down
Loading

0 comments on commit 803f70a

Please sign in to comment.