MathfEx.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. namespace Godot
  3. {
  4. public static partial class Mathf
  5. {
  6. // Define constants with Decimal precision and cast down to double or float.
  7. /// <summary>
  8. /// The natural number <c>e</c>.
  9. /// </summary>
  10. public const real_t E = (real_t)2.7182818284590452353602874714M; // 2.7182817f and 2.718281828459045
  11. /// <summary>
  12. /// The square root of 2.
  13. /// </summary>
  14. public const real_t Sqrt2 = (real_t)1.4142135623730950488016887242M; // 1.4142136f and 1.414213562373095
  15. /// <summary>
  16. /// A very small number used for float comparison with error tolerance.
  17. /// 1e-06 with single-precision floats, but 1e-14 if <c>REAL_T_IS_DOUBLE</c>.
  18. /// </summary>
  19. #if REAL_T_IS_DOUBLE
  20. public const real_t Epsilon = 1e-14; // Epsilon size should depend on the precision used.
  21. #else
  22. public const real_t Epsilon = 1e-06f;
  23. #endif
  24. /// <summary>
  25. /// Returns the amount of digits after the decimal place.
  26. /// </summary>
  27. /// <param name="s">The input value.</param>
  28. /// <returns>The amount of digits.</returns>
  29. public static int DecimalCount(real_t s)
  30. {
  31. return DecimalCount((decimal)s);
  32. }
  33. /// <summary>
  34. /// Returns the amount of digits after the decimal place.
  35. /// </summary>
  36. /// <param name="s">The input <see langword="decimal"/> value.</param>
  37. /// <returns>The amount of digits.</returns>
  38. public static int DecimalCount(decimal s)
  39. {
  40. return BitConverter.GetBytes(decimal.GetBits(s)[3])[2];
  41. }
  42. /// <summary>
  43. /// Rounds <paramref name="s"/> upward (towards positive infinity).
  44. ///
  45. /// This is the same as <see cref="Ceil(real_t)"/>, but returns an <see langword="int"/>.
  46. /// </summary>
  47. /// <param name="s">The number to ceil.</param>
  48. /// <returns>The smallest whole number that is not less than <paramref name="s"/>.</returns>
  49. public static int CeilToInt(real_t s)
  50. {
  51. return (int)Math.Ceiling(s);
  52. }
  53. /// <summary>
  54. /// Rounds <paramref name="s"/> downward (towards negative infinity).
  55. ///
  56. /// This is the same as <see cref="Floor(real_t)"/>, but returns an <see langword="int"/>.
  57. /// </summary>
  58. /// <param name="s">The number to floor.</param>
  59. /// <returns>The largest whole number that is not more than <paramref name="s"/>.</returns>
  60. public static int FloorToInt(real_t s)
  61. {
  62. return (int)Math.Floor(s);
  63. }
  64. /// <summary>
  65. /// Rounds <paramref name="s"/> to the nearest whole number.
  66. ///
  67. /// This is the same as <see cref="Round(real_t)"/>, but returns an <see langword="int"/>.
  68. /// </summary>
  69. /// <param name="s">The number to round.</param>
  70. /// <returns>The rounded number.</returns>
  71. public static int RoundToInt(real_t s)
  72. {
  73. return (int)Math.Round(s);
  74. }
  75. /// <summary>
  76. /// Returns <see langword="true"/> if <paramref name="a"/> and <paramref name="b"/> are approximately
  77. /// equal to each other.
  78. /// The comparison is done using the provided tolerance value.
  79. /// If you want the tolerance to be calculated for you, use <see cref="IsEqualApprox(real_t, real_t)"/>.
  80. /// </summary>
  81. /// <param name="a">One of the values.</param>
  82. /// <param name="b">The other value.</param>
  83. /// <param name="tolerance">The pre-calculated tolerance value.</param>
  84. /// <returns>A <see langword="bool"/> for whether or not the two values are equal.</returns>
  85. public static bool IsEqualApprox(real_t a, real_t b, real_t tolerance)
  86. {
  87. // Check for exact equality first, required to handle "infinity" values.
  88. if (a == b)
  89. {
  90. return true;
  91. }
  92. // Then check for approximate equality.
  93. return Abs(a - b) < tolerance;
  94. }
  95. }
  96. }