GET EDUCATE

Contents
Basics of C Programming

⯈Basic C Programs

Mathematical functions in C

All the mathematical functions are available in header file called as “math.h”. These mathematical functions are used to preform mathematical operations in the program. All the mathematical functions take input as a double number and gives answer in form of double number. 

To use these mathematical functions, we need to include math.h in our program.

We will discuss the following functions:

sqrt             pow             log             log10             log2             sin             cos            tan          asin             acos             atan             floor             ceil             fmod             abs           fabs     

If you want you may skip the detailed explanation and directly goto summary part.

You may watch the video at the end of this page.

Syntax:
#include<math.h>

Some of commonly used mathematical functions

1. sqrt

  • This function finds the square root of a positive real number.
  • Prototype:
    • double sqrt(double);

Example 1:

double a = sqrt(9);

a = 3.00


Example 2:

double a = sqrt(2);

a = 1.414214


Example 3:

double a = sqrt(-2);

output: -nan (not a number)

2. pow

  • This function finds the power of base to exponent.
  • Prototype:
    • double pow(double base, double exp);

Example 1:

double y = pow(2, 4);    // y=24

y = 16.000


Example 2:

double y = pow(-1, 0.5);

output: nan (not a number)


 

log

  • This function finds the natural logarithm of a number to base – e.
  • Prototype:
    • double log(double n);

Example 1:

double y = log(10);

y = 2.302585


Example 2:

double y = log(0.6);

y = -0.510826


Example 3:

double y = log(0);

y = -inf (infinity)


Example 4:

double y = log(-1);

y = -nan (not a number)

log10

  • It gives log of a number to base – 10.
  • Prototype:
    • double log10(double);

Example 1:

double y = log10(10);

y = 1.000

log2

  • It gives logarithm of a number to base – 2.
  • Prototype:
    • double log2(double); 

Example 1:

double y = log2(4);

y = 2.000

sin

  • This function gives sin of an angle in radians.
  •  Prototype:
    • double sin(double angle_in_radians);

Example 1:

double y  = sin(3.142/4);          (pi/4)

y = 0.707179

cos

  • This function gives cos of an angle in radians.
  • Prototype:
    • double cos(double angle_in_radians);

Example 1:

double y = cos(3.142/4);

y = 0.707035

tan

  • This function gives tan of an angle in radians.
  • Prototype:
    • double tan(double angle_in_radians);

Example 1:

double y = tan(3.142/4);

y = 1.00024

asin

  • This function gives arc-sin. (sin-1 )
  • Prototype:
    • double asin(double);

Example 1:

double y = asin(1.00);

y = 1.570796


Example 2:

double y = asin(2);

y = nan (not a number), Rangle of sin is [-1, 1]

acos

  • This function gives arc-cos (cos-1 )
  • Prototype:
    • double acos(double);

Example 1:

double y = acos(1.00);

y = 0.00


Example 2:

double y = acos(2);

y = nan (not a number)

atan

  • This function gives arc-tan (tan-1 )
  • Prototype:
    • double atan(double);

Example 1:

double y = atan(1);

y = 0.785398

floor

  • This function gives the answer as the nearest integer value, less than or equal to given number.
  • Prototype:
    • double floor(double);

Example 1:

double y = floor (2.3);

y = 2.00

Since the nearest integer value less than or equal to 2.3 is 2


Example 2:

double y = floor (-2.3);

y = -3.00

Since the nearest integer value less than or equal to -2.3 is -3


Example 3:

double y = floor (2);

y = 2.00

Since the nearest integer value less than or equal to 2.0 is 2

ceil

  • This function gives answer as the nearest integer value greater than or equal to given number.
  • Prototype:
    • double ceil(double);

Example 1:

double y = ceil(2.3);

y = 3.00

Since, the nearest integer value greater than or equal to 2.3 is 3


Example 2:

double y = ceil(-2.3);

y = -2.00

Since, the nearest integer value greater than or equal to -2.3 is -2


Example 3:

double y = ceil(2);

y = 2.00

Since, the nearest integer value greater than or equal to 2.0 is 2

fmod

  • This function is used to find the remainder for the real numbers.
  • ‘%’ operator is used only for integers.
  • Prototype:
    • double fmod(double numerator, double denominator);

Example 1:

double y = fmod(3.2 , 2);

y = 1.2, The remainder obtained when 3.2 is divided by 2.


Example 2:

double y = fmod(-3.2 , 2);

y = -1.2, The remainder obtained when 3.2 is divided by 2. (Take the sign of the numerator)


Example 3:

double y = fmod(3.2 , -2);

y = 1.2, The remainder obtained when 3.2 is divided by 2. (Take the sign of the numerator)

abs

  • This function gives absolute value of the given number.
  • Prototype:
    • int abs(int);
  • This function can only be applied on integers,

Example 1:

int x = abs(-3);

x = 3


Example 2:

int x = abs(3);

x = 3

fabs

  • This function gives absolute value of a real number.
  •  Prototype:
    • double fabs(double);

Example 1:

double y = fabs(2.0);

y = 2.0


Example 2:

double y = fabs(-2.3);

y = 2.3

Summary

Function Name Purpose Example Output
sqrt
Finds square root of a number
sqrt(4)
2.00
pow
Finds pow(base, exp)
pow(3,4)
81.00
log
Finds log to base e
log(10)
2.302585
log10
Finds log to base 10
log10(10)
1.00
log2
Finds log to base 2
log2(4)
2.00
sin
Finds sin of angle in radians
sin(3.142/4)
0.707179
cos
Finds cos of an angle in radians.
cos(3.142/4)
0.707035
tan
Finds tan of an angle in radians
tan(3.142/4)
1.0024
asin
Finds sin inverse (arc sin)
asin(1)
1.570796
acos
Finds cos inverse (arc-cos)
acos(1)
0.00
atan
Finds tan inverse (arc-tan)
atan(1)
0.785398
floor
Finds nearest integer less than or equal given value
floor(-2.3)
-3.00
ceil
Finds nearest integer greater than or equal given value
ceil(2.3)
3.00
fmod
finds remainder for real numbers
fmod(3.2, 2)
1.20
abs
Finds absolute value of an integer
abs(-1)
1
fabs
Finds absolute value of a real number
fabs(-1.1)
1.1
Scroll to Top