Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Arduino Math Library


May 15, 2021 Arduino


Table of contents


The Arduino Math Library (math.h) contains many useful mathematical functions for operating floating-point numbers.

The macro in the library

Here are the macros defined in the title math.h:

Macro Value Describe
M_E 2.7182818284590452354 Constant e.
M_LOG2E

1.4426950408889634074

/* log_2 e */

e A pair of 2-based pairs.
M_1_PI

0.31830988618379067154

/* 1/pi */

Constant 1/pi.
M_2_PI

0.63661977236758134308

/* 2/pi */

Constant 2/pi.
M_2_SQRTPI

1.12837916709551257390

/* 2/sqrt(pi) */

Constant 2/sqrt (pi).
M_LN10

2.30258509299404568402

/* log_e 10 */

10 natural pairs.
M_LN2

0.69314718055994530942

/* log_e 2 */

2 Natural pair.
M_LOG10E

0.43429448190325182765

/* log_10 e */

e A pair of 10s.
M_PI

3.14159265358979323846

/* pi */

Constant pi.
M_PI_2

3.3V1.57079632679489661923

/* pi/2 */

Constant pi/2.
M_PI_4

0.78539816339744830962

/* pi/4 */

Constant pi/4.
M_SQRT1_2

0.70710678118654752440

/* 1/sqrt(2) */

Constant 1/sqrt (2).
M_SQRT2

1.41421356237309504880

/* sqrt(2) */

The square root of 2.
acosf - The alias of the acos() function.
asinf - The alias of the asin() function.
atan2f - The alias of the atan2() function.
cbrtf - The alias of the cbrt() function.
ceilf - The alias of the ceil() function.
copysignf - The alias of the copysign() function.
coshf - The alias of the cosh() function.
expf - The alias of the exp() function.
fabsf - The alias of the fabs() function.
fdimf - The alias of the fdim() function.
floorf - The alias of the floor() function.
fmaxf - The alias of the fmax() function.
fminf - The alias of the fmin() function.
fmodf - The alias of the fmod() function.
frexpf - The alias of the frexp() function.
hypotf - The alias of the hypot() function.
INFINITY - Infinity constant.
isfinitef - The alias of the isfinite() function.
isinff - The alias of the isinf() function.
isnanf - The alias of the isnan() function.
ldexpf - The alias of the ldexp() function.
log10f - The alias of the log10() function.
logf - The alias of the log() function.
lrintf - The alias of the lrint() function.
lroundf - The alias of the lround() function.

The library function

The following functions are defined in the title math.h:

Serial number Library functions and descriptions
1

double acos (double __x)

The acos() function calculates the main value of x's anti-cosine. T he return value is in the range of the arc of . A domain error will occur for parameters that are not in the range of .-1, .1.

2

double asin (double __x)

The asin() function calculates the main value of x's antisine. T he return value is in the range of the arc of .-pi/2, pi/2. A domain error will occur for parameters that are not in the range of .-1, .1.

3

double atan (double __x)

The atan() function calculates the main value of the reverse cut of x. The return value is in the range of the arc of .-pi/2, pi/2.

4

double atan2 (double __y, double __x)

The atan2() function calculates the main value of the reverse cut of y/x, using the symbols of two parameters to determine the quadrant of the returned value. The return value is in the range of the arc of .-pi, .pi.

5

double cbrt (double __x)

The cbrt() function returns the cubic root value of x.

6

double ceil (double __x)

The ceil() function returns the minimum integer value greater than or equal to x, expressed as floating points.

7

static double copysign (double __x, double __y)

The copysign() function returns x, but with the y symbol. Even if x or y is NaN or zero, they can work.

8

double cos(double __x)

The cos() function returns the cosine of x, in radians.

9

double cosh (double __x)

The cosh() function returns x's double cosine.

10

double exp (double __x)

exp() returns the value of the x-power of e.

11

double fabs (double __x)

The fabs() function calculates the absolute value of floating point x.

12

double fdim (double __x, double __y)

The fdim() function returns max(x - y, 0). If x or y or both are NaN, return NaN.

13

double floor (double __x)

The floor() function returns the maximum integer value of less than or equal to x, expressed as floating points.

14

double fma (double __x, double __y, double __z)

The fma() function performs floating-point multiplier, i.e. the operation (x-y) plus z, but the intermediate result is not rounded to the target type. This can sometimes improve the accuracy of calculations.

15

double fmax (double __x, double __y)

The fmax() function returns the larger of the two values x and y. I f one argument is NaN, another argument is returned. If both parameters are NaN, NaN is returned.

16

double fmin (double __x, double __y)

The fmin() function returns the smaller of the two values x and y. I f one argument is NaN, another argument is returned. If both parameters are NaN, NaN is returned.

17

double fmod (double __x, double__y)

The fmod() function returns the remaining x/y.

18

double frexp (double __x, int * __pexp)

The frexp() function breaks down floats into normalized fractions and the entire power of 2. I t stores integers in the int object that pexp points to. I f x is a normal floating point, the frexp() function returns a value of v, giving v a measure of the interval of 1/2, 1) or zero, and x is equal to v multiplied by the power of 2 pexp. I f x is zero, both parts of the result are zero. If x is not a finite number, frexp() will return x as is and store 0 via pexp.

Note - This implementation allows a zero pointer to be used as an instruction to skip the stored exponent.

19

