Version: Unity 6.5 Alpha (6000.5)
LanguageEnglish
  • C#

math

class in Unity.Mathematics

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Description

A static class to contain various math functions and constants.

Static Properties

Property Description
DBL_MIN_NORMALThe smallest positive normal number representable in a double. This is a f64/double precision constant.
EThe mathematical constant e also known as Euler's number. Approximately 2.72.
E_DBLThe mathematical constant e also known as Euler's number. Approximately 2.72. This is a f64/double precision constant.
EPSILON The difference between 1.0f and the next representable f32/single precision number. Beware: This value is different from System.Single.Epsilon, which is the smallest, positive, denormalized f32/single.
EPSILON_DBL The difference between 1.0 and the next representable f64/double precision number. Beware: This value is different from System.Double.Epsilon, which is the smallest, positive, denormalized f64/double.
FLT_MIN_NORMALThe smallest positive normal number representable in a float.
INFINITY Single precision constant for positive infinity.
INFINITY_DBL Double precision constant for positive infinity.
LN10The natural logarithm of 10. Approximately 2.30.
LN10_DBLThe natural logarithm of 10. Approximately 2.30. This is a f64/double precision constant.
LN2The natural logarithm of 2. Approximately 0.69.
LN2_DBLThe natural logarithm of 2. Approximately 0.69. This is a f64/double precision constant.
LOG10EThe base 10 logarithm of e. Approximately 0.43.
LOG10E_DBLThe base 10 logarithm of e. Approximately 0.43. This is a f64/double precision constant.
LOG2EThe base 2 logarithm of e. Approximately 1.44.
LOG2E_DBLThe base 2 logarithm of e. Approximately 1.44. This is a f64/double precision constant.
NAN Single precision constant for Not a Number. NAN is considered unordered, which means all comparisons involving it are false except for not equal (operator !=). As a consequence, NAN == NAN is false but NAN != NAN is true. Additionally, there are multiple bit representations for Not a Number, so if you must test if your value is NAN, use isnan().
NAN_DBL Double precision constant for Not a Number. NAN_DBL is considered unordered, which means all comparisons involving it are false except for not equal (operator !=). As a consequence, NAN_DBL == NAN_DBL is false but NAN_DBL != NAN_DBL is true. Additionally, there are multiple bit representations for Not a Number, so if you must test if your value is NAN_DBL, use isnan().
PIThe mathematical constant pi. Approximately 3.14.
PI_DBLThe mathematical constant pi. Approximately 3.14. This is a f64/double precision constant.
PI2 The mathematical constant (2 * pi). Approximately 6.28. Also known as math.TAU.
PI2_DBL The mathematical constant (2 * pi). Approximately 6.28. This is a f64/double precision constant. Also known as math.TAU_DBL.
PIHALF The mathematical constant (pi / 2). Approximately 1.57.
PIHALF_DBL The mathematical constant (pi / 2). Approximately 1.57. This is a f64/double precision constant.
SQRT2The square root 2. Approximately 1.41.
SQRT2_DBLThe square root 2. Approximately 1.41. This is a f64/double precision constant.
TAU The mathematical constant tau. Approximately 6.28. Also known as math.PI2.
TAU_DBL The mathematical constant tau. Approximately 6.28. This is a f64/double precision constant. Also known as math.PI2_DBL.
TODEGREES The conversion constant used to convert radians to degrees. Multiply the radian value by this constant to get degrees.
TODEGREES_DBL The conversion constant used to convert radians to degrees. Multiply the radian value by this constant to get degrees.
TORADIANS The conversion constant used to convert degrees to radians. Multiply the degree value by this constant to get radians.
TORADIANS_DBL The conversion constant used to convert degrees to radians. Multiply the degree value by this constant to get radians.

Static Methods

