Class MathExtensions
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
Returns
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
Returns
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
ifx
>= 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
ifx
>= 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
ifx
>= 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
ifx
<= 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
ifx
<= 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
Returns
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
Returns
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
Returns
Exceptions
- ArgumentOutOfRangeException
a
orb
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
Returns
Exceptions
- ArgumentOutOfRangeException
a
orb
are MinValue.
Max(params int[])
Returns the maximum element.
public static int Max(params int[] elements)
Parameters
elements
int[]
Returns
Exceptions
- ArgumentException
elements
is empty.
Min(params int[])
Returns the minimum element.
public static int Min(params int[] elements)
Parameters
elements
int[]
Returns
Exceptions
- ArgumentException
elements
is empty.
Mod(int, int)
Returns
, giving a result in the range a
mod b
[0, abs(b))
.
public static int Mod(this int a, int b)
Parameters
Returns
Exceptions
- DivideByZeroException
b
is 0.
Mod(long, long)
Returns
, giving a result in the range a
mod b
[0, abs(b))
.
public static long Mod(this long a, long b)
Parameters
Returns
Exceptions
- DivideByZeroException
b
is 0.
Mod(float, float)
Returns
, giving a result in the range a
mod b
[0, abs(b))
.
public static float Mod(this float a, float b)
Parameters
Returns
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
Returns
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
Returns
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
Returns
- float
-
ceil(
iftoRound
)toRound
>=awayFrom
-
floor(
iftoRound
)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
Returns
- int
-
ceil(
iftoRound
)toRound
>=awayFrom
-
floor(
iftoRound
)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
Returns
- int
-
floor(
iftoRound
)toRound
>=towards
-
ceil(
iftoRound
)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
Returns
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
Returns
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
Returns
- float
-
floor(
iftoRound
)toRound
>=towards
-
ceil(
iftoRound
)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
-
1 if
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
Square(float)
Computes
.n
* n
public static float Square(this float n)
Parameters
n
float