KEMBAR78
Math Functions in C Scanf Printf | PPT
Balochistan University of I.T &
M.S
1
Lecture 5
Mathematical Errors and Functions
input / output functions (scanf, printf)
CS106
Introduction to Programming
Methodology & Abstractions
Spring 20023
Balochistan University
of I.T & M.S
Faculty of System Sciences
Sadique Ahmed Bugti
Balochistan University of I.T &
M.S
2
Numerical Inaccuracies
• One of the problems with floating point types is
that sometimes errors occur in representing real
numbers.
• In the decimal number system, the only numbers
which can be represented exactly are those
numbers whose denominators contain only
factors of two (2) and five (5);
e.g. 1/2 = 0.5, 1/5 = 0.2, 7/10 = 0.7, 9/20 = 0.45,
3/25 = 0.12, etc.
Balochistan University of I.T &
M.S
3
Avoiding Round-Off Errors
• Use double instead of float.
– This solution provides approximately twice the
accuracy, as well as a greater range
Balochistan University of I.T &
M.S
4
Arithmetic Underflow and Overflow
• Arithmetic underflow occurs when a very small
computational result is represented as zero
– e.g. 0.00007 X 0.000005 = 0
• Arithmetic overflow is an error which results
from an attempt to represent a computational
result that is too large
– e.g. 4000 X 4000 (int) = 16,000,000
– 32,767 is the max for type int!
• Results are compiler / machine / OS dependant.
Balochistan University of I.T &
M.S
5
Math Functions
The C math library has the header file math.h, and
contains the following useful functions:
acos(d) arc cosine of d (in range 0 to pi)
asin(d) arc sine of d (in range -pi/2 to pi/2)
atan(d) arc tangent of d (in range -pi/2 to pi/2)
atan2(d1,d2) arc tangent of d1/d2 (in range -pi to pi)
cbrt(d) cube root of d
cos(d) cosine of d
cosh(d) hyperbolic cosine of d
exp(d) exponential of d
fabs(d) absolute value of d
hypot(d1,d2) sqrt(d1 * d1 + d2 * d2)
log(d) natural logarithm of d
Balochistan University of I.T &
M.S
6
Math Functions
log10(d) logarithm (base 10) of d
pow(d1,d2) 1 raised to the power d2
sin(d) sine of d
sinh(d) hyperbolic sine of d
sqrt(d) square root of d
tan(d) tangent of d
tanh(d) hyperbolic tangent of d
Balochistan University of I.T &
M.S
7
Math Functions
• A program that makes use of the C math library
would contain the statement
#include <math.h>
close to its start. In the body of the program, a
statement like
x = cos(y);
would cause the variable x to be assigned a
value which is the cosine of the value of the
variable y (both x and y should be of type
double).
Balochistan University of I.T &
M.S
8
Math Functions
• There is no built-in exponentiation operator in C:
– Normally represented by x^y, which implies x
raised to the power of y.
– Instead, there is a library function pow which
carries out this operation:
z = pow(x,y);
would cause the variable z to be assigned a value
which is x raised to the power of y.
Note: x2 is best represented as x * x
Balochistan University of I.T &
M.S
9
Math Constants
• The C math library comes with a useful set of
predefined mathematical constants:
M_PI Pi
M_PI_2 Pi divided by two
M_PI_4 Pi divided by four
M_1_PI The reciprocal of pi (1/pi)
M_SQRT2 The square root of two
M_SQRT1_2 The reciprocal of the square root of two
M_E The base of natural logarithms
Balochistan University of I.T &
M.S
10
The Output Function: printf()
• The function printf (print-eff) is used for
writing data to standard output (usually, the
terminal).
• A call to this function takes the general form:
printf(format_string, arg1, arg2, …)
– where format_string refers to a character
string containing certain required formatting
information, and arg1, arg2, etc., are
arguments that represent the individual input
data items.
Balochistan University of I.T &
M.S
11
The Output Function: printf()
• The format string consists of individual
groups of characters, with one character
group for each data input item.
Balochistan University of I.T &
M.S
12
Conversion Specifications
• The percent sign (%) symbol introduces a
conversion specification, or format. A
single conversion specification is a string
that begins with % and ends with a
conversion character.
• Explicit formatting may be included in a
conversion specification.
– Otherwise defaults are used.
Balochistan University of I.T &
M.S
13
Conversion Characters
Conversion
character
How the corresponding argument is printed
c as a character
d, i as an integer
u as an unsigned integer
e as a floating-point number, e.g. 3.141590e+00
E as a floating-point number, e.g. 3.141590E+00
f as a floating-point number, e.g. 3.141590
s as a string
% with the format %% a single % is written to
the output stream.
Balochistan University of I.T &
M.S
14
printf() Examples: Characters
& Strings
char c=‘M’, s[]=“blue moon”;
Balochistan University of I.T &
M.S
15
printf() Examples: Numbers
int i = 123;
double x = 32.17865753173828125
Balochistan University of I.T &
M.S
16
The Input Function: scanf()
• The function scanf (scan-eff) is used for reading
data from standard input (usually the terminal).
• A call to this function takes the general form:
scanf(format_string, &arg1, &arg2, …)
– where format_string refers to a character string
containing certain required formatting
information, and arg1, arg2, etc., are arguments
that represent the individual input data items.
Balochistan University of I.T &
M.S
17
The Input Function: scanf()
• The format string consists of individual groups of
characters, with one character group for each
data input item.
• In its simplest form, each character group
consists of a percent sign (%), followed by a set
of conversion characters which indicate the type
of the corresponding data item.
• The arguments are a comma-separated list of
addresses.
Balochistan University of I.T &
M.S
18
The End

