Skip to content

Mathf Extensions Utility

Andrzej Kebab edited this page Feb 4, 2024 · 2 revisions

UtilityLibrary.Unity.Runtime

The MathfExtensions class in the UtilityLibrary.Unity.Runtime namespace provides extension methods for finding the minimum and maximum values for various numeric types. The class includes methods for byte, short, long, half (if ENABLED_UNITY_MATHEMATICS is defined), decimal, and double. These methods allow finding the minimum and maximum values among multiple values or between two values.

Methods

Min

  • Min(byte a, byte b): Returns the smaller of two byte values.
  • Min(params byte[] values): Returns the smallest value among an array of byte values.
  • Min(short a, short b): Returns the smaller of two short values.
  • Min(params short[] values): Returns the smallest value among an array of short values.
  • Min(long a, long b): Returns the smaller of two long values.
  • Min(params long[] values): Returns the smallest value among an array of long values.
  • Min(half a, half b): Returns the smaller of two half values (if ENABLED_UNITY_MATHEMATICS is defined).
  • Min(params half[] values): Returns the smallest value among an array of half values (if ENABLED_UNITY_MATHEMATICS is defined).
  • Min(decimal a, decimal b): Returns the smaller of two decimal values.
  • Min(params decimal[] values): Returns the smallest value among an array of decimal values.
  • Min(double a, double b): Returns the smaller of two double values.
  • Min(params double[] values): Returns the smallest value among an array of double values.

Max

  • Max(byte a, byte b): Returns the larger of two byte values.
  • Max(params byte[] values): Returns the largest value among an array of byte values.
  • Max(short a, short b): Returns the larger of two short values.
  • Max(params short[] values): Returns the largest value among an array of short values.
  • Max(long a, long b): Returns the larger of two long values.
  • Max(params long[] values): Returns the largest value among an array of long values.
  • Max(half a, half b): Returns the larger of two half values (if ENABLED_UNITY_MATHEMATICS is defined).
  • Max(params half[] values): Returns the largest value among an array of half values (if ENABLED_UNITY_MATHEMATICS is defined).
  • Max(decimal a, decimal b): Returns the larger of two decimal values.
  • Max(params decimal[] values): Returns the largest value among an array of decimal values.
  • Max(double a, double b): Returns the larger of two double values.
  • Max(params double[] values): Returns the largest value among an array of double values.

Usage Example

using UtilityLibrary.Unity.Runtime;

public class MathfExtensionsExample : MonoBehaviour
{
    void Start()
    {
        byte minValue = MathfExtensions.Min((byte)5, (byte)10, (byte)3);
        byte maxValue = MathfExtensions.Max((byte)5, (byte)10, (byte)3);

        short minValueShort = MathfExtensions.Min((short)5, (short)10, (short)3);
        short maxValueShort = MathfExtensions.Max((short)5, (short)10, (short)3);

        long minValueLong = MathfExtensions.Min(5L, 10L, 3L);
        long maxValueLong = MathfExtensions.Max(5L, 10L, 3L);

        // etc...
    }
}

This utility class simplifies finding the minimum and maximum values for various numeric types, providing concise and readable code.

Clone this wiki locally