& Control Flow of Program

Similar documents
CS101 Introduction to computing Floating Point Numbers

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Systems I. Floating Point. Topics IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties

System Programming CISC 360. Floating Point September 16, 2008

Floating Point Puzzles The course that gives CMU its Zip! Floating Point Jan 22, IEEE Floating Point. Fractional Binary Numbers.

Giving credit where credit is due

Giving credit where credit is due

Floating Point January 24, 2008

CS429: Computer Organization and Architecture

Chapter 2 Float Point Arithmetic. Real Numbers in Decimal Notation. Real Numbers in Decimal Notation

Floating Point : Introduction to Computer Systems 4 th Lecture, May 25, Instructor: Brian Railing. Carnegie Mellon

Floating Point. CSE 238/2038/2138: Systems Programming. Instructor: Fatma CORUT ERGİN. Slides adapted from Bryant & O Hallaron s slides

Bryant and O Hallaron, Computer Systems: A Programmer s Perspective, Third Edition. Carnegie Mellon

Foundations of Computer Systems

Floating Point Puzzles. Lecture 3B Floating Point. IEEE Floating Point. Fractional Binary Numbers. Topics. IEEE Standard 754

Today: Floating Point. Floating Point. Fractional Binary Numbers. Fractional binary numbers. bi bi 1 b2 b1 b0 b 1 b 2 b 3 b j

Floating point. Today. IEEE Floating Point Standard Rounding Floating Point Operations Mathematical properties Next time.

Computer Organization: A Programmer's Perspective

Data Representation Floating Point

The course that gives CMU its Zip! Floating Point Arithmetic Feb 17, 2000

Floating Point Numbers

Floating Point Numbers

Floating Point Numbers

Data Representation Floating Point

Floating point. Today! IEEE Floating Point Standard! Rounding! Floating Point Operations! Mathematical properties. Next time. !

Floating Point (with contributions from Dr. Bin Ren, William & Mary Computer Science)

Integer Representation Floating point Representation Other data types

Type Conversion. and. Statements

Simple Data Types in C. Alan L. Cox

Computer System and programming in C

1.3b Type Conversion

Representing and Manipulating Floating Points

Floa.ng Point : Introduc;on to Computer Systems 4 th Lecture, Sep. 10, Instructors: Randal E. Bryant and David R.

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Operators and Expressions:

Representing and Manipulating Floating Points. Jo, Heeseung

Representing and Manipulating Floating Points

Representing and Manipulating Floating Points. Computer Systems Laboratory Sungkyunkwan University

Representing and Manipulating Floating Points

Floats are not Reals. BIL 220 Introduc6on to Systems Programming Spring Instructors: Aykut & Erkut Erdem

Data Type Fall 2014 Jinkyu Jeong

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

CS222: Processor Design

COMP 208 Computers in Engineering

Programming for Engineers Iteration

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

ANSI C Programming Simple Programs

IEEE 754 Floating-Point Format

Computer Architecture Chapter 3. Fall 2005 Department of Computer Science Kent State University

BRANCHING if-else statements

Dept. of CSE, IIT KGP

Integers Sep 3, 2002

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

Systems Programming and Computer Architecture ( )

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Basic Assignment and Arithmetic Operators

ICS Instructor: Aleksandar Kuzmanovic TA: Ionut Trestian Recitation 2

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Systems 1. Integers. Unsigned & Twoʼs complement. Addition, negation, multiplication

Data Representation Floating Point

CS367 Test 1 Review Guide

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Integers. Dr. Steve Goddard Giving credit where credit is due

Number Representation & Conversion

Flow Chart. The diagrammatic representation shows a solution to a given problem.

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

CS222: Mid Semester Exam: Model

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

COMP2611: Computer Organization. Data Representation

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

Data types, variables, constants

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Arithmetic type issues

CS101 Introduction to computing Problem Solving (Computing)

3.5 Floating Point: Overview

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Floating Point Arithmetic

15213 Recitation 2: Floating Point

C-LANGUAGE CURRICULAM

Finite arithmetic and error analysis

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Control Statements. If Statement if statement tests a particular condition

Computers in Engineering. Moving From Fortran to C Part 2 Michael A. Hawker

Floating Point II, x86 64 Intro

Fundamentals of Programming Session 7

Computers in Engineering COMP 208. Roots of a Quadratic in C 10/25/2007. Moving From Fortran to C Part 2 Michael A. Hawker

Topics of this Slideset. CS429: Computer Organization and Architecture. Encoding Integers: Unsigned. C Puzzles. Integers

CS429: Computer Organization and Architecture

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

C Programming Multiple. Choice

Fundamentals of Programming

Control Structures in Java if-else and switch

Transcription:

