C Library math.h functions

0

The math.h library in C provides many useful mathematical functions. Here are some of the most commonly used ones:

  • sqrt(x) : Returns the square root of x.
  • pow(x, y) : Returns x raised to the power of y.
  • fabs(x) : Returns the absolute value of x.
  • ceil(x) : Returns the smallest integer greater than or equal to x.
  • floor(x) : Returns the largest integer less than or equal to x.
  • round(x) : Rounds x to the nearest integer.
  • sin(x) : Returns the sine of x (in radians).
  • cos(x) : Returns the cosine of x (in radians).
  • tan(x) : Returns the tangent of x (in radians).
  • exp(x) : Returns the value of e raised to the power of x.
  • log(x) : Returns the natural logarithm (base e) of x.
  • log10(x) : Returns the base 10 logarithm of x.
  • fmod(x, y) : Returns the remainder when x is divided by y.
  • sinh(x) : Returns the hyperbolic sine of x.
  • cosh(x) : Returns the hyperbolic cosine of x.
  • tanh(x) : Returns the hyperbolic tangent of x.
  • asin(x) : Returns the arc sine of x (in radians).
  • acos(x) : Returns the arc cosine of x (in radians).
  • atan(x) : Returns the arc tangent of x (in radians).
  • atan2(y, x) : Returns the arc tangent of y/x (in radians), taking into account the signs of both arguments.

Note that all of these functions take and return values of type double. Some functions also have variants with a trailing f in their name, such as sqrtf, which take and return values of type float.

  • double ceil(double x): The C library function double ceil (double x) returns the smallest integer value greater than or equal to x. 

syntax : double ceil(double x)

  • double floor(double x): The C library function double floor (double x) returns the largest integer value less than or equal to x.

syntax: double floor(double x)

  • double fabs(double x): The C library function double fabs (double x) returns the absolute value of x.

syntax: double fabs(double x)

  • double pow(double x, double y): The C library function double pow (double x, double y) returns the value of x raised to the power of y.

syntax: double pow(double x, double y)

  • double sqrt(double x): The C library function double sqrt (double x) returns the square root of x.

syntax: double sqrt(double x)

  • double sin(double x): The C library function double sin (double x) returns the sine of x radians.

syntax: double sin(double x)

  • double cos(double x): The C library function double cos (double x) returns the cosine of x radians.

syntax: double cos(double x)

  • double tan(double x): The C library function double tan (double x) returns the tangent of x radians.

syntax: double tan(double x)

  • double exp(double x): The C library function double exp (double x) returns the exponential value of x.

syntax: double exp(double x)

  • double log(double x): The C library function double log (double x) returns the natural logarithm (base-e logarithm) of x.

syntax: double log(double x)

  • double log10(double x): The C library function double log10 (double x) returns the base-10 logarithm of x.

syntax: double log10(double x)

  • double fmod(double x, double y): The C library function double fmod (double x, double y) returns the remainder of the division x/y.

syntax: double fmod(double x, double y)


Note that all of these functions take and return values of type double. Some functions also have variants with a trailing f in their name, such as ceilf, floorf, etc., which take and return values of type float. Additionally, there are other functions available in math.h, such as acos, asin, atan, atan2, cosh, sinh, tanh, etc., which perform other mathematical operations.


double floor(double x)

The double floor(double x) function is part of the math.h header file in C. It takes a double value as its argument and returns the largest integer value that is less than or equal to the argument. The returned value has the same type as the argument (double).


The syntax of the double floor(double x) function is:


double floor(double x)


Here, x is the argument passed to the function. The function returns the largest integer value less than or equal to x.

For example, if we call floor(3.14) the function will return 3. Similarly, calling floor(-2.8) will return -3 because it is the largest integer less than or equal to -2.8.

It's worth noting that the floor() function rounds down the given number to the nearest integer. If you want to round up to the nearest integer, you can use the ceil() function. If you want to round to the nearest integer, you can use the round() function.

double fabs(double x)

The double fabs(double x) function is part of the math.h header file in C. It takes a double value as its argument and returns the absolute value of the argument. The returned value has the same type as the argument (double).

The syntax of the double fabs(double x) function is:

double fabs(double x)


Here, x is the argument passed to the function. The function returns the absolute value of x.

For example, if we call fabs(-3.14) the function will return 3.14. Similarly, calling fabs(2.8) will return 2.8 because the absolute value of 2.8 is 2.8.

It's worth noting that the fabs() function works on both positive and negative numbers, and returns the same result regardless of the sign of the input. If you only want to get the absolute value of an integer, you can use the abs() function instead.

double log(double x)