double hypot (double __x, double__y)

The hypot() function returns sqrt (x x x s y sy). T his is the length of a right triangle with an edge length of x and y, or the distance of the point (x, y) from the origin. I t is wise to use this function instead of using the formula directly, because the error is much smaller. x and y did not overflow. If the result is in range, it does not overflow.

20

static int isfinite (double __x)

If x is finite() the function returns a non-zero value: not positive or negative infinity, nor NaN.

21

int isinf (double __x)

If the argument x is positive infinity, the function isinf() returns 1;

Note : GCC 4.3 can replace this function with inline code, which returns a value of 1 (gcc bug #35509).

22

int isnan (double __x)

If the parameter x represents a "non-numeric" (NaN) object, the function isnan() returns 1 or 0.

23

double ldexp (double __x, int __exp )

The ldexp() function multiplies the floating point by the integer power of 2. It returns the value of x multiplied by the exp power of 2.

24

double log (double __x)

The log() function returns the natural log of argument x.

25

double log10(double __x)

The log10() function returns the log number of argument x, based on 10.

26

long lrint (double __x)

The lrint() function rounds x to the nearest integer, rounding the intermediate situation to an even integer direction (for example, the values of 1.5 and 2.5 are rounded to 2). This function is similar to the inrint() function, but its return value type is different and may overflow.

Return

Rounded long integer values. If x is not a finite number or overflows, this implementation returns LONG_MIN value (0x80000000).

27

long lround (double __x)

lround() rounds the function x to the nearest integer, but the intermediate does not round to 0 (not to the nearest even integer). This function is similar to the round() function, but its return value is of a different type and can overflow.

Return

Rounded long integer values. If x is not a finite number or overflows, this implementation returns LONG_MIN value (0x80000000).

28

double modf (double __x, double * __iptr )

The modf() function decomposes argument x into integer and integer parts, each with the same symbol as the argument. It stores the integer portion as double in the object that the iptr points to.

The modf() function returns a signed subsector of x.

Note : This implementation skips the write of the zero pointer. However, GCC 4.3 can replace this function with inline code, and NULL addresses are not allowed to avoid storage.

29

float modff (float __x, float * __iptr)

The alias of the modf() function.

30

double pow (double __x, double __y)

The pow() function returns the y-power of x.

31

double round (double __x)

The round() function rounds x to the nearest integer, but the intermediate situation is not rounded to 0 (not to the nearest even integer). T here is no likely to overflow.

Return

The rounded value. I f x is integer or infinity, return x itself. If x is NaN, return NaN.

32

int signbit (double __x)

If the value of x is set with a symbol bit, the signbit() function returns a non-zero value. T his is different from "x -lt; 0.0" because the IEEE 754 floating point allows zero signatures. It is wrong to compare "-0.0- lt; 0.0", but "signbit (-0.0)" returns a non-zero value.

33

double sin (double __x)

The sin() function returns the sine value of x, in radians.

34

double sinh (double __x)

The sinh() function returns x's double sine.

35

double sqrt (double __x)

The sqrt() function returns x's non-negative square root.

36

double square (double __x)

The square() function returns x.

Note - This function does not belong to the C standard definition.

37

double tan (double __x)

The tan() function returns a positive cut value of x, in radians.

38

double tanh ( double __x)

The tanh() function returns a double positive cut of x.

39

double trunc (double __x)

The trunc() function rounds x to the nearest integer, not greater than the absolute value.


Example

The following example shows how to use the most commonly used math.h library functions:

double double__x = 45.45 ;
double double__y = 30.20 ;

void setup() {
   Serial.begin(9600);
   Serial.print("cos num = ");
   Serial.println (cos (double__x) ); // returns cosine of x
   Serial.print("absolute value of num = ");
   Serial.println (fabs (double__x) ); // absolute value of a float
   Serial.print("floating point modulo = ");
   Serial.println (fmod (double__x, double__y)); // floating point modulo
   Serial.print("sine of num = ");
   Serial.println (sin (double__x) ) ;// returns sine of x
   Serial.print("square root of num : ");
   Serial.println ( sqrt (double__x) );// returns square root of x
   Serial.print("tangent of num : ");
   Serial.println ( tan (double__x) ); // returns tangent of x
   Serial.print("exponential value of num : ");
   Serial.println ( exp (double__x) ); // function returns the exponential value of x.
   Serial.print("cos num : ");

   Serial.println (atan (double__x) ); // arc tangent of x
   Serial.print("tangent of num : ");
   Serial.println (atan2 (double__y, double__x) );// arc tangent of y/x
   Serial.print("arc tangent of num : ");
   Serial.println (log (double__x) ) ; // natural logarithm of x
   Serial.print("cos num : ");
   Serial.println ( log10 (double__x)); // logarithm of x to base 10.
   Serial.print("logarithm of num to base 10 : ");
   Serial.println (pow (double__x, double__y) );// x to power of y
   Serial.print("power of num : ");
   Serial.println (square (double__x)); // square of x
}

void loop() {

}

Results

cos num = 0.10
absolute value of num = 45.45
floating point modulo =15.25
sine of num = 0.99
square root of num : 6.74
tangent of num : 9.67
exponential value of num : ovf
cos num : 1.55
tangent of num : 0.59
arc tangent of num : 3.82
cos num : 1.66
logarithm of num to base 10 : inf
power of num : 2065.70