CS101 Introduction to computing Recap of Floating Point & Control Flow of Program A. Sahu and S. V.Rao Dept of Comp. Sc. & Engg. Indian Institute of Technology Guwahati 1

Outline Recap Operation on Floating Point Conversions and type casting in C Program flow control If else l Switch case Looping : while, for, do while Problem Solving 2

IEEE 754 standard Single precision numbers 1 8 23 0 1011 0101 1101 0110 1011 0001 0110 110 S E F Double precision numbers 1 11 20+32 0 1011 0101 111 1101 0110 1011 0001 0110 S E F 1011 0001 0110 1100 1011 0101 1101 0110

Density of int vs float Int : 32 bit Exponent Mantissa Float : 32 bit Number of number can be represented Both the cases (float, int) : 2 32 Range int ( 2 31 to 2 31 1) float Large ±(2 ( 2 23 ) x 2 127 Small± 1 x 2 126 50% of float numbers are Small (less then ±1 ) 4

Expressible Numbers(int and float) Expressible integers overflow + overflow 2 31 0 2 31 1 Expressible Float underflow + underflow overflow (1 2 24 )x2 128 + overflow 0 0.5x2 127 0.5x2 127 (1 2 24 )x2 128

Density of 32 bit float SP Fraction/mantissa is 23 bit Number of different number can be stored for particular value of exponent Assume for exp=1, 2 23 =8x1024x1024 8x10 6 Between 1 2 we can store 8x10 6 numbers Similarly for exp=2, between 2 4, 8x10 6 number of number can be stored for exp=3, between 4 8, 8x10 6 number of number can be stored for exp=4, between 8 16, 8x10 6 number of number can be stored 6

Similarly Density of 32 bit float SP for exp=23, between 2 22 22 23, 810 8x10 6 number of number can be stored for exp=24, bt between 2 23 22 24, 810 8x10 6 number of number can be stored OK for exp=25, between 2 24 22 25, 8x10 6 number of number can be stored 2 24 2 25 >8 x10 6 BAD for exp=127, between 2 126 22 127, 8x10 6 number of number can be stored WROST 7

Density of 32 bit float SP 2 23 =8x1024x1024 8x10 6 0 1 2 4 8 16 8

Floating Point in C C Guarantees Two/Three Levels float single precision double long double double precision quad precision

Mathematical Properties of FP Add Compare to those of Abelian Group Closed Cosedunder addition? But may generate infinity or NaN Commutative? Associative? Overflow and inexactness of rounding YES YES NO 0 is additive identity? YES Every y element has additive inverse Except for infinities & NaNs ALMOST Monotonicity a b a+c b+c? ALMOST Except for infinities & NaNs

Math. Properties of FP Mult Compare to Commutative Ring Closed under multiplication? But may generate infinity or NaN YES Multiplication Commutative? YES Multiplication M li is Associative? NO Possibility of overflow, inexactness of rounding 11 is multiplicative identity? YES Multiplication distributes over addition? NO Possibility of overflow, inexactness of rounding Monotonicity a b & c 0 a *c b *c? ALMOST Except for infinities & NaNs

Type Casting Some languages are strictly type, addition of two different type of object Result an compilation error ( Example ML) You can not add a mango and an apple In C we have : auto up gradation and demotion (Implicit type casting) of type int I; float F; Explicit type casting I=F; //auto demotion F=I; //auto promotion I=(int) I(int) F; //manual demotion F=(float) I; //manual promotion

Type Casting Floating Point Casting between int, float, and double changes numeric values double or float to int Truncates fractional part Like rounding toward zero Not defined when out of range Generally saturates to TMin or TMax int to double Exact conversion, as long as int has 53 bit word size int to float Will round according to rounding mode

Answers to Floating Point Puzzles int x = ; float f = ; Assume neither d nor f is NAN double d = ; x==(int)(float)x x==(int)(double)x f==(float)(double)f d==(float)d Comparison Result? Comparison Result? Comparison Result? Comparison Result? f==-(-f); f) Comparison Result?

Answers to Floating Point Puzzles int x = ; float f = ; Assume neither d nor f is NAN double d = ; x==(int)(float)x No: 24 bit Fraction (1+23) x==(int)(double)x f==(float)(double)f d==(float)d Yes: 53 bit Fraction(1+52) Yes: increases precision No: loses precision f==-(-f); f) Yes: Just change sign bit

Answers to Floating Point Puzzles int x = ; float f = ; Assume neither d nor f is NAN double d = ; 2/3 == 2/3.0 Comparison Result? d < 0.0 ((d*2)<0.0) Comparison Result? d > f -f > -d d * d >= 0.0 Comparison Result? Comparison Result? (d+f)-d d == f Comparison Result?