The double log(double x) function is part of the math.h header file in C. It takes a double value as its argument and returns the natural logarithm (base-e logarithm) of the argument. The returned value has the same type as the argument (double).

The syntax of the double log(double x) function is:

double log(double x)

Here, x is the argument passed to the function. The function returns the natural logarithm of x.

For example, if we call log(1.0) the function will return 0, because the natural logarithm of 1 is 0. Similarly, calling log(10.0) will return 2.302585, because the natural logarithm of 10 is approximately 2.302585.

It's worth noting that the log() function works only on positive numbers. If you want to calculate the logarithm of a negative number, you can use the clog() function instead, which returns a complex number. If you want to calculate the logarithm with a different base, you can use the log10() function for a base-10 logarithm or use the formula log(x)/log(base) for a logarithm with a different base.

double log10(double x)


The double log10(double x) function is part of the math.h header file in C. It takes a double value as its argument and returns the common logarithm (base-10 logarithm) of the argument. The returned value has the same type as the argument (double).

The syntax of the double log10(double x) function is:

double log10(double x)


Here, x is the argument passed to the function. The function returns the common logarithm of x.

For example, if we call log10(1.0) the function will return 0, because the common logarithm of 1 is 0. Similarly, calling log10(100.0) will return 2, because the common logarithm of 100 is 2.

It's worth noting that the log10() function works only on positive numbers. If you want to calculate the logarithm with a different base, you can use the formula log(x)/log(base). If you want to calculate the natural logarithm (base-e logarithm), you can use the log() function. If you want to calculate the logarithm of a negative number, you can use the clog() function instead, which returns a complex number.

double fmod(double x, double y)


The double fmod(double x, double y) function is part of the math.h header file in C. It takes two double values as its arguments and returns the remainder of x divided by y. The returned value has the same type as the arguments (double).

The syntax of the double fmod(double x, double y) function is:

double fmod(double x, double y)

Here, x and y are the arguments passed to the function. The function returns the remainder of x divided by y.

For example, if we call fmod(5.0, 2.0) the function will return 1.0, because the remainder of 5 divided by 2 is 1. Similarly, calling fmod(10.0, 3.0) will return 1.0, because the remainder of 10 divided by 3 is 1.

It's worth noting that the fmod() function works with both positive and negative numbers. However, if you want to compute the modulus of integer values, it's more efficient to use the % operator instead of fmod(). Also, if you want to perform division and get the quotient as well as the remainder, you can use the div() function.

double sqrt(double x)


The double sqrt(double x) function is part of the math.h header file in C. It takes a double value as its argument and returns the square root of the argument. The returned value has the same type as the argument (double).

The syntax of the double sqrt(double x) function is:

double sqrt(double x)

Here, x is the argument passed to the function. The function returns the square root of x.

For example, if we call sqrt(16.0) the function will return 4.0, because the square root of 16 is 4. Similarly, calling sqrt(2.0) will return approximately 1.414214, because the square root of 2 is approximately 1.414214.

It's worth noting that the sqrt() function works only on non-negative numbers. If you want to compute the square root of a negative number, you can use the csqrt() function instead, which returns a complex number. If you want to compute the cube root of a number, you can use the cbrt() function.

double pow(double x, double y)


 The double pow(double x, double y) function is part of the math.h header file in C. It takes two double values as its arguments and returns the value of x raised to the power of y. The returned value has the same type as the arguments (double).

The syntax of the double pow(double x, double y) function is:

double pow(double x, double y)

Here, x and y are the arguments passed to the function. The function returns the value of x raised to the power of y.

For example, if we call pow(2.0, 3.0) the function will return 8.0, because 2 raised to the power of 3 is 8. Similarly, calling pow(3.0, 2.0) will return 9.0, because 3 raised to the power of 2 is 9. If the second argument is a fraction, the function returns the appropriate root of the first argument. For example, pow(16.0, 0.5) returns 4.0, because the square root of 16 is 4.

It's worth noting that the pow() function can also handle negative and fractional exponents. If the second argument is negative, the function returns the reciprocal of the value raised to the absolute value of the exponent. For example, pow(2.0, -3.0) returns 0.125, because 2 raised to the power of -3 is 1/8. If the first argument is negative and the second argument is a fraction whose denominator is odd, the function returns a negative value. If the first argument is negative and the second argument is a fraction whose denominator is even, the function returns a NaN (not a number) value. If the first argument is 0 and the second argument is negative or 0, the function returns a NaN value.

double modf(double x, double *integer)


