No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MathUtil.cs 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. public static class MathUtil
  6. {
  7. public static double Abs(double d)
  8. {
  9. return d > 0 ? d : -d;
  10. }
  11. public static double Clamp(double d, double min, double max)
  12. {
  13. if (d >= min && d <= max) return d;
  14. else if (d < min) return min;
  15. else return max;
  16. }
  17. public static bool Approximately(double a, double b)
  18. {
  19. return Math.Abs(b - a) < Math.Max(0.000001f * Math.Max(Math.Abs(a), Math.Abs(b)), Mathf.Epsilon * 8);
  20. }
  21. public static double Clamp01(double value)
  22. {
  23. if (value < 0F)
  24. return 0F;
  25. else if (value > 1F)
  26. return 1F;
  27. else
  28. return value;
  29. }
  30. public static double Lerp(double a, double b, double t)
  31. {
  32. return a + (b - a) * Clamp01(t);
  33. }
  34. public static bool IsInteger(double value)
  35. {
  36. return Math.Abs(value % 1) <= (Double.Epsilon * 100);
  37. }
  38. public static int GetPrecision(double value)
  39. {
  40. if (IsInteger(value)) return 0;
  41. int count = 1;
  42. double intvalue = value * Mathf.Pow(10, count);
  43. while (!IsInteger(intvalue) && count < 38)
  44. {
  45. count++;
  46. intvalue = value * Mathf.Pow(10, count);
  47. }
  48. return count;
  49. }
  50. }
  51. }