Table of Contents

Class MathExtensions

Namespace
PAC.Maths

Defines some mathematical functions.

public static class MathExtensions
Inheritance
MathExtensions
Inherited Members

Methods

CeilToMultipleOf(float, int)

Returns the least multiple of multipleOf that is >= toRound.

public static int CeilToMultipleOf(this float toRound, int multipleOf)

Parameters

toRound float
multipleOf int

Returns

int

Exceptions

ArgumentException

multipleOf is 0.

CeilToMultipleOf(float, float)

Returns the least multiple of multipleOf that is >= toRound.

public static float CeilToMultipleOf(this float toRound, float multipleOf)

Parameters

toRound float
multipleOf float

Returns

float

Exceptions

ArgumentException

multipleOf is 0.

ClampNonNegative(int)

Clamps the value so it's non-negative.

public static int ClampNonNegative(this int x)

Parameters

x int

Returns

int
  • x if x >= 0
  • 0 if x < 0

ClampNonNegative(long)

Clamps the value so it's non-negative.

public static long ClampNonNegative(this long x)

Parameters

x long

Returns

long
  • x if x >= 0
  • 0 if x < 0

ClampNonNegative(float)

Clamps the value so it's non-negative.

public static float ClampNonNegative(this float x)

Parameters

x float

Returns

float
  • x if x >= 0
  • 0 if x < 0

ClampNonPositive(int)

Clamps the value so it's non-positive.

public static int ClampNonPositive(this int x)

Parameters

x int

Returns

int
  • x if x <= 0
  • 0 if x > 0

ClampNonPositive(float)

Clamps the value so it's non-positive.

public static float ClampNonPositive(this float x)

Parameters

x float

Returns

float
  • x if x <= 0
  • 0 if x > 0

FloorToMultipleOf(float, int)

Returns the greatest multiple of multipleOf that is <= toRound.

public static int FloorToMultipleOf(this float toRound, int multipleOf)

Parameters

toRound float
multipleOf int

Returns

int

Exceptions

ArgumentException

multipleOf is 0.

FloorToMultipleOf(float, float)

Returns the greatest multiple of multipleOf that is <= toRound.

public static float FloorToMultipleOf(this float toRound, float multipleOf)

Parameters

toRound float
multipleOf float

Returns

float

Exceptions

ArgumentException

multipleOf is 0.

Gcd(int, int)

Computes the greatest non-negative common divisor of a and b.

public static int Gcd(this int a, int b)

Parameters

a int
b int

Returns

int

Exceptions

ArgumentOutOfRangeException

a or b are MinValue.

Gcd(long, long)

Computes the greatest non-negative common divisor of a and b.

public static long Gcd(this long a, long b)

Parameters

a long
b long

Returns

long

Exceptions

ArgumentOutOfRangeException

a or b are MinValue.

Max(params int[])

Returns the maximum element.

public static int Max(params int[] elements)

Parameters

elements int[]

Returns

int

Exceptions

ArgumentException

elements is empty.

Min(params int[])

Returns the minimum element.

public static int Min(params int[] elements)

Parameters

elements int[]

Returns

int

Exceptions

ArgumentException

elements is empty.

Mod(int, int)

Returns a mod b, giving a result in the range [0, abs(b)).

public static int Mod(this int a, int b)

Parameters

a int
b int

Returns

int

Exceptions

DivideByZeroException

b is 0.

Mod(long, long)

Returns a mod b, giving a result in the range [0, abs(b)).

public static long Mod(this long a, long b)

Parameters

a long
b long

Returns

long

Exceptions

DivideByZeroException

b is 0.

Mod(float, float)

Returns a mod b, giving a result in the range [0, abs(b)).

public static float Mod(this float a, float b)

Parameters

a float
b float

Returns

float

Exceptions

DivideByZeroException

b is 0.

Pow(int, int)

Computes n ^ exponent (n raised to the exponent).

public static int Pow(this int n, int exponent)

Parameters

n int
exponent int

Returns

int

Remarks

0^0 is defined to be 1.

Exceptions

ArgumentOutOfRangeException

exponent is negative.

Round(float, int)

Rounds toRound to decimalPlaces decimal places.

public static float Round(this float toRound, int decimalPlaces)

Parameters

toRound float
decimalPlaces int

Returns

float

Exceptions

ArgumentOutOfRangeException

decimalPlaces is negative.

RoundAwayFrom(float, int)

Rounds toRound to an integer, choosing to round up or down so that it becomes further (or unchanged) away from awayFrom.

public static float RoundAwayFrom(this float toRound, int awayFrom)

Parameters

toRound float
awayFrom int

Returns

float
  • ceil(toRound) if toRound >= awayFrom
  • floor(toRound) if toRound < awayFrom

RoundToIntAwayFrom(float, int)

Rounds toRound to an integer, choosing to round up or down so that it becomes further (or unchanged) away from awayFrom.

public static int RoundToIntAwayFrom(this float toRound, int awayFrom)

Parameters

toRound float
awayFrom int

Returns

int
  • ceil(toRound) if toRound >= awayFrom
  • floor(toRound) if toRound < awayFrom

RoundToIntTowards(float, int)

Rounds toRound to an integer, choosing to round up or down so that it becomes closer (or unchanged) to towards.

public static int RoundToIntTowards(this float toRound, int towards)

Parameters

toRound float
towards int

Returns

int
  • floor(toRound) if toRound >= towards
  • ceil(toRound) if toRound < towards

RoundToMultipleOf(float, int)

Returns the multiple of multipleOf that is closest to toRound.

public static int RoundToMultipleOf(this float toRound, int multipleOf)

Parameters

toRound float
multipleOf int

Returns

int

Exceptions

ArgumentException

multipleOf is 0.

RoundToMultipleOf(float, float)

Returns the multiple of multipleOf that is closest to toRound.

public static float RoundToMultipleOf(this float toRound, float multipleOf)

Parameters

toRound float
multipleOf float

Returns

float

Exceptions

ArgumentException

multipleOf is 0.

RoundTowards(float, int)

Rounds toRound to an integer, choosing to round up or down so that it becomes closer (or unchanged) to towards.

public static float RoundTowards(this float toRound, int towards)

Parameters

toRound float
towards int

Returns

float
  • floor(toRound) if toRound >= towards
  • ceil(toRound) if toRound < towards

Sign(float)

Returns the sign of the given value.

public static float Sign(this float x)

Parameters

x float

Returns

float
  • 1 if x > 0
  • 0 if x = 0
  • -1 if x < 0

Remarks

This is different from Unity's Mathf.Sign(float) since this method defines the sign of 0 to be 0, whereas Mathf.Sign(float) defines it to be 1.

Square(int)

Computes n * n.

public static float Square(this int n)

Parameters

n int

Returns

float

Square(float)

Computes n * n.

public static float Square(this float n)

Parameters

n float

Returns

float