# <QtMath> - Generic Math Functions

The <QtMath> header file provides various math functions. More...

Header: | #include <QtMath> |

## Functions

auto | qAcos(T v) |

auto | qAsin(T v) |

auto | qAtan2(T1 y, T2 x) |

auto | qAtan(T v) |

auto | qCos(T v) |

float | qDegreesToRadians(float degrees) |

double | qDegreesToRadians(double degrees) |

long double | qDegreesToRadians(long double degrees) |

auto | qExp(T v) |

auto | qFabs(T v) |

auto | qLn(T v) |

quint32 | qNextPowerOfTwo(quint32 value) |

quint32 | qNextPowerOfTwo(qint32 value) |

quint64 | qNextPowerOfTwo(quint64 value) |

quint64 | qNextPowerOfTwo(qint64 value) |

auto | qPow(T1 x, T2 y) |

float | qRadiansToDegrees(float radians) |

double | qRadiansToDegrees(double radians) |

long double | qRadiansToDegrees(long double radians) |

auto | qSin(T v) |

auto | qSqrt(T v) |

auto | qTan(T v) |

## Detailed Description

These functions are partly convenience definitions for basic math operations not available in the C or Standard Template Libraries.

The header also ensures some constants specified in POSIX, but not present in C++ standards (so absent from <math.h> on some platforms), are defined:

Constant | Description |
---|---|

`M_E` | The base of the natural logarithms, e = exp(1) |

`M_LOG2E` | The base-two logarithm of e |

`M_LOG10E` | The base-ten logarithm of e |

`M_LN2` | The natural logarithm of two |

`M_LN10` | The natural logarithm of ten |

`M_PI` | The ratio of a circle's circumference to diameter, π |

`M_PI_2` | Half M_PI, π / 2 |

`M_PI_4` | Quarter M_PI, π / 4 |

`M_1_PI` | The inverse of M_PI, 1 / π |

`M_2_PI` | Twice the inverse of M_PI, 2 / π |

`M_2_SQRTPI` | Two divided by the square root of pi, 2 / √π |

`M_SQRT2` | The square root of two, √2 |

`M_SQRT1_2` | The square roof of half, 1 / √2 |

## Function Documentation

### template <typename T> auto qAcos(T *v*)

Returns the arccosine of *v* as an angle in radians. Arccosine is the inverse operation of cosine.

**See also **qAtan(), qAsin(), and qCos().

### template <typename T> auto qAsin(T *v*)

Returns the arcsine of *v* as an angle in radians. Arcsine is the inverse operation of sine.

**See also **qSin(), qAtan(), and qAcos().

### template <typename T1, typename T2> auto qAtan2(T1 *y*, T2 *x*)

Returns the arctangent of a point specified by the coordinates *y* and *x*. This function will return the angle (argument) of that point.

**See also **qAtan().

### template <typename T> auto qAtan(T *v*)

Returns the arctangent of *v* as an angle in radians. Arctangent is the inverse operation of tangent.

**See also **qTan(), qAcos(), and qAsin().

### template <typename T> auto qCos(T *v*)

Returns the cosine of an angle *v* in radians.

`[since 5.1] `

float qDegreesToRadians(float *degrees*)

This function converts the *degrees* in float to radians.

Example:

float degrees = 180.0f float radians = qDegreesToRadians(degrees)

This function was introduced in Qt 5.1.

**See also **qRadiansToDegrees().

`[since 5.1] `

double qDegreesToRadians(double *degrees*)

This function converts the *degrees* in double to radians.

Example:

double degrees = 180.0 double radians = qDegreesToRadians(degrees)

This function was introduced in Qt 5.1.

**See also **qRadiansToDegrees().

`[since 6.0] `

long double qDegreesToRadians(long double *degrees*)

This function converts the *degrees* in double to radians.

This function was introduced in Qt 6.0.

**See also **qRadiansToDegrees().

### template <typename T> auto qExp(T *v*)

Returns the exponential function of `e`

to the power of *v*.

**See also **qLn().

### template <typename T> auto qFabs(T *v*)

Returns the absolute value of *v*.

### template <typename T> auto qLn(T *v*)

Returns the natural logarithm of *v*. Natural logarithm uses base e.

**See also **qExp().

`[since 5.4] `

quint32 qNextPowerOfTwo(quint32 *value*)

This function returns the nearest power of two greater than *value*. For 0 it returns 1, and for values larger than or equal to 2^31 it returns 0.

This function was introduced in Qt 5.4.

`[since 5.4] `

quint32 qNextPowerOfTwo(qint32 *value*)

This is an overloaded function.

This function returns the nearest power of two greater than *value*. For negative values it returns 0.

This function was introduced in Qt 5.4.

`[since 5.4] `

quint64 qNextPowerOfTwo(quint64 *value*)

This function returns the nearest power of two greater than *value*. For 0 it returns 1, and for values larger than or equal to 2^63 it returns 0.

This function was introduced in Qt 5.4.

`[since 5.4] `

quint64 qNextPowerOfTwo(qint64 *value*)

This is an overloaded function.

This function returns the nearest power of two greater than *value*. For negative values it returns 0.

This function was introduced in Qt 5.4.

### template <typename T1, typename T2> auto qPow(T1 *x*, T2 *y*)

Returns the value of *x* raised to the power of *y*. That is, *x* is the base and *y* is the exponent.

**See also **qSqrt().

`[since 5.1] `

float qRadiansToDegrees(float *radians*)

This function converts the *radians* in float to degrees.

Example:

float radians = float(M_PI) float degrees = qRadiansToDegrees(radians)

This function was introduced in Qt 5.1.

**See also **qDegreesToRadians().

`[since 5.1] `

double qRadiansToDegrees(double *radians*)

This function converts the *radians* in double to degrees.

Example:

double radians = M_PI double degrees = qRadiansToDegrees(radians)

This function was introduced in Qt 5.1.

**See also **qDegreesToRadians().

`[since 6.0] `

long double qRadiansToDegrees(long double *radians*)

This function converts the *radians* in double to degrees.

This function was introduced in Qt 6.0.

**See also **qDegreesToRadians().

### template <typename T> auto qSin(T *v*)

Returns the sine of the angle *v* in radians.

### template <typename T> auto qSqrt(T *v*)

Returns the square root of *v*. This function returns a NaN if *v* is a negative number.

**See also **qPow().

### template <typename T> auto qTan(T *v*)

Returns the tangent of an angle *v* in radians.

© 2021 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.