SlideShare a Scribd company logo
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

More Related Content

PPT
Intermediate code generation
PPT
Code generator
PPTX
Intermediate code
PPTX
Optimization of basic blocks
PPTX
COMPILER DESIGN AND CONSTRUCTION
PPT
Intermediate code generation (Compiler Design)
PPTX
Dag representation of basic blocks
PPTX
Aspect extraction using conditional random fields [SentiRuEval]
Intermediate code generation
Code generator
Intermediate code
Optimization of basic blocks
COMPILER DESIGN AND CONSTRUCTION
Intermediate code generation (Compiler Design)
Dag representation of basic blocks
Aspect extraction using conditional random fields [SentiRuEval]

What's hot (20)

PPTX
Lecture 12 intermediate code generation
PPT
Intermediate code generation
PDF
Intermediate code generation in Compiler Design
PPTX
Three address code In Compiler Design
DOC
Ocs752 unit 1
DOC
Ocs752 unit 3
DOC
Ocs752 unit 2
PPTX
Three Address code
PPTX
Basic blocks - compiler design
PDF
Fast coputation of Phi(x) inverse
PPT
Introduction To Algorithm [2]
PDF
Digital communication lab lectures
DOC
Ocs752 unit 4
PDF
The Basic Model of Computation
PPT
Chapter 6 intermediate code generation
PDF
Programming with matlab session 3 notes
PDF
Intermediate code generation
DOC
Ocs752 unit 5
DOCX
PPTX
Intermediate code generation1
Lecture 12 intermediate code generation
Intermediate code generation
Intermediate code generation in Compiler Design
Three address code In Compiler Design
Ocs752 unit 1
Ocs752 unit 3
Ocs752 unit 2
Three Address code
Basic blocks - compiler design
Fast coputation of Phi(x) inverse
Introduction To Algorithm [2]
Digital communication lab lectures
Ocs752 unit 4
The Basic Model of Computation
Chapter 6 intermediate code generation
Programming with matlab session 3 notes
Intermediate code generation
Ocs752 unit 5
Intermediate code generation1
Ad

Similar to Math Functions in C Scanf Printf (20)

PPTX
COM1407: Type Casting, Command Line Arguments and Defining Constants
PPT
Chapter-2 is for tokens in C programming
PPT
lecture2.ppt
PPT
Data Types in C
PDF
lec1).pdfbjkbvvttytxtyvkuvvtryccjbvuvibu
PPT
lecture2.ppt FOR C PROGRAMMING AND DATA S TRUCT
PPT
lecture-ON-C.ppt BASIC WITH DEPTH CONTENT
PPT
lecture2.ppt...............................................
PPT
basic of C programming (token, keywords)lecture2.ppt
PPT
PDF
C standard library functions
PDF
C programming part2
PDF
C programming part2
PDF
C programming part2
PDF
Control Flow Statements and Datatypes in C
PPT
Mba Ebooks ! Edhole
PPT
Mba Ebooks ! Edhole
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
PPTX
Lecture 5 – Computing with Numbers (Math Lib).pptx
COM1407: Type Casting, Command Line Arguments and Defining Constants
Chapter-2 is for tokens in C programming
lecture2.ppt
Data Types in C
lec1).pdfbjkbvvttytxtyvkuvvtryccjbvuvibu
lecture2.ppt FOR C PROGRAMMING AND DATA S TRUCT
lecture-ON-C.ppt BASIC WITH DEPTH CONTENT
lecture2.ppt...............................................
basic of C programming (token, keywords)lecture2.ppt
C standard library functions
C programming part2
C programming part2
C programming part2
Control Flow Statements and Datatypes in C
Mba Ebooks ! Edhole
Mba Ebooks ! Edhole
Lecture 5 – Computing with Numbers (Math Lib).pptx
Lecture 5 – Computing with Numbers (Math Lib).pptx
Ad

More from yarkhosh (6)

PPT
Decisions in C or If condition
PPT
Arithmetic Operator in C
PPT
Algorithm
PPT
Operators in C
PPT
Escape Sequences and Variables
PPT
Introduct To C Language Programming
Decisions in C or If condition
Arithmetic Operator in C
Algorithm
Operators in C
Escape Sequences and Variables
Introduct To C Language Programming

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Nekopoi APK 2025 free lastest update
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administration Chapter 2
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Introduction to Artificial Intelligence
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
PTS Company Brochure 2025 (1).pdf.......
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Reimagine Home Health with the Power of Agentic AI​
Nekopoi APK 2025 free lastest update
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administration Chapter 2
Adobe Illustrator 28.6 Crack My Vision of Vector Design
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Choose the Right IT Partner for Your Business in Malaysia
Introduction to Artificial Intelligence
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Navsoft: AI-Powered Business Solutions & Custom Software Development
Wondershare Filmora 15 Crack With Activation Key [2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms I-SECS-1021-03
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Odoo Companies in India – Driving Business Transformation.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
PTS Company Brochure 2025 (1).pdf.......
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free

Math Functions in C Scanf Printf

  • 1. 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
  • 2. 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.
  • 3. 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
  • 4. 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.
  • 5. 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
  • 6. 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
  • 7. 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).
  • 8. 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
  • 9. 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
  • 10. 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.
  • 11. 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.
  • 12. 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.
  • 13. 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.
  • 14. Balochistan University of I.T & M.S 14 printf() Examples: Characters & Strings char c=‘M’, s[]=“blue moon”;
  • 15. Balochistan University of I.T & M.S 15 printf() Examples: Numbers int i = 123; double x = 32.17865753173828125
  • 16. 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.
  • 17. 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.
  • 18. Balochistan University of I.T & M.S 18 The End