mirror of
https://github.com/godotengine/godot.git
synced 2024-12-03 01:22:21 +00:00
C#: Add float an double overloads to Mathf
- Add `float` and `double` overloads to all methods of `Mathf`. This allows the methods to be usable with `float`, `double` or `real_t`. - Use `System.MathF` in the `float` overloads which may result in better performance. - Constants remain as `real_t` for the time being. - Add aggresive inlining for methods that wrap `System.Math` calls.
This commit is contained in:
parent
4fa6edc888
commit
8b07f95ba0
File diff suppressed because it is too large
Load Diff
@ -1,4 +1,8 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
// This file contains extra members for the Mathf class that aren't part of Godot's Core API.
|
||||
// Math API that is also part of Core should go into Mathf.cs.
|
||||
|
||||
namespace Godot
|
||||
{
|
||||
@ -16,14 +20,18 @@ namespace Godot
|
||||
/// </summary>
|
||||
public const real_t Sqrt2 = (real_t)1.4142135623730950488016887242M; // 1.4142136f and 1.414213562373095
|
||||
|
||||
// Epsilon size should depend on the precision used.
|
||||
private const float _epsilonF = 1e-06f;
|
||||
private const double _epsilonD = 1e-14;
|
||||
|
||||
/// <summary>
|
||||
/// A very small number used for float comparison with error tolerance.
|
||||
/// 1e-06 with single-precision floats, but 1e-14 if <c>REAL_T_IS_DOUBLE</c>.
|
||||
/// </summary>
|
||||
#if REAL_T_IS_DOUBLE
|
||||
public const real_t Epsilon = 1e-14; // Epsilon size should depend on the precision used.
|
||||
public const real_t Epsilon = _epsilonD;
|
||||
#else
|
||||
public const real_t Epsilon = 1e-06f;
|
||||
public const real_t Epsilon = _epsilonF;
|
||||
#endif
|
||||
|
||||
/// <summary>
|
||||
@ -31,7 +39,8 @@ namespace Godot
|
||||
/// </summary>
|
||||
/// <param name="s">The input value.</param>
|
||||
/// <returns>The amount of digits.</returns>
|
||||
public static int DecimalCount(real_t s)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int DecimalCount(double s)
|
||||
{
|
||||
return DecimalCount((decimal)s);
|
||||
}
|
||||
@ -41,6 +50,7 @@ namespace Godot
|
||||
/// </summary>
|
||||
/// <param name="s">The input <see langword="decimal"/> value.</param>
|
||||
/// <returns>The amount of digits.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int DecimalCount(decimal s)
|
||||
{
|
||||
return BitConverter.GetBytes(decimal.GetBits(s)[3])[2];
|
||||
@ -49,11 +59,25 @@ namespace Godot
|
||||
/// <summary>
|
||||
/// Rounds <paramref name="s"/> upward (towards positive infinity).
|
||||
///
|
||||
/// This is the same as <see cref="Ceil(real_t)"/>, but returns an <see langword="int"/>.
|
||||
/// This is the same as <see cref="Ceil(float)"/>, but returns an <see langword="int"/>.
|
||||
/// </summary>
|
||||
/// <param name="s">The number to ceil.</param>
|
||||
/// <returns>The smallest whole number that is not less than <paramref name="s"/>.</returns>
|
||||
public static int CeilToInt(real_t s)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CeilToInt(float s)
|
||||
{
|
||||
return (int)MathF.Ceiling(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds <paramref name="s"/> upward (towards positive infinity).
|
||||
///
|
||||
/// This is the same as <see cref="Ceil(double)"/>, but returns an <see langword="int"/>.
|
||||
/// </summary>
|
||||
/// <param name="s">The number to ceil.</param>
|
||||
/// <returns>The smallest whole number that is not less than <paramref name="s"/>.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int CeilToInt(double s)
|
||||
{
|
||||
return (int)Math.Ceiling(s);
|
||||
}
|
||||
@ -61,11 +85,25 @@ namespace Godot
|
||||
/// <summary>
|
||||
/// Rounds <paramref name="s"/> downward (towards negative infinity).
|
||||
///
|
||||
/// This is the same as <see cref="Floor(real_t)"/>, but returns an <see langword="int"/>.
|
||||
/// This is the same as <see cref="Floor(float)"/>, but returns an <see langword="int"/>.
|
||||
/// </summary>
|
||||
/// <param name="s">The number to floor.</param>
|
||||
/// <returns>The largest whole number that is not more than <paramref name="s"/>.</returns>
|
||||
public static int FloorToInt(real_t s)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int FloorToInt(float s)
|
||||
{
|
||||
return (int)MathF.Floor(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds <paramref name="s"/> downward (towards negative infinity).
|
||||
///
|
||||
/// This is the same as <see cref="Floor(double)"/>, but returns an <see langword="int"/>.
|
||||
/// </summary>
|
||||
/// <param name="s">The number to floor.</param>
|
||||
/// <returns>The largest whole number that is not more than <paramref name="s"/>.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int FloorToInt(double s)
|
||||
{
|
||||
return (int)Math.Floor(s);
|
||||
}
|
||||
@ -73,11 +111,25 @@ namespace Godot
|
||||
/// <summary>
|
||||
/// Rounds <paramref name="s"/> to the nearest whole number.
|
||||
///
|
||||
/// This is the same as <see cref="Round(real_t)"/>, but returns an <see langword="int"/>.
|
||||
/// This is the same as <see cref="Round(float)"/>, but returns an <see langword="int"/>.
|
||||
/// </summary>
|
||||
/// <param name="s">The number to round.</param>
|
||||
/// <returns>The rounded number.</returns>
|
||||
public static int RoundToInt(real_t s)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int RoundToInt(float s)
|
||||
{
|
||||
return (int)MathF.Round(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rounds <paramref name="s"/> to the nearest whole number.
|
||||
///
|
||||
/// This is the same as <see cref="Round(double)"/>, but returns an <see langword="int"/>.
|
||||
/// </summary>
|
||||
/// <param name="s">The number to round.</param>
|
||||
/// <returns>The rounded number.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static int RoundToInt(double s)
|
||||
{
|
||||
return (int)Math.Round(s);
|
||||
}
|
||||
@ -87,23 +139,32 @@ namespace Godot
|
||||
/// </summary>
|
||||
/// <param name="s">The angle in radians.</param>
|
||||
/// <returns>The sine and cosine of that angle.</returns>
|
||||
public static (real_t Sin, real_t Cos) SinCos(real_t s)
|
||||
public static (float Sin, float Cos) SinCos(float s)
|
||||
{
|
||||
(double sin, double cos) = Math.SinCos(s);
|
||||
return ((real_t)sin, (real_t)cos);
|
||||
return MathF.SinCos(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the sine and cosine of angle <paramref name="s"/> in radians.
|
||||
/// </summary>
|
||||
/// <param name="s">The angle in radians.</param>
|
||||
/// <returns>The sine and cosine of that angle.</returns>
|
||||
public static (double Sin, double Cos) SinCos(double s)
|
||||
{
|
||||
return Math.SinCos(s);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see langword="true"/> if <paramref name="a"/> and <paramref name="b"/> are approximately
|
||||
/// equal to each other.
|
||||
/// The comparison is done using the provided tolerance value.
|
||||
/// If you want the tolerance to be calculated for you, use <see cref="IsEqualApprox(real_t, real_t)"/>.
|
||||
/// If you want the tolerance to be calculated for you, use <see cref="IsEqualApprox(float, float)"/>.
|
||||
/// </summary>
|
||||
/// <param name="a">One of the values.</param>
|
||||
/// <param name="b">The other value.</param>
|
||||
/// <param name="tolerance">The pre-calculated tolerance value.</param>
|
||||
/// <returns>A <see langword="bool"/> for whether or not the two values are equal.</returns>
|
||||
public static bool IsEqualApprox(real_t a, real_t b, real_t tolerance)
|
||||
public static bool IsEqualApprox(float a, float b, float tolerance)
|
||||
{
|
||||
// Check for exact equality first, required to handle "infinity" values.
|
||||
if (a == b)
|
||||
@ -111,7 +172,28 @@ namespace Godot
|
||||
return true;
|
||||
}
|
||||
// Then check for approximate equality.
|
||||
return Abs(a - b) < tolerance;
|
||||
return Math.Abs(a - b) < tolerance;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns <see langword="true"/> if <paramref name="a"/> and <paramref name="b"/> are approximately
|
||||
/// equal to each other.
|
||||
/// The comparison is done using the provided tolerance value.
|
||||
/// If you want the tolerance to be calculated for you, use <see cref="IsEqualApprox(double, double)"/>.
|
||||
/// </summary>
|
||||
/// <param name="a">One of the values.</param>
|
||||
/// <param name="b">The other value.</param>
|
||||
/// <param name="tolerance">The pre-calculated tolerance value.</param>
|
||||
/// <returns>A <see langword="bool"/> for whether or not the two values are equal.</returns>
|
||||
public static bool IsEqualApprox(double a, double b, double tolerance)
|
||||
{
|
||||
// Check for exact equality first, required to handle "infinity" values.
|
||||
if (a == b)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
// Then check for approximate equality.
|
||||
return Math.Abs(a - b) < tolerance;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user