Answers to Floating Point Puzzles int x = ; float f = ; Assume neither d nor f is NAN double d = ; 2/3 == 2/3.0 No: 2/3 == 0 d < 0.0 ((d*2) < 0.0) Yes! d > f -f > -d d * d >= 0.0 Yes! Yes! (d+f)-d d == f No: Not associative iti

Control Flow of Program if-else else, if-goto goto, switch- case & for, while, do-while 18

Structured Programming All programs can be written in terms of only three control structures Sequence, selection and repetition The sequence structure Unless otherwise directed, the statements are executed in the order in which they are written. The selection structure Used to choose among alternative courses of action. The repetition structure Allows an action to be repeated while some condition remains true.

Sequential Execution Statement 1 Statement 2... Statement n

Compute the resonant frequency of an RLC circuit #include <stdio.h> #include <math.h> h> int main() { double l, c, omega, f; printf("enter inductance in mh: "); //S1 scanf("%lf", &l); //S2 printf("enter capacitance in microf: "); //S4 scanf("%lf", &c); //S5 omega = 1.0/sqrt((l.0/1000)*(c/1000000)); //S6 f = omega / (2 * M_PI); //S7 printf("resonant freq: %.2f\n", f); //S8 return 0; //S9 }

Selective Execution : Flow chart (only if) Boolean Expression true Statement 1 Statement S

Selective Execution : Flow chart true Boolean Expr false statement 1 statement 2

Selection: the if else statement if ( condition ){ } else { } statement(s)/*if clause */ statement(s) t t( /*else l clause */

Nesting of if else Statements if ( condition 1 ) { statement(s) } else if ( condition 2 ) { statement(s) }... /* more else clauses may be here */ else { statement(s) /* the default case */ }

Bad Example : 2 if 1 else if ( n > 0 ) if ( a > b ) z=a; if ( n > 0 ) else { z=b; if (a> b) if ( n > 0 ) if ( a > b ) } else z=a; z=a; z=b; else z=b; Indentation will not ensure result : else match with closest if Code of Red box behaves like Code of Green box

In Assembly language: No if else Assembly language No support for [if else, No for loop, No while loop] All higher construct get implemented using if and goto statement goto statement uses Label If else get converted to if goto if(a>b) goto L1: if(a> b ) z=b; z=a; else z=b; goto L2: NextStmt; L1: z=a; L2: Next stmt 27

In Assembly language: No if else if(a>b) goto L1: z=b; L1: L2: goto L2: z=a; Next stmt 28

Multi way if else : switch case If else : two way, if part and else part To make it multi way: nested if else Confusing, lengthy C language provide Switch case Multi way selection Range multi way selection 29

The switch Multiple Selection Structure switch Useful when expression is tested for multiple values Consists of a series of case labels and an optional default case break is (almost always) necessary switch (<expression>){ case <Value1> : <Action/Stmts for Value1>; break; case <Value2> : <Action/Stmts for Value2>; break;... default: <Action/Stmts for DefaultValue>; break; }

Flowchart of Switch Statement case a true case a action(s) break false case b false true case b action(s) break... case z false true case z action(s) break default action(s)

Multiway Switch Selection example int main(){//simple calculator int a=50,b=10, R; char choice; printf( Enter choice ); scanf( %c,&choice); switch (choice){ case a : R=a+b; printf( R=%d,R); break; case s : R=a-b; printf( R=%d,R); break; case m : R=a*b; printf( R=%d,R); break; case d : R=a/b; printf( R=%d,R); break; default : printf( Wrong choice ) ; break; } return 0; }

Multiway Switch Selection example int main(){//simple calculator int a=50,b=10, R; char choice; printf( Enter choice ); scanf( %c,&choice); switch (choice){ case a : R=a+b; printf( R=%d,R); break; case s : R=a-b; printf( R=%d,R); break; case m : R=a*b; printf( R=%d,R); break; case d : R=a/b; printf( R=%d,R); break; default : printf( Wrong choice ) ; break; } return 0; }

Multiway Switch Selection example switch (choice){ case A : // no break, work for both A & a // next statement automatically // get executed case a : R=a+b; printf( R=%d,R); break; case S : case s : R=a-b; printf( R=%d,R); break; case M : case m : R=a*b; printf( R=%d,R); break; case D : case d : R=a/b; printf( R=%d,R); break; default : printf( Wrong choice ) ; break; }

Range Multiway Switch Selection example int x; scanf( %d,&x); switch (x){ case 1... 20:// 1 space three dots space 20 printf( You entered >=1 and <=20 ); break; case 21... 30 : printf( You entered >=21 and <=30 ); break; default : printf( You entered < 1 and >31 ) ; break; } Syntax = case <low_range>... <high_range> :

Thanks 36