CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004.

Slides:



Advertisements
Similar presentations
2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
Advertisements

Chapter 10 C Structures, Unions, Bit Manipulations and Enumerations Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc.
Chapter 10 C Structures, Unions, Bit Manipulations, and Enumerations.
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Enumerated data type & typedef. Enumerated Data Type An enumeration consists of a set of named integer constants. An enumeration type declaration gives.
ENUMERATED, typedef. ENUMERATED DATA TYPES An enumeration consists of a set of named integer constants. An enumeration type declaration gives the name.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 10 - Structures, Unions, Bit Manipulations, and Enumerations Outline 10.1Introduction 10.2Structure.
Sizes of simple data types sizeof(char) = 1 size(short) = 2 sizeof(int) = 4 size(long) = 8 sizeof(char) = 1 size(short) = 2 sizeof(int) = 2 size(long)
Round-Off and Truncation Errors
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 10 - C Structures, Unions, Bit Manipulations,
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Representing Real Numbers Using Floating Point Notation Lecture 6 CSCI 1405, CSCI 1301 Introduction to Computer Science Fall 2009.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs Data Types & Arithmetic Errors Arithmetic Operators and Functions Program Testing Tips.
Computer Science 210 Computer Organization Floating Point Representation.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Simple Data Type Representation and conversion of numbers
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
1 COMS 161 Introduction to Computing Title: Numeric Processing Date: October 22, 2004 Lecture Number: 24.
Five Tips to Success. Work hard Try more exercises and more practice.
Chapter 6 Fundamental Types Dept of Computer Engineering Khon Kaen University.
Round-off Errors and Computer Arithmetic. The arithmetic performed by a calculator or computer is different from the arithmetic in algebra and calculus.
16. Binary Numbers Programming in C++ Computer Science Dept Va Tech August, 1999 © Barnette ND, McQuain WD, Keenan MA 1 Binary Number System Base.
Lecture #5 Introduction to C++
MECN 3500 Inter - Bayamon Lecture 3 Numerical Methods for Engineering MECN 3500 Professor: Dr. Omar E. Meza Castillo
CISC105 – General Computer Science Class 9 – 07/03/2006.
ITEC 1011 Introduction to Information Technologies 4. Floating Point Numbers Chapt. 5.
Term 2, 2011 Week 1. CONTENTS Problem-solving methodology Programming and scripting languages – Programming languages Programming languages – Scripting.
What is C? C is a programming language. It was developed in 1972 USA. It was designed and written by a man named dennis ritchie. C is the base for all.
 2007 Pearson Education, Inc. All rights reserved C Structures, Unions, Bit Manipulations and Enumerations.
CSPP58001 Floating Point Numbers. CSPP58001 Floating vs. fixed point Floating point refers to a binary decimal representation where there is not a fixed.
CONSTANTS Constants are also known as literals in C. Constants are quantities whose values do not change during program execution. There are two types.
Chapter 10 Structures, Unions, Bit Manipulations, and Enumerations Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Computers and Numbers. Types of Numbers Computers store two different types of numbers: Whole Numbers AKA Integers (mathematics) AKA Fixed Point Numbers.
Floating Point Numbers
CS 160 Lecture 4 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Lecture 6: Floating Point Number Representation Information Representation: Floating Point Number Representation Lecture # 7.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Floating Point Numbers
Nat 4/5 Computing Science Lesson 1: Binary
Fundamentals of Computer Science
Objectives Today: P4 Data Types – Floating Points P4 Variable Quiz P3 Iteration and Selection Practical Are you logged on? Then come around the table Unit.
Computer Science 210 Computer Organization
Jan 2016 Solar Lunar Data.
Structure, Unions, typedef and enumeration
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
C Structures, Unions, and Enumerations
C Structures, Unions, Bit Manipulations and Enumerations
Floating Point Representation
Computer Science 210 Computer Organization
Gantt Chart Enter Year Here Activities Jan Feb Mar Apr May Jun Jul Aug
COMS 361 Computer Organization
C Structures, Unions, Bit Manipulations and Enumerations
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
Text for section 1 1 Text for section 2 2 Text for section 3 3
1.6) Storing Integer: 1.7) storing fraction:
Presentation transcript:

CS1061 C Programming Lecture 7: Floating Point A. O’Riordan, 2004

A Floating Point Floating point variable types (or floats) allow us to store non-integer numbers (real numbers), which can have fractional parts as well as whole (integral) parts. for any given storage size, floating point variables can represent much larger numbers than integer variables can. The reason for this is that a float type is represented internally in exponential format. The exponential format consists of two parts: the mantissa and the exponent. The mantissa contains the significant digits - that is, the digits that represent the precision (detail) of a quantity. The greater the number of mantissa digits, the greater the precision. The second part, the exponent, merely specifies where to place a decimal point that separates the whole part from the fractional part of a number - hence, the term 'floating point'.

Exponentials The reason that floats can store larger numbers is that the size of the number is indicated by a power of ten. Actually, the size is indicated by a power of 2 since all internal values are in binary format but we use decimal numbers (base 10) in our illustrations because this is more natural x10 4 = x10x10x10x10 = x10 4 is represented as e4 or E4 Here are example floats (all have the same value): e2 0.2E3

Floats can have round-off error More advanced material here (don’t worry about this too much)! Suppose we have a hypothetical storage location that can hold four decimal digits; the largest integer that this could represent would be 9999 but we use a float representation and apportion the mantissa as three digits and the exponent as one digit. Then we could represent a much larger number within these four digits, the largest being: 999 x 10 9 = 999,000,000,000 (Here the mantissa is 999 and the exponent is 9.) Of course we can really only represent a small subset of the numbers less than 999x10 9. Computations involving large numbers thus result in inaccuracies, called round-off error.

Floats can have round-off error (2) The maximum value that can be represented here is: 999 x 10 9, but the maximum precision that can be maintained is only three digits - the length of the mantissa. For example, we could not store the number 425,735,485,001 in that format - we could only approximate it with 426e9 So floating point data types can hold larger numbers than integer types of the same length. But a floating point type offers no more precision than an integer type of the same length.

Declarations Floating point numbers are declared in C using the float keyword. For example, the following declares a floating point variable named balance. float balance; To maintain higher-precision numbers, we must increase the storage size, and this is the purpose of yet another data type provided by the C programming language: the double precision type. double velocity= ;

Bit Manipulation All data is represented internally as sequences of bits - C has some operators for bit manipulation OperatorNameDescription & bitwise ANDThe bits in the result are set to 1 if the corresponding bits in the two operands are both 1. | bitwise ORThe bits in the result are set to 1 if at least one of the corresponding bits in the two operands is 1. ^ bitwise exclusive ORThe bits in the result are set to 1 if exactly one of the corresponding bits in the two operands is 1. << left shiftShifts the bits of the first operand left by the number of bits specified by the second operand; fill from right with 0 bits. >> right shiftShifts the bits of the first operand right by the number of bits specified by the second operand; the method of filling from the left is machine dependent. ~ One’s complementAll 0 bits are set to 1 and all 1 bits are set to 0.

Enumeration constants Set of integer constants represented by identifiers Enumeration constants are like symbolic constants whose values are automatically set Values start at 0 and are incremented by 1 Values can be set explicitly with = Need unique constant names Example: enum Months { JAN = 1, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC}; Creates a new type enum Months in which the identifiers are set to the integers 1 to 12