Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

C Math

C Programming allows us to perform mathematical operations through the functions defined in


<math.h> header file. The <math.h> header file contains various methods for performing
mathematical operations such as sqrt(), pow(), ceil(), floor() etc.

C Math Functions

Function Description Example

sqrt(4.0) is 2.0
sqrt(x) square root of x sqrt(10.0) is
3.162278

exp(1.0) is 2.718282
exp(x) exponential (ex)
exp(4.0) is 54.598150

log(2.0) is 0.693147
log(x) natural logarithm of x (base e)
log(4.0) is 1.386294

log10(10.0) is 1.0
log10(x) logarithm of x (base 10)
log10(100.0) is 2.0

fabs(2.0) is 2.0
fabs(x) absolute value of x
fabs(-2.0) is 2.0

ceil function returns the integer value just ceil(9.2) is 10.0


ceil(x)
greater than the given rational value. ceil(-9.2) is -9.0

floor function returns the integer value just lesser floor(9.2) is 9.0
floor(x)
than the given rational value. floor(-9.2) is -10.0

pow(x,y) x raised to power y (xy) pow(2,2) is 4.0

fmod(13.657, 2.333)
fmod(x) remainder of x/y as floating-point number
is 1.992
Function Description Example

sin(x) sine of x (x in radian) sin(0.0) is 0.0

cos(x) cosine of x (x in radian) cos(0.0) is 1.0

tan(x) tangent of x (x in radian) tan(0.0) is 0.0

Example:

#include<stdio.h>
#include <math.h>
int main()
{
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%f",fabs(-12));
return 0;
}

Output:

4.000000
4.000000
3.000000
3.000000
4.000000
2.645751
16.000000
27.000000
12.000000

Expressions:

An expression in C is a combination of operands and operators – it


computes a single value stored in a variable. The operator denotes the
action or operation to be performed. The operands are the items to
which we apply the operation.

C Expression Evaluation
In the C programming language, an expression is evaluated based on the operator precedence

and associativity. When there are multiple operators in an expression, they are evaluated

according to their precedence and associativity. The operator with higher precedence is

evaluated first and the operator with the least precedence is evaluated last.

10 + 4 * 3 / 2
4 * 3 ====> 12
12 / 2 ===> 6
10 + 6 ===> 16
The expression is evaluated to 16.

Evaluation of expression Description of each operation

6*2/( 2+1 * 2/3 +6) +8 * (8/4) An expression is given.

6*2/(2+2/3 + 6) + 8 * (8/4) 2 is multiplied by 1, giving value 2.

6*2/(2+0+6) + 8 * (8/4) 2 is divided by 3, giving value 0.

6*2/ 8+ 8 * (8/4) 2 is added to 6, giving value 8.

6*2/8 + 8 * 2 8 is divided by 4, giving value 2.

12/8 +8 * 2 6 is multiplied by 2, giving value 12.

1+8*2 12 is divided by 8, giving value 1.

1 + 16 8 is multiplied by 2, giving value 16.

17 1 is added to 16, giving value 17.

operator precedence and associativity in C helps in determining which


operators will be given priority when there are multiple operators in the
expression. It is very common to have multiple operators in C language and the
compiler first evaluates the operator with higher precedence.

Category Operator Associativity

Postfix () [] -> . ++ - - Left to right

Unary + - ! ~ ++ - - (type)* & sizeof Right to left

Multiplicative */% Left to right

Additive +- Left to right


Shift << >> Left to right

Relational < <= > >= Left to right

Equality == != Left to right

Bitwise AND & Left to right

Bitwise XOR ^ Left to right

Bitwise OR | Left to right

Logical AND && Left to right

Logical OR || Left to right

Conditional ?: Right to left

Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left

Comma , Left to right

You might also like