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.

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
Data Types H&K Chapter 7 Instructor – Gokcen Cilingir Cpt S 121 (July 12, 2011) Washington State University.
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)
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng. Nat. Chung Cheng Univ. TA: 鄭筱親 陳昱豪.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Introduction to Computers and Programming Lecture 7:
Scope and Casting. Scope Region of the program where a particular name can be referenced Formal parameters and local variables –can be accessed from within.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
Lab 10 rRepresentation And Conversion of Numeric Types l Difference between Numeric Types l Automatic conversion of Data types l Explicit Conversion of.
1 Fundamental Data Types. 2 Declaration All variables must be declared before being used. –Tells the compiler to set aside an appropriate amount of space.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
Instructor: Sanchita Mal-Sarkar Course: CIS 265
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
Chapter 7 Simple Data Types and Function Calls Alkar / Demirer.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Simple Data Type Representation and conversion of numbers
Chapter 7 Simple Date Types Instructor: Kun-Mao Chao ( 台大資工 趙坤茂 )
Input & Output: Console
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
chap7 Chapter 7 Simple Data Types chap7 2 Objectives No programming language can predefine all the data types that a programmer may.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
COMP 116: Introduction to Scientific Programming Lecture 28: Data types.
First Program.  The type of all data used in a C program must be specified  A data type is a description of the data being represented ▪ That is, a.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
Chapter 2: Using Data.
Java Software Solutions Lewis and Loftus Chapter 5 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. More Programming Constructs.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
CISC105 – General Computer Science Class 9 – 07/03/2006.
Unit 3 Lesson 4 How Data Types Affect Calculations Dave Clausen La Cañada High School.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
1 Lecture 5 More Programming Constructs Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung Institute of Technology.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Programming and Data Structure
Data Representation Binary Numbers Binary Addition
Tokens in C Keywords Identifiers Constants
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Modified from Sharon Guffy
Chapter 6 Floating Point
Fundamental Data Types
Conversions of the type of the value of an expression
Simple Data Types and Function Calls
Lectures on Numerical Methods
Chapter 7 Simple Date Types Dr. Jiung-yao Huang Dept. Comm. Eng.
Fundamental Data Types
Variables and Constants
ECE 120 Midterm 1 HKN Review Session.
Presentation transcript:

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 between numbers represented in these two ways? –Integer operations are typically faster –Integers require less storage space –Floating point arithmetic is subject to round-off error

Representation and Conversion of Numeric Types 4 Different numeric types are represented differently in the computer’s memory –Remember that each word of memory holds a string of 0’s and 1’s –An int is represented by a single binary string –A double is represented by two binary strings One string for the mantissa One string for the exponent The number stored is then: mantissaX2 exponent

Representation and Conversion of Numeric Types 4 The mantissa is a binary fraction between 0.5 and 1.0 for positive numbers and between -0.5 and -1.0 for negative numbers 4 Because of the finite size of memory cells not all numbers can be represented 4 Advantages of double: –Contains a fractional part –Can represent much larger numbers

Representation and Conversion of Numeric Types 4 The minimum range of positive integer values is 1 to 32,767 (ANSI C) 4 The minimum range of positive double values is approximately to ANSI C provides several other integer types short-32, ,767 unsigned short 0..65,535 unsigned0..65,535 long-2,147,483,647..2,147,483,647 unsigned long0..4,294,967,295

Representation and Conversion of Numeric Types 4 In addition to double, ANSI C defines the following floating point data types float significant digits double ““ long double ““ 4 Data types giving a larger range of values require more storage and take a longer time to evaluate in arithmetic expressions

Numerical Inaccuracies 4 Due to the finite word size of a computer, not all fractional values can be represented exactly (consider 1/3) 4 The representational error will depend on the number of bits used in the mantissa 4 An equality comparison of two floating point numbers may result in an unexpected result

Numerical Inaccuracies 4 Addition of a very large number to a very small fraction may cancel out the smaller number, resulting in cancellation error 4 If two very small numbers are multiplied the result may be too small to represent, resulting in arithmetic underflow 4 Similarly, multiplying two very large numbers may result in arithmetic overflow

Automatic Conversion of Data Types 4 In several situations, a numeric value is converted to another numeric type When adding (for example) an int and a double, the int is first converted to a double When assigning an expression of type double to a variable of type int, the expression is first evaluated as a double and then converted to an int to allow the assignment Likewise, an int expression is evaluated before it is converted to double to be assigned to a double variable

Explicit Conversion of Data Types 4 The programmer can also cause a value to be converted to another type explicitly using the cast operator frac = (double)n1 / (double)d1; –The data type we want to convert to is inside of parentheses and we convert the value directly to the right (i.e. the cast has a high precedence) frac = (double)(n1 / d1);

Representation and Conversion of Type char 4 The char data type allows us to store single characters in a variable 4 In order to understand how C evaluates comparison operations involving characters, we need to know how characters are represented internally 4 The most common code for representing characters (and the one used in C is called ASCII)

Representation and Conversion of Type char 4 The ASCII character set consists of 256 characters –Each character is associated with one of the numbers from –The number of the character defines the order used in comparisons –Only 95 of the characters are printable, the others are control characters (e.g. move the cursor to the top of the screen) –Cast a char to int to find its ASCII value