Math Functions in C Scanf Printf

  • 1.
    Balochistan University ofI.T & M.S 1 Lecture 5 Mathematical Errors and Functions input / output functions (scanf, printf) CS106 Introduction to Programming Methodology & Abstractions Spring 20023 Balochistan University of I.T & M.S Faculty of System Sciences Sadique Ahmed Bugti
  • 2.
    Balochistan University ofI.T & M.S 2 Numerical Inaccuracies • One of the problems with floating point types is that sometimes errors occur in representing real numbers. • In the decimal number system, the only numbers which can be represented exactly are those numbers whose denominators contain only factors of two (2) and five (5); e.g. 1/2 = 0.5, 1/5 = 0.2, 7/10 = 0.7, 9/20 = 0.45, 3/25 = 0.12, etc.
  • 3.
    Balochistan University ofI.T & M.S 3 Avoiding Round-Off Errors • Use double instead of float. – This solution provides approximately twice the accuracy, as well as a greater range
  • 4.
    Balochistan University ofI.T & M.S 4 Arithmetic Underflow and Overflow • Arithmetic underflow occurs when a very small computational result is represented as zero – e.g. 0.00007 X 0.000005 = 0 • Arithmetic overflow is an error which results from an attempt to represent a computational result that is too large – e.g. 4000 X 4000 (int) = 16,000,000 – 32,767 is the max for type int! • Results are compiler / machine / OS dependant.
  • 5.
    Balochistan University ofI.T & M.S 5 Math Functions The C math library has the header file math.h, and contains the following useful functions: acos(d) arc cosine of d (in range 0 to pi) asin(d) arc sine of d (in range -pi/2 to pi/2) atan(d) arc tangent of d (in range -pi/2 to pi/2) atan2(d1,d2) arc tangent of d1/d2 (in range -pi to pi) cbrt(d) cube root of d cos(d) cosine of d cosh(d) hyperbolic cosine of d exp(d) exponential of d fabs(d) absolute value of d hypot(d1,d2) sqrt(d1 * d1 + d2 * d2) log(d) natural logarithm of d
  • 6.
    Balochistan University ofI.T & M.S 6 Math Functions log10(d) logarithm (base 10) of d pow(d1,d2) 1 raised to the power d2 sin(d) sine of d sinh(d) hyperbolic sine of d sqrt(d) square root of d tan(d) tangent of d tanh(d) hyperbolic tangent of d
  • 7.
    Balochistan University ofI.T & M.S 7 Math Functions • A program that makes use of the C math library would contain the statement #include <math.h> close to its start. In the body of the program, a statement like x = cos(y); would cause the variable x to be assigned a value which is the cosine of the value of the variable y (both x and y should be of type double).
  • 8.
    Balochistan University ofI.T & M.S 8 Math Functions • There is no built-in exponentiation operator in C: – Normally represented by x^y, which implies x raised to the power of y. – Instead, there is a library function pow which carries out this operation: z = pow(x,y); would cause the variable z to be assigned a value which is x raised to the power of y. Note: x2 is best represented as x * x
  • 9.
    Balochistan University ofI.T & M.S 9 Math Constants • The C math library comes with a useful set of predefined mathematical constants: M_PI Pi M_PI_2 Pi divided by two M_PI_4 Pi divided by four M_1_PI The reciprocal of pi (1/pi) M_SQRT2 The square root of two M_SQRT1_2 The reciprocal of the square root of two M_E The base of natural logarithms
  • 10.
    Balochistan University ofI.T & M.S 10 The Output Function: printf() • The function printf (print-eff) is used for writing data to standard output (usually, the terminal). • A call to this function takes the general form: printf(format_string, arg1, arg2, …) – where format_string refers to a character string containing certain required formatting information, and arg1, arg2, etc., are arguments that represent the individual input data items.
  • 11.
    Balochistan University ofI.T & M.S 11 The Output Function: printf() • The format string consists of individual groups of characters, with one character group for each data input item.
  • 12.
    Balochistan University ofI.T & M.S 12 Conversion Specifications • The percent sign (%) symbol introduces a conversion specification, or format. A single conversion specification is a string that begins with % and ends with a conversion character. • Explicit formatting may be included in a conversion specification. – Otherwise defaults are used.
  • 13.
    Balochistan University ofI.T & M.S 13 Conversion Characters Conversion character How the corresponding argument is printed c as a character d, i as an integer u as an unsigned integer e as a floating-point number, e.g. 3.141590e+00 E as a floating-point number, e.g. 3.141590E+00 f as a floating-point number, e.g. 3.141590 s as a string % with the format %% a single % is written to the output stream.
  • 14.
    Balochistan University ofI.T & M.S 14 printf() Examples: Characters & Strings char c=‘M’, s[]=“blue moon”;
  • 15.
    Balochistan University ofI.T & M.S 15 printf() Examples: Numbers int i = 123; double x = 32.17865753173828125
  • 16.
    Balochistan University ofI.T & M.S 16 The Input Function: scanf() • The function scanf (scan-eff) is used for reading data from standard input (usually the terminal). • A call to this function takes the general form: scanf(format_string, &arg1, &arg2, …) – where format_string refers to a character string containing certain required formatting information, and arg1, arg2, etc., are arguments that represent the individual input data items.
  • 17.
    Balochistan University ofI.T & M.S 17 The Input Function: scanf() • The format string consists of individual groups of characters, with one character group for each data input item. • In its simplest form, each character group consists of a percent sign (%), followed by a set of conversion characters which indicate the type of the corresponding data item. • The arguments are a comma-separated list of addresses.
  • 18.
    Balochistan University ofI.T & M.S 18 The End