Method Description
absReturns the absolute value of a int value.
acosReturns the arccosine of a float value.
AffineTransformReturns an AffineTransform constructed from a translation represented by a float3 vector and rotation represented by a unit quaternion.
allReturns true if all components of the input bool2 vector are true, false otherwise.
angleReturns the angle in radians between two unit quaternions.
anyReturns true if any component of the input bool2 vector is true, false otherwise.
asdoubleReturns the bit pattern of a long as a double.
asfloatReturns the bit pattern of an int as a float.
asinReturns the arcsine of a float value.
asintReturns the bit pattern of a uint as an int.
aslongReturns the bit pattern of a ulong as a long.
asuintReturns the bit pattern of an int as a uint.
asulongReturns the bit pattern of a long as a ulong.
atanReturns the arctangent of a float value.
atan2Returns the 2-argument arctangent of a pair of float values.
back Unity's back axis (0, 0, -1).
bitmask Returns a bitmask representation of a bool4. Storing one 1 bit per component in LSB order, from lower to higher bits (so 4 bits in total). The component x is stored at bit 0, The component y is stored at bit 1, The component z is stored at bit 2, The component w is stored at bit 3 The bool4(x = true, y = true, z = false, w = true) would produce the value 1011 = 0xB
bool2Returns a bool2 vector constructed from two bool values.
bool2x2Returns a bool2x2 matrix constructed from two bool2 vectors.
bool2x3Returns a bool2x3 matrix constructed from three bool2 vectors.
bool2x4Returns a bool2x4 matrix constructed from four bool2 vectors.
bool3Returns a bool3 vector constructed from three bool values.
bool3x2Returns a bool3x2 matrix constructed from two bool3 vectors.
bool3x3Returns a bool3x3 matrix constructed from three bool3 vectors.
bool3x4Returns a bool3x4 matrix constructed from four bool3 vectors.
bool4Returns a bool4 vector constructed from four bool values.
bool4x2Returns a bool4x2 matrix constructed from two bool4 vectors.
bool4x3Returns a bool4x3 matrix constructed from three bool4 vectors.
bool4x4Returns a bool4x4 matrix constructed from four bool4 vectors.
ceilReturns the result of rounding a float value up to the nearest integral value greater or equal to the original value.
ceillog2 Computes the ceiling of the base-2 logarithm of x.
ceilpow2Returns the smallest power of two greater than or equal to the input.
chgsignChange the sign of x based on the most significant bit of y [msb(y) ? -x : x].
clampReturns the result of clamping the value valueToClamp into the interval (inclusive) [lowerBound, upperBound], where valueToClamp, lowerBound and upperBound are int values.
cmaxReturns the maximum component of an int2 vector.
cminReturns the minimum component of an int2 vector.
compress Packs components with an enabled mask to the left.
conjugateReturns the conjugate of a quaternion value.
cosReturns the cosine of a float value.
coshReturns the hyperbolic cosine of a float value.
countbitsReturns number of 1-bits in the binary representation of an int value. Also known as the Hamming weight, popcnt on x86, and vcnt on ARM.
crossReturns the cross product of two float3 vectors.
csumReturns the horizontal sum of components of an int2 vector.
decomposeDecomposes the AffineTransform in translation, rotation and scale.
degreesReturns the result of converting a double value from radians to degrees.
determinantReturns the determinant of a double2x2 matrix.
distanceReturns the distance between two float values.
distancesqReturns the squared distance between two float values.
dotReturns the dot product of two int values. Equivalent to multiplication.
double2Returns a double2 vector constructed from two double values.
double2x2Returns a double2x2 matrix constructed from two double2 vectors.
double2x3Returns a double2x3 matrix constructed from three double2 vectors.
double2x4Returns a double2x4 matrix constructed from four double2 vectors.
double3Returns a double3 vector constructed from three double values.
double3x2Returns a double3x2 matrix constructed from two double3 vectors.
double3x3Returns a double3x3 matrix constructed from three double3 vectors.
double3x4Returns a double3x4 matrix constructed from four double3 vectors.
double4Returns a double4 vector constructed from four double values.
double4x2Returns a double4x2 matrix constructed from two double4 vectors.
double4x3Returns a double4x3 matrix constructed from three double4 vectors.
double4x4Returns a double4x4 matrix constructed from four double4 vectors.
down Unity's down axis (0, -1, 0).
Euler Returns the Euler angle representation of the quaternion. The returned angles depend on the specified order to apply the three rotations around the principal axes. All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin. When the rotation order is known at compile time, to get the best performance you should use the specific Euler rotation constructors such as EulerZXY(...).
EulerXYZ Returns the Euler angle representation of the quaternion following the XYZ rotation order. All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
EulerXZY Returns the Euler angle representation of the quaternion following the XZY rotation order. All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
EulerYXZ Returns the Euler angle representation of the quaternion following the YXZ rotation order. All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
EulerYZX Returns the Euler angle representation of the quaternion following the YZX rotation order. All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
EulerZXY Returns the Euler angle representation of the quaternion following the ZXY rotation order. All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
EulerZYX Returns the Euler angle representation of the quaternion following the ZYX rotation order. All rotation angles are in radians and clockwise when looking along the rotation axis towards the origin.
expReturns the base-e exponential of x.
exp10Returns the base-10 exponential of x.
exp2Returns the base-2 exponential of x.
f16tof32Returns the floating point representation of a half-precision floating point value.
f32tof16Returns the result converting a float value to its nearest half-precision floating point representation.
faceforwardConditionally flips a vector n if two vectors i and ng are pointing in the same direction. Returns n if dot(i, ng) < 0, -n otherwise.
fastinverseFast matrix inverse for rigid transforms (orthonormal basis and translation)
float2Returns a float2 vector constructed from two float values.
float2x2Returns a float2x2 matrix constructed from two float2 vectors.
float2x3Returns a float2x3 matrix constructed from three float2 vectors.
float2x4Returns a float2x4 matrix constructed from four float2 vectors.
float3Returns a float3 vector constructed from three float values.
float3x2Returns a float3x2 matrix constructed from two float3 vectors.
float3x3Returns a float3x3 matrix constructed from three float3 vectors.
float3x4Returns a float3x4 matrix constructed from an AffineTransform.
float4Returns a float4 vector constructed from four float values.
float4x2Returns a float4x2 matrix constructed from two float4 vectors.
float4x3Returns a float4x3 matrix constructed from three float4 vectors.
float4x4Returns a float4x4 matrix constructed from an AffineTransform.
floorReturns the result of rounding a float value up to the nearest integral value less or equal to the original value.
floorlog2 Computes the floor of the base-2 logarithm of x.
fmodReturns the floating point remainder of x/y.
forward Unity's forward axis (0, 0, 1).
fracReturns the fractional part of a float value.
halfReturns a half value constructed from a half values.
half2Returns a half2 vector constructed from two half values.
half3Returns a half3 vector constructed from three half values.
half4Returns a half4 vector constructed from four half values.
hashReturns a uint hash code of an AffineTransform.
hashwide Returns a uint4 vector hash code of an AffineTransform. When multiple elements are to be hashes together, it can more efficient to calculate and combine wide hash that are only reduced to a narrow uint hash at the very end instead of at every step.
int2Returns a int2 vector constructed from two int values.
int2x2Returns a int2x2 matrix constructed from two int2 vectors.
int2x3Returns a int2x3 matrix constructed from three int2 vectors.
int2x4Returns a int2x4 matrix constructed from four int2 vectors.
int3Returns a int3 vector constructed from three int values.
int3x2Returns a int3x2 matrix constructed from two int3 vectors.
int3x3Returns a int3x3 matrix constructed from three int3 vectors.
int3x4Returns a int3x4 matrix constructed from four int3 vectors.
int4Returns a int4 vector constructed from four int values.
int4x2Returns a int4x2 matrix constructed from two int4 vectors.
int4x3Returns a int4x3 matrix constructed from three int4 vectors.
int4x4Returns a int4x4 matrix constructed from four int4 vectors.
inverseReturns the inverse of an AffineTransform.
isfiniteReturns true if the input float is a finite floating point value, false otherwise.
isinfReturns true if the input float is an infinite floating point value, false otherwise.
isnanReturns true if the input float is a NaN (not a number) floating point value, false otherwise.
ispow2 Checks if the input is a power of two.
left Unity's left axis (-1, 0, 0).
lengthReturns the length of a float value. Equivalent to the absolute value.
lengthsqReturns the squared length of a float value. Equivalent to squaring the value.
lerpReturns the result of linearly interpolating from start to end using the interpolation parameter t.
logReturns the natural logarithm of a float value.
log10Returns the base-10 logarithm of a float value.
log2Returns the base-2 logarithm of a float value.
lzcntReturns the componentwise number of leading zeros in the binary representations of an int vector.
madReturns the result of a multiply-add operation (a * b + c) on 3 int values.
maxReturns the maximum of two int values.
minReturns the minimum of two int values.
modfSplits a float value into an integral part i and a fractional part that gets returned. Both parts take the sign of the input.
mulReturns the result of transforming the AffineTransform b by the AffineTransform a.
mulScale Matrix columns multiplied by scale components m.c0.x " s.x | m.c1.x " s.y | m.c2.x " s.z m.c0.y " s.x | m.c1.y " s.y | m.c2.y " s.z m.c0.z " s.x | m.c1.z " s.y | m.c2.z * s.z
nlerpReturns the result of a normalized linear interpolation between two quaternions q1 and a2 using an interpolation parameter t.
normalizeReturns a normalized version of the float2 vector x by scaling it by 1 / length(x).
normalizesafe Returns a safe normalized version of the float2 vector x by scaling it by 1 / length(x). Returns the given default value when 1 / length(x) does not produce a finite number.
orthonormal_basis Generate an orthonormal basis given a single unit length normal vector.
powReturns x raised to the power y.
project Compute vector projection of a onto b.
projectsafe Compute vector projection of a onto b. If result is not finite, then return the default value instead.
quaternionReturns a quaternion constructed from four float values.
radiansReturns the result of converting a float value from degrees to radians.
rcpReturns the reciprocal a float value.
reflectGiven an incident vector i and a normal vector n, returns the reflection vector r = i - 2.0f " dot(i, n) " n.
refractReturns the refraction vector given the incident vector i, the normal vector n and the refraction index.
remapReturns the result of a non-clamping linear remapping of a value x from source range [srcStart, srcEnd] to the destination range [dstStart, dstEnd].
reversebitsReturns the result of performing a reversal of the bit pattern of an int value.
right Unity's right axis (1, 0, 0).
RigidTransformReturns a RigidTransform constructed from a rotation represented by a unit quaternion and a translation represented by a float3 vector.
rolReturns the result of rotating the bits of an int left by bits n.
rorReturns the result of rotating the bits of an int right by bits n.
rotateReturns the result of rotating a float3 vector by an AffineTransform.
rotation Extracts the rotation from a matrix.
roundReturns the result of rounding a float value to the nearest integral value.
rsqrtReturns the reciprocal square root of a float value.
saturateReturns the result of clamping the float value x into the interval [0, 1].
scaleMul Matrix rows multiplied by scale components m.c0.x " s.x | m.c1.x " s.x | m.c2.x " s.x m.c0.y " s.y | m.c1.y " s.y | m.c2.y " s.y m.c0.z " s.z | m.c1.z " s.z | m.c2.z * s.z
selectReturns trueValue if test is true, falseValue otherwise.
shuffleReturns the result of specified shuffling of the components from two bool2 vectors into a bool value.
signReturns the sign of a int value. -1 if it is less than zero, 0 if it is zero and 1 if it greater than zero.
sinReturns the sine of a float value.
sincosReturns the sine and cosine of the input float value x through the out parameters s and c.
sinhReturns the hyperbolic sine of a float value.
slerpReturns the result of a spherical interpolation between two quaternions q1 and a2 using an interpolation parameter t.
smoothstepReturns a smooth Hermite interpolation between 0.0f and 1.0f when x is in the interval (inclusive) [xMin, xMax].
sqrtReturns the square root of a float value.
square Computes the square (x * x) of the input argument x.
stepReturns the result of a step function where the result is 1.0f when x >= threshold and 0.0f otherwise.
tanReturns the tangent of a float value.
tanhReturns the hyperbolic tangent of a float value.
transformReturns the result of transforming a float3 point by an AffineTransform.
transposeReturn the bool2x2 transpose of a bool2x2 matrix.
truncReturns the result of truncating a float value to an integral float value.
tzcnt Computes the trailing zero count in the binary representation of the input value.
uint2Returns a uint2 vector constructed from two uint values.
uint2x2Returns a uint2x2 matrix constructed from two uint2 vectors.
uint2x3Returns a uint2x3 matrix constructed from three uint2 vectors.
uint2x4Returns a uint2x4 matrix constructed from four uint2 vectors.
uint3Returns a uint3 vector constructed from three uint values.
uint3x2Returns a uint3x2 matrix constructed from two uint3 vectors.
uint3x3Returns a uint3x3 matrix constructed from three uint3 vectors.
uint3x4Returns a uint3x4 matrix constructed from four uint3 vectors.
uint4Returns a uint4 vector constructed from four uint values.
uint4x2Returns a uint4x2 matrix constructed from two uint4 vectors.
uint4x3Returns a uint4x3 matrix constructed from three uint4 vectors.
uint4x4Returns a uint4x4 matrix constructed from four uint4 vectors.
unitexpReturns the natural exponent of a quaternion. Assumes w is zero.
unitlogReturns the natural logarithm of a unit length quaternion.
unlerpReturns the result of normalizing a floating point value x to a range [a, b]. The opposite of lerp. Equivalent to (x - a) / (b - a).
up Unity's up axis (0, 1, 0).