Cover Image for C Math Functions
130 views

C Math Functions

C provides a set of math functions as part of the standard library, which are declared in the <math.h> header. These functions allow you to perform various mathematical operations, including basic arithmetic, trigonometry, logarithms, exponentiation, and more. Here are some commonly used math functions in C:

  1. Basic Arithmetic Functions:
  • int abs(int x): Returns the absolute value of an integer x.
  • double fabs(double x): Returns the absolute value of a floating-point number x.
  • double sqrt(double x): Returns the square root of a number x.
  • double pow(double x, double y): Returns x raised to the power of y.
  1. Trigonometric Functions:
  • double sin(double x): Returns the sine of an angle x (in radians).
  • double cos(double x): Returns the cosine of an angle x (in radians).
  • double tan(double x): Returns the tangent of an angle x (in radians).
  • double atan(double x): Returns the arctangent of a number x (in radians).
  • double atan2(double y, double x): Returns the arctangent of the ratio y/x (in radians).
  1. Logarithmic Functions:
  • double log(double x): Returns the natural logarithm of a number x.
  • double log10(double x): Returns the base-10 logarithm of a number x.
  • double log2(double x): Returns the base-2 logarithm of a number x.
  1. Exponential Functions:
  • double exp(double x): Returns the exponential value of x.
  • double exp2(double x): Returns 2 raised to the power of x.
  1. Rounding Functions:
  • double floor(double x): Returns the largest integer less than or equal to x.
  • double ceil(double x): Returns the smallest integer greater than or equal to x.
  • double round(double x): Rounds x to the nearest integer value.
  1. Other Functions:
  • double fmod(double x, double y): Returns the remainder of x divided by y.
  • double hypot(double x, double y): Returns the hypotenuse length of a right triangle with sides of length x and y.
  • double copysign(double x, double y): Returns x with the sign of y.

These are just some of the commonly used math functions available in C. To use these functions, include the <math.h> header and link your program with the math library (e.g., by adding -lm during compilation on Unix-like systems).

Here’s an example of using some of these functions:

C
#include <stdio.h>
#include <math.h>

int main() {
    double x = 2.0;
    double y = 3.0;

    printf("Square root of %f is %f\n", x, sqrt(x));
    printf("2 raised to the power of %f is %f\n", y, exp2(y));
    printf("The sine of %f radians is %f\n", x, sin(x));

    return 0;
}

This example demonstrates the use of sqrt(), exp2(), and sin() functions to perform basic mathematical operations.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS