Mathf
class in Math
Description
(code of this item is picked from Godot Engine in compliance with MIT license).
Provides constants and static methods for common mathematical functions.
Constants
Name | Type | Value | Description |
---|---|---|---|
Tau | float | (real_t)6.2831853071795864769252867666M | The circle constant, the circumference of the unit circle in radians. |
Pi | float | (real_t)3.1415926535897932384626433833M | Constant that represents how many times the diameter of a circle fits around its perimeter. This is equivalent to Mathf.Tau / 2 . |
Inf | float | real_t.PositiveInfinity | Positive infinity. For negative infinity, use -Mathf.Inf . |
NaN | float | real_t.NaN | "Not a Number", an invalid value. NaN has special properties, including that it is not equal to itself. It is output by some invalid operations, such as dividing zero by zero. |
E | float | (real_t)2.7182818284590452353602874714M | The natural number e . |
Sqrt2 | float | (real_t)1.4142135623730950488016887242M | The square root of 2. |
Epsilon | float | EpsilonF | A very small number used for float comparison with error tolerance. 1e-06 with single-precision floats, but 1e-14 if REAL_T_IS_DOUBLE . |
Static Methods
Return Type | Signature | Description |
---|---|---|
int | Abs ( int s ) | Returns the absolute value of name (i.e. positive value). |
float | Abs ( float s ) | Returns the absolute value of name (i.e. positive value). |
double | Abs ( double s ) | Returns the absolute value of name (i.e. positive value). |
float | Acos ( float s ) | Returns the arc cosine of name in radians. Use to get the angle of cosine name . |
double | Acos ( double s ) | Returns the arc cosine of name in radians. Use to get the angle of cosine name . |
float | Acosh ( float s ) | Returns the hyperbolic arc (also called inverse) cosine of name in radians. Use it to get the angle from an angle's cosine in hyperbolic space if name is larger or equal to 1. |
double | Acosh ( double s ) | Returns the hyperbolic arc (also called inverse) cosine of name in radians. Use it to get the angle from an angle's cosine in hyperbolic space if name is larger or equal to 1. |
float | AngleDifference ( float from, float to ) | Returns the difference between the two angles, in range of - Pi , Pi . When name and name are opposite, returns - Pi if name is smaller than name , or Pi otherwise. |
double | AngleDifference ( double from, double to ) | Returns the difference between the two angles, in range of - Pi , Pi . When name and name are opposite, returns - Pi if name is smaller than name , or Pi otherwise. |
float | Asin ( float s ) | Returns the arc sine of name in radians. Use to get the angle of sine name . |
double | Asin ( double s ) | Returns the arc sine of name in radians. Use to get the angle of sine name . |
float | Asinh ( float s ) | Returns the hyperbolic arc (also called inverse) sine of name in radians. Use it to get the angle from an angle's sine in hyperbolic space if name is larger or equal to 1. |
double | Asinh ( double s ) | Returns the hyperbolic arc (also called inverse) sine of name in radians. Use it to get the angle from an angle's sine in hyperbolic space if name is larger or equal to 1. |
float | Atan ( float s ) | Returns the arc tangent of name in radians. Use to get the angle of tangent name . The method cannot know in which quadrant the angle should fall. See Atan2 if you have both y and x . |
double | Atan ( double s ) | Returns the arc tangent of name in radians. Use to get the angle of tangent name . The method cannot know in which quadrant the angle should fall. See Atan2 if you have both y and x . |
float | Atan2 ( float y, float x ) | Returns the arc tangent of name and name in radians. Use to get the angle of the tangent of y/x . To compute the value, the method takes into account the sign of both arguments in order to determine the quadrant. Important note: The Y coordinate comes first, by convention. |
double | Atan2 ( double y, double x ) | Returns the arc tangent of name and name in radians. Use to get the angle of the tangent of y/x . To compute the value, the method takes into account the sign of both arguments in order to determine the quadrant. Important note: The Y coordinate comes first, by convention. |
float | Atanh ( float s ) | Returns the hyperbolic arc (also called inverse) tangent of name in radians. Use it to get the angle from an angle's tangent in hyperbolic space if name is between -1 and 1 (non-inclusive). |
double | Atanh ( double s ) | Returns the hyperbolic arc (also called inverse) tangent of name in radians. Use it to get the angle from an angle's tangent in hyperbolic space if name is between -1 and 1 (non-inclusive). |
float | Ceil ( float s ) | Rounds name upward (towards positive infinity). |
double | Ceil ( double s ) | Rounds name upward (towards positive infinity). |
int | Clamp ( int value, int min, int max ) | Clamps a name so that it is not less than name and not more than name . |
float | Clamp ( float value, float min, float max ) | Clamps a name so that it is not less than name and not more than name . |
double | Clamp ( double value, double min, double max ) | Clamps a name so that it is not less than name and not more than name . |
float | Cos ( float s ) | Returns the cosine of angle name in radians. |
double | Cos ( double s ) | Returns the cosine of angle name in radians. |
float | Cosh ( float s ) | Returns the hyperbolic cosine of angle name in radians. |
double | Cosh ( double s ) | Returns the hyperbolic cosine of angle name in radians. |
float | CubicInterpolate ( float from, float to, float pre, float post, float weight ) | Cubic interpolates between two values by the factor defined in name with pre and post values. |
double | CubicInterpolate ( double from, double to, double pre, double post, double weight ) | Cubic interpolates between two values by the factor defined in name with pre and post values. |
float | CubicInterpolateAngle ( float from, float to, float pre, float post, float weight ) | Cubic interpolates between two rotation values with shortest path by the factor defined in name with pre and post values. See also LerpAngle . |
double | CubicInterpolateAngle ( double from, double to, double pre, double post, double weight ) | Cubic interpolates between two rotation values with shortest path by the factor defined in name with pre and post values. See also LerpAngle . |
float | CubicInterpolateInTime ( float from, float to, float pre, float post, float weight, float toT, float preT, float postT ) | Cubic interpolates between two values by the factor defined in name with pre and post values. It can perform smoother interpolation than CubicInterpolate by the time values. |
double | CubicInterpolateInTime ( double from, double to, double pre, double post, double weight, double toT, double preT, double postT ) | Cubic interpolates between two values by the factor defined in name with pre and post values. It can perform smoother interpolation than CubicInterpolate by the time values. |
float | CubicInterpolateAngleInTime ( float from, float to, float pre, float post, float weight, float toT, float preT, float postT ) | Cubic interpolates between two rotation values with shortest path by the factor defined in name with pre and post values. See also LerpAngle . It can perform smoother interpolation than CubicInterpolateAngle by the time values. |
double | CubicInterpolateAngleInTime ( double from, double to, double pre, double post, double weight, double toT, double preT, double postT ) | Cubic interpolates between two rotation values with shortest path by the factor defined in name with pre and post values. See also LerpAngle . It can perform smoother interpolation than CubicInterpolateAngle by the time values. |
float | BezierInterpolate ( float start, float control1, float control2, float end, float t ) | Returns the point at the given name on a one-dimensional Bezier curve defined by the given name , name , and name points. |
double | BezierInterpolate ( double start, double control1, double control2, double end, double t ) | Returns the point at the given name on a one-dimensional Bezier curve defined by the given name , name , and name points. |
float | BezierDerivative ( float start, float control1, float control2, float end, float t ) | Returns the derivative at the given name on a one dimensional Bezier curve defined by the given name , name , and name points. |
double | BezierDerivative ( double start, double control1, double control2, double end, double t ) | Returns the derivative at the given name on a one dimensional Bezier curve defined by the given name , name , and name points. |
float | DbToLinear ( float db ) | Converts from decibels to linear energy (audio). |
double | DbToLinear ( double db ) | Converts from decibels to linear energy (audio). |
float | DegToRad ( float deg ) | Converts an angle expressed in degrees to radians. |
double | DegToRad ( double deg ) | Converts an angle expressed in degrees to radians. |
float | Ease ( float s, float curve ) | Easing function, based on exponent. The name values are: 0 is constant, 1 is linear, 0 to 1 is ease-in, 1 or more is ease-out. Negative values are in-out/out-in. |
double | Ease ( double s, double curve ) | Easing function, based on exponent. The name values are: 0 is constant, 1 is linear, 0 to 1 is ease-in, 1 or more is ease-out. Negative values are in-out/out-in. |
float | Exp ( float s ) | The natural exponential function. It raises the mathematical constant e to the power of name and returns it. |
double | Exp ( double s ) | The natural exponential function. It raises the mathematical constant e to the power of name and returns it. |
float | Floor ( float s ) | Rounds name downward (towards negative infinity). |
double | Floor ( double s ) | Rounds name downward (towards negative infinity). |
float | InverseLerp ( float from, float to, float weight ) | Returns a normalized value considering the given range. This is the opposite of Lerp . |
double | InverseLerp ( double from, double to, double weight ) | Returns a normalized value considering the given range. This is the opposite of Lerp . |
bool | IsEqualApprox ( float a, float b ) | Returns true if name and name are approximately equal to each other. The comparison is done using a tolerance calculation with Epsilon . |
bool | IsEqualApprox ( double a, double b ) | Returns true if name and name are approximately equal to each other. The comparison is done using a tolerance calculation with Epsilon . |
bool | IsFinite ( float s ) | Returns whether name is a finite value, i.e. it is not NaN , positive infinite, or negative infinity. |
bool | IsFinite ( double s ) | Returns whether name is a finite value, i.e. it is not NaN , positive infinite, or negative infinity. |
bool | IsInf ( float s ) | Returns whether name is an infinity value (either positive infinity or negative infinity). |
bool | IsInf ( double s ) | Returns whether name is an infinity value (either positive infinity or negative infinity). |
bool | IsNaN ( float s ) | Returns whether name is a NaN ("Not a Number" or invalid) value. |
bool | IsNaN ( double s ) | Returns whether name is a NaN ("Not a Number" or invalid) value. |
bool | IsZeroApprox ( float s ) | Returns true if name is zero or almost zero. The comparison is done using a tolerance calculation with Epsilon . This method is faster than using IsEqualApprox with one value as zero. |
bool | IsZeroApprox ( double s ) | Returns true if name is zero or almost zero. The comparison is done using a tolerance calculation with Epsilon . This method is faster than using IsEqualApprox with one value as zero. |
float | Lerp ( float from, float to, float weight ) | Linearly interpolates between two values by a normalized value. This is the opposite InverseLerp . |
double | Lerp ( double from, double to, double weight ) | Linearly interpolates between two values by a normalized value. This is the opposite InverseLerp . |
float | LerpAngle ( float from, float to, float weight ) | Linearly interpolates between two angles (in radians) by a normalized value. Similar to Lerp , but interpolates correctly when the angles wrap around Tau . |
double | LerpAngle ( double from, double to, double weight ) | Linearly interpolates between two angles (in radians) by a normalized value. Similar to Lerp , but interpolates correctly when the angles wrap around Tau . |
float | LinearToDb ( float linear ) | Converts from linear energy to decibels (audio). This can be used to implement volume sliders that behave as expected (since volume isn't linear). |
double | LinearToDb ( double linear ) | Converts from linear energy to decibels (audio). This can be used to implement volume sliders that behave as expected (since volume isn't linear). |
float | Log ( float s ) | Natural logarithm. The amount of time needed to reach a certain level of continuous growth. Note: This is not the same as the "log" function on most calculators, which uses a base 10 logarithm. |
double | Log ( double s ) | Natural logarithm. The amount of time needed to reach a certain level of continuous growth. Note: This is not the same as the "log" function on most calculators, which uses a base 10 logarithm. |
int | Max ( int a, int b ) | Returns the maximum of two values. |
float | Max ( float a, float b ) | Returns the maximum of two values. |
double | Max ( double a, double b ) | Returns the maximum of two values. |
int | Min ( int a, int b ) | Returns the minimum of two values. |
float | Min ( float a, float b ) | Returns the minimum of two values. |
double | Min ( double a, double b ) | Returns the minimum of two values. |
float | MoveToward ( float from, float to, float delta ) | Moves name toward name by the name value. Use a negative name value to move away. |
double | MoveToward ( double from, double to, double delta ) | Moves name toward name by the name value. Use a negative name value to move away. |
int | NearestPo2 ( int value ) | Returns the nearest larger power of 2 for the integer name . |
int | PosMod ( int a, int b ) | Performs a canonical Modulus operation, where the output is on the range [0, name ). |
float | PosMod ( float a, float b ) | Performs a canonical Modulus operation, where the output is on the range [0, name ). |
double | PosMod ( double a, double b ) | Performs a canonical Modulus operation, where the output is on the range [0, name ). |
float | Pow ( float x, float y ) | Returns the result of name raised to the power of name . |
double | Pow ( double x, double y ) | Returns the result of name raised to the power of name . |
float | RadToDeg ( float rad ) | Converts an angle expressed in radians to degrees. |
double | RadToDeg ( double rad ) | Converts an angle expressed in radians to degrees. |
float | Remap ( float value, float inFrom, float inTo, float outFrom, float outTo ) | Maps a name from [ name , name ] to [ name , name ]. |
double | Remap ( double value, double inFrom, double inTo, double outFrom, double outTo ) | Maps a name from [ name , name ] to [ name , name ]. |
float | RotateToward ( float from, float to, float delta ) | Rotates name toward name by the name amount. Will not go past name . Similar to MoveToward but interpolates correctly when the angles wrap around Tau . If name is negative, this function will rotate away from name , toward the opposite angle, and will not go past the opposite angle. |
double | RotateToward ( double from, double to, double delta ) | Rotates name toward name by the name amount. Will not go past name . Similar to MoveToward but interpolates correctly when the angles wrap around Tau . If name is negative, this function will rotate away from name , toward the opposite angle, and will not go past the opposite angle. |
float | Round ( float s ) | Rounds name to the nearest whole number, with halfway cases rounded towards the nearest multiple of two. |
double | Round ( double s ) | Rounds name to the nearest whole number, with halfway cases rounded towards the nearest multiple of two. |
int | Sign ( int s ) | Returns the sign of name : -1 or 1 . Returns 0 if name is 0 . |
int | Sign ( float s ) | Returns the sign of name : -1 or 1 . Returns 0 if name is 0 . |
int | Sign ( double s ) | Returns the sign of name : -1 or 1 . Returns 0 if name is 0 . |
float | Sin ( float s ) | Returns the sine of angle name in radians. |
double | Sin ( double s ) | Returns the sine of angle name in radians. |
float | Sinh ( float s ) | Returns the hyperbolic sine of angle name in radians. |
double | Sinh ( double s ) | Returns the hyperbolic sine of angle name in radians. |
float | SmoothStep ( float from, float to, float weight ) | Returns a number smoothly interpolated between name and name , based on the name . Similar to Lerp , but interpolates faster at the beginning and slower at the end. |
double | SmoothStep ( double from, double to, double weight ) | Returns a number smoothly interpolated between name and name , based on the name . Similar to Lerp , but interpolates faster at the beginning and slower at the end. |
float | Sqrt ( float s ) | Returns the square root of name , where name is a non-negative number. If you need negative inputs, use System.Numerics.Complex . |
double | Sqrt ( double s ) | Returns the square root of name , where name is a non-negative number. If you need negative inputs, use System.Numerics.Complex . |
int | StepDecimals ( double step ) | Returns the position of the first non-zero digit, after the decimal point. Note that the maximum return value is 10, which is a design decision in the implementation. |
float | Snapped ( float s, float step ) | Snaps float value name to a given name . This can also be used to round a floating point number to an arbitrary number of decimals. |
double | Snapped ( double s, double step ) | Snaps float value name to a given name . This can also be used to round a floating point number to an arbitrary number of decimals. |
float | Tan ( float s ) | Returns the tangent of angle name in radians. |
double | Tan ( double s ) | Returns the tangent of angle name in radians. |
float | Tanh ( float s ) | Returns the hyperbolic tangent of angle name in radians. |
double | Tanh ( double s ) | Returns the hyperbolic tangent of angle name in radians. |
int | Wrap ( int value, int min, int max ) | Wraps name between name and name . Usable for creating loop-alike behavior or infinite surfaces. If name is 0 , this is equivalent to PosMod , so prefer using that instead. |
float | Wrap ( float value, float min, float max ) | Wraps name between name and name . Usable for creating loop-alike behavior or infinite surfaces. If name is 0 , this is equivalent to PosMod , so prefer using that instead. |
double | Wrap ( double value, double min, double max ) | Wraps name between name and name . Usable for creating loop-alike behavior or infinite surfaces. If name is 0 , this is equivalent to PosMod , so prefer using that instead. |
float | PingPong ( float value, float length ) | Returns the name wrapped between 0 and the name . If the limit is reached, the next value the function returned is decreased to the 0 side or increased to the name side (like a triangle wave). If name is less than zero, it becomes positive. |
double | PingPong ( double value, double length ) | Returns the name wrapped between 0 and the name . If the limit is reached, the next value the function returned is decreased to the 0 side or increased to the name side (like a triangle wave). If name is less than zero, it becomes positive. |
int | DecimalCount ( double s ) | Returns the amount of digits after the decimal place. |
int | DecimalCount ( decimal s ) | Returns the amount of digits after the decimal place. |
int | CeilToInt ( float s ) | Rounds name upward (towards positive infinity). This is the same as Ceil , but returns an int . |
int | CeilToInt ( double s ) | Rounds name upward (towards positive infinity). This is the same as Ceil , but returns an int . |
int | FloorToInt ( float s ) | Rounds name downward (towards negative infinity). This is the same as Floor , but returns an int . |
int | FloorToInt ( double s ) | Rounds name downward (towards negative infinity). This is the same as Floor , but returns an int . |
int | RoundToInt ( float s ) | Rounds name to the nearest whole number. This is the same as Round , but returns an int . |
int | RoundToInt ( double s ) | Rounds name to the nearest whole number. This is the same as Round , but returns an int . |
(float Sin, float Cos) | SinCos ( float s ) | Returns the sine and cosine of angle name in radians. |
(double Sin, double Cos) | SinCos ( double s ) | Returns the sine and cosine of angle name in radians. |
bool | IsEqualApprox ( float a, float b, float tolerance ) | Returns true if name and name are approximately equal to each other. The comparison is done using the provided tolerance value. If you want the tolerance to be calculated for you, use IsEqualApprox . |
bool | IsEqualApprox ( double a, double b, double tolerance ) | Returns true if name and name are approximately equal to each other. The comparison is done using the provided tolerance value. If you want the tolerance to be calculated for you, use IsEqualApprox . |