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

What's hot

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
Iffat Anjum
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
RamchandraRegmi
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
Kuppusamy P
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
Shine Raj
 
Ocs752 unit 1
Ocs752   unit 1Ocs752   unit 1
Ocs752 unit 1
mgrameshmail
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
mgrameshmail
 
Ocs752 unit 2
Ocs752   unit 2Ocs752   unit 2
Ocs752 unit 2
mgrameshmail
 
Three Address code
Three Address code Three Address code
Three Address code
Pooja Dixit
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
hmnasim15
 
Fast coputation of Phi(x) inverse
Fast coputation of Phi(x) inverseFast coputation of Phi(x) inverse
Fast coputation of Phi(x) inverse
John Cook
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]ecko_disasterz
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lectures
marwaeng
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
mgrameshmail
 
The Basic Model of Computation
The Basic Model of ComputationThe Basic Model of Computation
The Basic Model of Computation
DipakKumar122
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
Vipul Naik
 
Programming with matlab session 3 notes
Programming with matlab session 3 notesProgramming with matlab session 3 notes
Programming with matlab session 3 notes
Infinity Tech Solutions
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
Akshaya Arunan
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
mgrameshmail
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 

What's hot (20)

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
Ocs752 unit 1
Ocs752   unit 1Ocs752   unit 1
Ocs752 unit 1
 
Ocs752 unit 3
Ocs752   unit 3Ocs752   unit 3
Ocs752 unit 3
 
Ocs752 unit 2
Ocs752   unit 2Ocs752   unit 2
Ocs752 unit 2
 
Three Address code
Three Address code Three Address code
Three Address code
 
Basic blocks - compiler design
Basic blocks - compiler designBasic blocks - compiler design
Basic blocks - compiler design
 
Fast coputation of Phi(x) inverse
Fast coputation of Phi(x) inverseFast coputation of Phi(x) inverse
Fast coputation of Phi(x) inverse
 
Introduction To Algorithm [2]
Introduction To Algorithm [2]Introduction To Algorithm [2]
Introduction To Algorithm [2]
 
Digital communication lab lectures
Digital communication lab  lecturesDigital communication lab  lectures
Digital communication lab lectures
 
Ocs752 unit 4
Ocs752   unit 4Ocs752   unit 4
Ocs752 unit 4
 
The Basic Model of Computation
The Basic Model of ComputationThe Basic Model of Computation
The Basic Model of Computation
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Programming with matlab session 3 notes
Programming with matlab session 3 notesProgramming with matlab session 3 notes
Programming with matlab session 3 notes
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Ocs752 unit 5
Ocs752   unit 5Ocs752   unit 5
Ocs752 unit 5
 
Theory1&amp;2
Theory1&amp;2Theory1&amp;2
Theory1&amp;2
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 

Similar to Math Functions in C Scanf Printf

VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
Syed Mustafa
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
Syed Mustafa
 
Lk module3
Lk module3Lk module3
Lk module3
Krishna Nanda
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
JinTaek Seo
 
Interactive Transmission System Computation Unit
Interactive Transmission System Computation UnitInteractive Transmission System Computation Unit
Interactive Transmission System Computation Unit
mayankraj0805
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
Bharat Kalia
 
Cbasic
CbasicCbasic
Cbasic
rohitladdu
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013
ericupnorth
 
Functions
FunctionsFunctions
Functions
Swarup Boro
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
Hemantha Kulathilake
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
SumitSingh813090
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
KrishanPalSingh39
 
Automated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWSAutomated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWS
Abhik Roychoudhury
 
Basics of c
Basics of cBasics of c
Basics of c
DebanjanSarkar11
 
C++ Language
C++ LanguageC++ Language
C++ Language
Syed Zaid Irshad
 
Java cơ bản java co ban
Java cơ bản java co ban Java cơ bản java co ban
Java cơ bản java co ban
ifis
 

Similar to Math Functions in C Scanf Printf (20)

VTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in CVTU PCD Model Question Paper - Programming in C
VTU PCD Model Question Paper - Programming in C
 
answer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcdanswer-model-qp-15-pcd13pcd
answer-model-qp-15-pcd13pcd
 
Lk module3
Lk module3Lk module3
Lk module3
 
01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo01 stack 20160908_jintaek_seo
01 stack 20160908_jintaek_seo
 
Interactive Transmission System Computation Unit
Interactive Transmission System Computation UnitInteractive Transmission System Computation Unit
Interactive Transmission System Computation Unit
 
Programming in C Basics
Programming in C BasicsProgramming in C Basics
Programming in C Basics
 
Cbasic
CbasicCbasic
Cbasic
 
talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013talk at Virginia Bioinformatics Institute, December 5, 2013
talk at Virginia Bioinformatics Institute, December 5, 2013
 
Functions
FunctionsFunctions
Functions
 
COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants COM1407: Type Casting, Command Line Arguments and Defining Constants
COM1407: Type Casting, Command Line Arguments and Defining Constants
 
Cbasic
CbasicCbasic
Cbasic
 
Cbasic
CbasicCbasic
Cbasic
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
C SLIDES PREPARED BY M V B REDDY
C SLIDES PREPARED BY  M V B REDDYC SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY M V B REDDY
 
presentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptxpresentation_data_types_and_operators_1513499834_241350.pptx
presentation_data_types_and_operators_1513499834_241350.pptx
 
Automated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWSAutomated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWS
 
functions
functionsfunctions
functions
 
Basics of c
Basics of cBasics of c
Basics of c
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
Java cơ bản java co ban
Java cơ bản java co ban Java cơ bản java co ban
Java cơ bản java co ban
 

More from yarkhosh

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
yarkhosh
 
Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in C
yarkhosh
 
Algorithm
AlgorithmAlgorithm
Algorithm
yarkhosh
 
Operators in C
Operators in COperators in C
Operators in C
yarkhosh
 
Data Types in C
Data Types in CData Types in C
Data Types in C
yarkhosh
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
yarkhosh
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programming
yarkhosh
 

More from yarkhosh (7)

Decisions in C or If condition
Decisions in C or If conditionDecisions in C or If condition
Decisions in C or If condition
 
Arithmetic Operator in C
Arithmetic Operator in CArithmetic Operator in C
Arithmetic Operator in C
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Operators in C
Operators in COperators in C
Operators in C
 
Data Types in C
Data Types in CData Types in C
Data Types in C
 
Escape Sequences and Variables
Escape Sequences and VariablesEscape Sequences and Variables
Escape Sequences and Variables
 
Introduct To C Language Programming
Introduct To C Language ProgrammingIntroduct To C Language Programming
Introduct To C Language Programming
 

Recently uploaded

E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
Aftab Hussain
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 

Recently uploaded (20)

E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Graspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code AnalysisGraspan: A Big Data System for Big Code Analysis
Graspan: A Big Data System for Big Code Analysis
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
Vitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdfVitthal Shirke Java Microservices Resume.pdf
Vitthal Shirke Java Microservices Resume.pdf
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 

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