Skip to main content

MathUtils

A collection of math utilities.

note

For Kotlin users, all the following except for the vararg functions are extension functions

MU.toIn

Converts a value to inches

Params
  • value: Number - The value to convert
  • from: DistanceUnit - The unit of the value
  • Returns
  • Number - The value in inches
    MU.toIn(1, DistanceUnit.METER); // 39.37007874015748

    MU.toCm

    Converts a value to centimeters

    Params
  • value: Number - The value to convert
  • from: DistanceUnit - The unit of the value
  • Returns
  • Number - The value in centimeters
    MU.toCm(1, DistanceUnit.METER); // 100.0

    MU.toRad

    Converts a value to radians

    Params
  • value: Number - The value to convert
  • from: AngleUnit - The unit of the value
  • Returns
  • Number - The value in radians
    MU.toRad(1, AngleUnit.DEGREES); // 0.017453292519943295

    MU.zeroIfNaN

    Returns 0 if the value is NaN

    Params
  • value: Double - The value to check
  • Returns
  • Double - 0 if the value is NaN, otherwise the value
  • note

    An overload exists for Floats

    MU.zeroIfNaN(Double.NaN); // 0.0

    MU.zeroIfNaN(1.0); // 1.0

    MU.isInRange

    Checks if a value is within a range

    Params
  • value: Number - The value to check
  • min: Number - The minimum value
  • max: Number - The maximum value
  • Returns
  • Boolean - True if the value is within the range, false otherwise
    MU.isInRange(1, 0, 2); // true

    MU.clamp

    Clamps a value to a range. Returns it as a Double.

    Params
  • value: Number - The value to clamp
  • min: Number - The minimum value
  • max: Number - The maximum value
  • Returns
  • Double - The clamped value
    MU.clamp(1, 0, 2); // 1.0

    MU.clamp(3, 0, 2); // 2.0

    MU.avg

    Calculates the average of a list of numbers

    Important for Java users

    This (and the other vararg) method(s) can not take in array of primitives. You must use an array of boxed types (Double, Float, Integer, etc.) if not directly using varargs.

    Params
  • xs: Number... - The values to average
  • Returns
  • Double - The average of the values
    double average = MU.avg(1, 2, 3); // 2.0 (can be used as normal varargs)

    Double[] values = new Double[] { 1.0, 2.0, 3.0 };
    double average = MU.avg(values); // 2.0 (boxed array is fine)

    double[] error = new double[] { 1.0, 2.0, 3.0 };
    double average = MU.avg(error); // Error because of primitive array

    MU.maxByMagnitude

    Returns the value with the largest magnitude (not absolute value), or 0.0 if the list is empty

    Params
  • xs: Number... - The values to check
  • Returns
  • Double - The value with the largest magnitude (or 0.0 if the list is empty)
    double max = MU.maxByMagnitude(-1, 2, -3); // 3.0

    Double[] values = new Double[] { 1.0, 2.0, 3.0 };
    double max = MU.maxByMagnitude(values); // 3.0 (boxed array is fine)

    double[] error = new double[] { 1.0, 2.0, 3.0 };
    double max = MU.maxByMagnitude(error); // Error because of primitive array

    MU.maxMagnitude

    Returns the largest magnitude (as absolute value) of a list of numbers, or 0.0 if the list is empty

    Params
  • xs: Number... - The values to check
  • Returns
  • Double - The largest magnitude (or 0.0 if the list is empty)
    double max = MU.maxMagnitude(-1, 2, -3); // 3.0

    Double[] values = new Double[] { 1.0, 2.0, 3.0 };
    double max = MU.maxMagnitude(values); // 3.0 (boxed array is fine)

    double[] error = new double[] { 1.0, 2.0, 3.0 };
    double max = MU.maxMagnitude(error); // Error because of primitive array

    MU.withDeadzone

    Returns a value with a deadzone applied. If the value is within the deadzone, it will be set to given origin (defaults to 0.0).

    Params
  • value: Number - The value to apply the deadzone to
  • deadzone: Number - The deadzone to apply
  • origin: Number - The origin to set the value to if it is within the deadzone
    - Defaults to: 0.0
  • Returns
  • Double - The value with the deadzone applied
    MU.withDeadzone(0.5, 0.1); // 0.5

    MU.withDeadzone(0.05, 0.1); // 0.0

    MU.withDeadzone(10.5, 1, 10); // 10