The double modf(double x, double *integer) function is part of the math.h header file in C. It takes a double value x as its argument and a pointer to a double variable integer, and returns the fractional part of x. The integer part of x is stored in the variable pointed to by integer. The returned value has the same type as the argument (double).

The syntax of the double modf(double x, double *integer) function is:

double modf(double x, double *integer)

Here, x is the argument passed to the function and integer is a pointer to a double variable that will store the integer part of x. The function returns the fractional part of x.

For example, if we call modf(3.14, &integer), where integer is a double variable, the function will return 0.14 and store the value 3.0 in integer. Similarly, calling modf(-2.5, &integer) will return -0.5 and store the value -2.0 in integer. If the input value is NaN (not a number), the function returns NaN as well.

It's worth noting that the modf() function can be useful for separating the integer and fractional parts of a number. For example, if you want to print a number with a specific number of decimal places, you can use modf() to split the number into its integer and fractional parts, and then print the integer part followed by a decimal point and the appropriate number of digits from the fractional part.

double exp(double x)


The double exp(double x) function is part of the math.h header file in C. It takes a double value x as its argument and returns the value of the mathematical constant e (approximately 2.71828) raised to the power of x. The result has the same type as the argument (double).

The syntax of the double exp(double x) function is:

double exp(double x)

Here, x is the argument passed to the function, and the function returns the value of e raised to the power of x.

For example, calling exp(1.0) will return the value e (approximately 2.71828) because e raised to the power of 1 is e. Similarly, calling exp(0.0) will return 1.0 because e raised to the power of 0 is 1.0. If the input value is NaN (not a number), the function returns NaN as well.

The exp() function is often used in conjunction with other mathematical functions to compute more complex expressions. For example, to calculate the exponential function e^x + 1 for a given value of x, you could use the expression exp(x) + 1.

double cos(double x) 


The double cos(double x) function is part of the math.h header file in C, and it computes the cosine of an angle specified in radians. The input value x is the angle in radians for which the cosine is to be calculated.

Similarly, the functions sin(double x) and tan(double x) return the sine and tangent of the angle x, respectively.

The syntax of these functions is:

double cos(double x);
double sin(double x);
double tan(double x);

Here, x is the angle in radians for which the corresponding trigonometric function is to be computed. The functions return the value of the cosine, sine, or tangent of the angle x, respectively.

For example, calling cos(0.0) returns 1.0, because the cosine of 0 radians (or 0 degrees) is 1. Similarly, calling sin(0.0) returns 0.0, because the sine of 0 radians (or 0 degrees) is 0. The tan() function returns the tangent of the input angle.

These functions can be useful in a wide range of applications that involve mathematical calculations, such as physics, engineering, and computer graphics.

double acos(double x)


The double acos(double x) function is part of the math.h header file in C, and it computes the arc cosine of a given value x in radians. The input value x must be between -1 and 1 inclusive.

Similarly, the functions asin(double x) and atan(double x) compute the arc sine and arc tangent of x, respectively.

The syntax of these functions is:

double acos(double x);
double asin(double x);
double atan(double x);

Here, x is the value for which the corresponding arc trigonometric function is to be computed. The functions return the value of the arc cosine, arc sine, or arc tangent of x, respectively, in radians.

For example, calling acos(0.5) returns a value of approximately 1.0472 radians (or 60 degrees), because the cosine of 60 degrees is 0.5. Similarly, calling asin(0.5) returns a value of approximately 0.5236 radians (or 30 degrees), because the sine of 30 degrees is 0.5. The atan() function returns the arc tangent of the input value.

These functions can be useful in a wide range of applications that involve mathematical calculations, such as physics, engineering, and computer graphics.

double tanh(double x)


The double tanh(double x) function is part of the math.h header file in C, and it computes the hyperbolic tangent of a given value x.

Similarly, the functions sinh(double x) and cosh(double x) compute the hyperbolic sine and hyperbolic cosine of x, respectively.

The syntax of these functions is:

double tanh(double x);
double sinh(double x);
double cosh(double x);


Here, x is the value for which the corresponding hyperbolic trigonometric function is to be computed. The functions return the value of the hyperbolic tangent, hyperbolic sine, or hyperbolic cosine of x, respectively.

For example, calling tanh(0.5) returns a value of approximately 0.4621, because the hyperbolic tangent of 0.5 is 0.4621. Similarly, calling sinh(0.5) returns a value of approximately 0.5211, because the hyperbolic sine of 0.5 is 0.5211. The cosh() function returns the hyperbolic cosine of the input value.

These functions can be useful in a wide range of applications that involve mathematical calculations, such as statistics, physics, and engineering.
Tags

Post a Comment

0Comments
Post a Comment (0)