Advanced Maths Functions

Here's a list of important math functions in the C standard library along with sample code and explanations:

  1. sqrt() - Square Root:

    • Calculates the square root of a number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 25.0;
        double result = sqrt(num);
        printf("Square root of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. pow() - Power:

    • Calculates the power of a number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double base = 2.0;
        double exponent = 3.0;
        double result = pow(base, exponent);
        printf("%.2f raised to the power of %.2f is %.2f\n", base, exponent, result);
        return 0;
    }
  1. fabs() - Absolute Value:

    • Returns the absolute value of a floating-point number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = -7.5;
        double result = fabs(num);
        printf("Absolute value of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. ceil() - Ceiling:

    • Rounds up to the nearest integer.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 4.3;
        double result = ceil(num);
        printf("Ceiling of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. floor() - Floor:

    • Rounds down to the nearest integer.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 4.7;
        double result = floor(num);
        printf("Floor of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. sin(), cos(), tan() - Trigonometric Functions:

    • Calculate sine, cosine, and tangent of an angle (in radians).
    #include <stdio.h>
    #include <math.h>

    int main() {
        double angle = 30.0;
        double radian = angle * (M_PI / 180.0);
        printf("sin(%.2f) = %.2f\n", angle, sin(radian));
        printf("cos(%.2f) = %.2f\n", angle, cos(radian));
        printf("tan(%.2f) = %.2f\n", angle, tan(radian));
        return 0;
    }
  1. log() - Natural Logarithm:

    • Calculates the natural logarithm of a number.
    #include <stdio.h>
    #include <math.h>

    int main() {
        double num = 2.0;
        double result = log(num);
        printf("Natural log of %.2f is %.2f\n", num, result);
        return 0;
    }
  1. exp() - Exponential Function:

    • Calculates the exponential function (e^x).
    #include <stdio.h>
    #include <math.h>

    int main() {
        double exponent = 2.0;
        double result = exp(exponent);
        printf("e raised to the power of %.2f is %.2f\n", exponent, result);
        return 0;
    }

These are just a few examples of the many math functions available in the C standard library. Make sure to include the <math.h> header in your C program to use these functions.

0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.