Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


math

A static class to contain various math functions and constants.
Read time 19 minutesLast updated 5 days ago

Definition

public static class math

Static Fields

Value

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.
EPSILONThe difference between 1.0f and the next representable f32/single precision number.
EPSILON_DBLThe difference between 1.0 and the next representable f64/double precision number.
FLT_MIN_NORMALThe smallest positive normal number representable in a float.
INFINITYSingle precision constant for positive infinity.
INFINITY_DBLDouble 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.
NANSingle precision constant for Not a Number.
NAN_DBLDouble precision constant for Not a Number.
PIThe mathematical constant pi. Approximately 3.14.
PI2The mathematical constant (2 * pi). Approximately 6.28. Also known as math.TAU.
PI2_DBLThe mathematical constant (2 * pi). Approximately 6.28. This is a f64/double precision constant. Also known as math.TAU_DBL.
PI_DBLThe mathematical constant pi. Approximately 3.14. This is a f64/double precision constant.
PIHALFThe mathematical constant (pi / 2). Approximately 1.57.
PIHALF_DBLThe 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.
TAUThe mathematical constant tau. Approximately 6.28. Also known as math.PI2.
TAU_DBLThe mathematical constant tau. Approximately 6.28. This is a f64/double precision constant. Also known as math.PI2_DBL.
TODEGREESThe conversion constant used to convert radians to degrees. Multiply the radian value by this constant to get degrees.
TODEGREES_DBLThe conversion constant used to convert radians to degrees. Multiply the radian value by this constant to get degrees.
TORADIANSThe conversion constant used to convert degrees to radians. Multiply the degree value by this constant to get radians.
TORADIANS_DBLThe 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 double value.
acosReturns the arccosine of a double value.
AffineTransformReturns an AffineTransform constructed from a translation represented by float3 vector and a float3x3 matrix representing both rotation and scale.
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 double value.
asintReturns the bit pattern of a float as an int.
aslongReturns the bit pattern of a double as a long.
asuintReturns the bit pattern of an int as a uint.
asulongReturns the bit pattern of a double as a ulong.
atanReturns the arctangent of a double value.
atan2Returns the 2-argument arctangent of a pair of double values.
backUnity's back axis (0, 0, -1).
bitmaskReturns 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 a single bool value by assigning it to every component.
bool2x2Returns a bool2x2 matrix constructed from a single bool value by assigning it to every component.
bool2x3Returns a bool2x3 matrix constructed from a single bool value by assigning it to every component.
bool2x4Returns a bool2x4 matrix constructed from a single bool value by assigning it to every component.
bool3Returns a bool3 vector constructed from a single bool value by assigning it to every component.
bool3x2Returns a bool3x2 matrix constructed from a single bool value by assigning it to every component.
bool3x3Returns a bool3x3 matrix constructed from a single bool value by assigning it to every component.
bool3x4Returns a bool3x4 matrix constructed from a single bool value by assigning it to every component.
bool4Returns a bool4 vector constructed from a single bool value by assigning it to every component.
bool4x2Returns a bool4x2 matrix constructed from a single bool value by assigning it to every component.
bool4x3Returns a bool4x3 matrix constructed from a single bool value by assigning it to every component.
bool4x4Returns a bool4x4 matrix constructed from a single bool value by assigning it to every component.
ceilReturns the result of rounding a double value up to the nearest greater integral value greater or equal to the original value.
ceillog2Computes 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 double values.
cmaxReturns the maximum component of a double2 vector.
cminReturns the minimum component of a double2 vector.
compressPacks components with an enabled mask to the left.
conjugateReturns the conjugate of a quaternion value.
cosReturns the cosine of a double value.
coshReturns the hyperbolic cosine of a double 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 double3 vectors.
csumReturns the horizontal sum of components of a double2 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 double values.
distancesqReturns the squared distance between two double values.
dotReturns the dot product of two double values. Equivalent to multiplication.
double2Returns a double2 vector constructed from a single bool value by converting it to double and assigning it to every component.
double2x2Returns a double2x2 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double2x3Returns a double2x3 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double2x4Returns a double2x4 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double3Returns a double3 vector constructed from a single bool value by converting it to double and assigning it to every component.
double3x2Returns a double3x2 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double3x3Returns a double3x3 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double3x4Returns a double3x4 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double4Returns a double4 vector constructed from a single bool value by converting it to double and assigning it to every component.
double4x2Returns a double4x2 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double4x3Returns a double4x3 matrix constructed from a single bool value by converting it to double and assigning it to every component.
double4x4Returns a double4x4 matrix constructed from a single bool value by converting it to double and assigning it to every component.
downUnity's down axis (0, -1, 0).
EulerReturns 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(...).
EulerXYZReturns 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.
EulerXZYReturns 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.
EulerYXZReturns 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.
EulerYZXReturns 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.
EulerZXYReturns 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.
EulerZYXReturns 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 a single bool value by converting it to float and assigning it to every component.
float2x2Returns a float2x2 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float2x3Returns a float2x3 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float2x4Returns a float2x4 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float3Returns a float3 vector constructed from a single bool value by converting it to float and assigning it to every component.
float3x2Returns a float3x2 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float3x3Returns a float3x3 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float3x4Returns a float3x4 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float4Returns a float4 vector constructed from a single bool value by converting it to float and assigning it to every component.
float4x2Returns a float4x2 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float4x3Returns a float4x3 matrix constructed from a single bool value by converting it to float and assigning it to every component.
float4x4Returns a float4x4 matrix constructed from a single bool value by converting it to float and assigning it to every component.
floorReturns the result of rounding a double value down to the nearest integral value less or equal to the original value.
floorlog2Computes the floor of the base-2 logarithm of x.
fmodReturns the double precision floating point remainder of x/y.
forwardUnity's forward axis (0, 0, 1).
fracReturns the fractional part of a double value.
halfReturns a half value constructed from a double value.
half2Returns a half2 vector constructed from a single double value by converting it to half and assigning it to every component.
half3Returns a half3 vector constructed from a single double value by converting it to half and assigning it to every component.
half4Returns a half4 vector constructed from a single double value by converting it to half and assigning it to every component.
hashReturns a uint hash from a block of memory using the xxhash32 algorithm. Can only be used in an unsafe context.
hashwideReturns 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 a single bool value by converting it to int and assigning it to every component.
int2x2Returns a int2x2 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int2x3Returns a int2x3 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int2x4Returns a int2x4 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int3Returns a int3 vector constructed from a single bool value by converting it to int and assigning it to every component.
int3x2Returns a int3x2 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int3x3Returns a int3x3 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int3x4Returns a int3x4 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int4Returns a int4 vector constructed from a single bool value by converting it to int and assigning it to every component.
int4x2Returns a int4x2 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int4x3Returns a int4x3 matrix constructed from a single bool value by converting it to int and assigning it to every component.
int4x4Returns a int4x4 matrix constructed from a single bool value by converting it to int and assigning it to every component.
inverseReturns the inverse of an AffineTransform.
isfiniteReturns true if the input double is a finite floating point value, false otherwise.
isinfReturns true if the input double is an infinite floating point value, false otherwise.
isnanReturns true if the input double is a NaN (not a number) floating point value, false otherwise.
ispow2Checks if the input is a power of two.
leftUnity's left axis (-1, 0, 0).
lengthReturns the length of a double value. Equivalent to the absolute value.
lengthsqReturns the squared length of a double value. Equivalent to squaring the value.
lerpReturns the result of linearly interpolating from x to y using the interpolation parameter t.
logReturns the natural logarithm of a double value.
log10Returns the base-10 logarithm of a double value.
log2Returns the base-2 logarithm of a double 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 double values.
maxReturns the maximum of two double values.
minReturns the minimum of two double values.
modfSplits a double 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.
mulScaleMatrix 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 double2 vector x by scaling it by 1 / length(x).
normalizesafeReturns a safe normalized version of the double4 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_basisGenerate an orthonormal basis given a single unit length normal vector.
powReturns x raised to the power y.
projectCompute vector projection of a onto b.
projectsafeCompute 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 double value.
reflectGiven an incident vector i and a normal vector n, returns the reflection vector r = i - 2.0 " 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.
rightUnity's right axis (1, 0, 0).
RigidTransformReturns a RigidTransform constructed from a rotation represented by a float3x3 rotation matrix 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.
rotationExtracts the rotation from a matrix.
roundReturns the result of rounding a double value to the nearest integral value.
rsqrtReturns the reciprocal square root of a double value.
saturateReturns the result of clamping the double value x into the interval [0, 1].
scaleMulMatrix 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 double value. -1.0 if it is less than zero, 0.0 if it is zero and 1.0 if it greater than zero.
sinReturns the sine of a double value.
sincosReturns the sine and cosine of the input double value x through the out parameters s and c.
sinhReturns the hyperbolic sine of a double 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.0 and 1.0 when x is in the interval (inclusive) [xMin, xMax].
sqrtReturns the square root of a double value.
squareComputes 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 double value.
tanhReturns the hyperbolic tangent of a double 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 double value to an integral double value.
tzcntComputes the trailing zero count in the binary representation of the input value.
uint2Returns a uint2 vector constructed from a single bool value by converting it to uint and assigning it to every component.
uint2x2Returns a uint2x2 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint2x3Returns a uint2x3 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint2x4Returns a uint2x4 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint3Returns a uint3 vector constructed from a single bool value by converting it to uint and assigning it to every component.
uint3x2Returns a uint3x2 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint3x3Returns a uint3x3 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint3x4Returns a uint3x4 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint4Returns a uint4 vector constructed from a single bool value by converting it to uint and assigning it to every component.
uint4x2Returns a uint4x2 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint4x3Returns a uint4x3 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
uint4x4Returns a uint4x4 matrix constructed from a single bool value by converting it to uint and assigning it to every component.
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).
upUnity's up axis (0, 1, 0).

Enums

Enum

Description

math.RotationOrderExtrinsic rotation order. Specifies in which order rotations around the principal axes (x, y and z) are to be applied.
math.ShuffleComponentSpecifies a shuffle component.