You are on page 1of 16

CSE101-lec#13

Math library functions

©LPU CSE101 C Programming


Outline
• Math library functions

©LPU CSE101 C Programming


Absolute Value Function in C
• The abs function calculates the absolute
value of its argument. It’s the C analogue of
the mathematical function
a(y) = | y |
(the absolute value function)
a= abs(y);

©LPU CSE101 C Programming


Absolute Value Function in C

fabs(-2.5) returns 2.5


abs(-2) returns 2
abs(-1) returns 1
abs(0) returns 0
abs(1) returns 1
abs(2) returns 2
fabs(2.5) returns 2.5

©LPU CSE101 C Programming


Absolute Value Function in C #3
We say “abs of -2 evaluates to 2” or “abs of -2
returns 2.”

Note that the function named abs calculates the


absolute value of an int argument, and fabs
calculates the absolute value of a float argument.

©LPU CSE101 C Programming


Function Call in Programming
In programming, the use of a function in an
expression is referred to as a call.
We say that the statement
printf("%d\n", abs(-2));
invokes or calls the function abs;
the statement passes an argument of -2 to
the function; the function abs returns a
value of 2.

©LPU CSE101 C Programming


Math Function vs Programming
Function
An important distinction between a function in
mathematics and a function in programming:
a function in mathematics is simply a
definition (“this name means that
expression”), while a function in
programming is an action (“this name means
execute that sequence of statements”).

©LPU CSE101 C Programming


C Standard Library
• Every implementation of C comes with a
standard library of predefined functions.
• Note that, in programming, a library is a
collection of functions.
• The functions that are common to all
versions of C are known as the C
Standard Library.

©LPU CSE101 C Programming


Math Library Functions
• Math library functions
– perform common mathematical calculations
– #include <math.h>
• Format for calling maths functions
– functionName( argument );
• If multiple arguments, use comma-separated list
• Example:
printf( "%.2f", sqrt( 900.0 ) );
• Calls function sqrt, which returns the square root of its argument
• All math functions return data type double
– Arguments may be constants, variables, or expressions

©LPU CSE101 C Programming


Math Library Functions
Function Description Example
sqrt( x ) square root of x sqrt( 900.0 ) is 30.0
sqrt( 9.0 ) is 3.0

exp( x ) exponential function ex exp( 1.0 ) is 2.718282


exp( 2.0 ) is 7.389056
log( x ) natural logarithm of x log( 2.718282 ) is 1.0
log( 7.389056 ) is 2.0
(base e)
log10( x ) logarithm of x (base 10) log10( 1.0 ) is 0.0
log10( 10.0 ) is 1.0
log10( 100.0 ) is 2.0
fabs( x ) absolute value of x fabs( 5.0 ) is 5.0
fabs( 0.0 ) is 0.0
fabs( -5.0 ) is 5.0
ceil( x ) rounds x to the smallest ceil( 9.2 ) is 10.0
ceil( -9.8 ) is -9.0
integer not less than x
floor( x ) rounds x to the largest floor( 9.2 ) is 9.0
floor( -9.8 ) is -10.0
integer not greater than x
pow( x, y ) x raised to power y (xy) pow( 2, 7 ) is 128.0
pow( 9, .5 ) is 3.0
fmod( x, y ) remainder of x/y as a fmod( 13.657, 2.333 ) is
1.992
floating point number
sin( x ) trigonometric sine of x sin( 0.0 ) is 0.0
(x in radians)
cos( x ) trigonometric cosine of x cos( 0.0 ) is 1.0
(x in radians)
tan( x ) trigonometric tangent of x tan( 0.0 ) is 0.0
(x in radians)
©LPU CSE101 C Programming
Math Library Functions: pow()
 The power function, pow(x,y), calculates xy; that is, the
value of
pow(x,y) = xy.
 pow(2,3)= 2³ = 8.0 and pow(2.5,3) = 15.625.
 The function pow is of the type double or that the function
pow returns a value of the type double.
 x and y are called the parameters (or arguments) of the
function pow.
 Function pow has two parameters.

©LPU CSE101 C Programming


sqrt()
 The square root function, sqrt(x),
calculates the non-negative square root of x for x >= 0.0
sqrt(2.25) is 1.5
sqrt(25) is 5.0
 The function sqrt is of the type double and has only one
parameter.

©LPU CSE101 C Programming


fabs()
• fabs calculates the absolute value of a float
argument.
fabs(2.25) is 2.25
fabs(-25.0) is 25.0
The function fabs is of the type double
and has only one parameter.

©LPU CSE101 C Programming


floor()
 The floor function, floor, calculates the largest
whole number that is not greater than x.
floor(48.79) is 48.0
floor(48.03) is 48.0
floor(47.79) is 47.0
 The function floor is of the type double and has
only one parameter.

©LPU CSE101 C Programming


ceil()
 The ceil function, ceil, calculates the smallest
whole number that is not less than x.
ceil(48.79) is 49
ceil(48.03) is 49
ceil(47.79) is 48
 The function ceil is of the type double and has
only one parameter.

©LPU CSE101 C Programming


©LPU CSE101 C Programming

You might also like