COMP 116: Introduction to Scientific Programming Lecture 28: Data types.

Slides:



Advertisements
Similar presentations
2009 Spring Errors & Source of Errors SpringBIL108E Errors in Computing Several causes for malfunction in computer systems. –Hardware fails –Critical.
Advertisements

Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
ES 314 Lecture 2 Sep 1 Summary of lecture 1: course overview intro to matlab sections of Chapters 2 and 3.
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.
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Types and Variables. Computer Programming 2 C++ in one page!
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
General Computer Science for Engineers CISC 106 Lecture 22 Dr. John Cavazos Computer and Information Sciences 04/13/2009.
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.
Introduction to Programming with Java, for Beginners
1 CSC 1401 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Chapter 2 Data Types, Declarations, and Displays
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.
Simple Data Type Representation and conversion of numbers
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
1 Lecture 5 Floating Point Numbers ITEC 1000 “Introduction to Information Technology”
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Information Representation: Negative and Floating Point.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Computer Science 111 Fundamentals of Programming I Number Systems.
Number Systems So far we have studied the following integer number systems in computer Unsigned numbers Sign/magnitude numbers Two’s complement numbers.
Copyright © 2002 W. A. Tucker1 Chapter 7 Lecture Notes Bill Tucker Austin Community College COSC 1315.
Why does it matter how data is stored on a computer? Example: Perform each of the following calculations in your head. a = 4/3 b = a – 1 c = 3*b e = 1.
Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.
Data Representation - Part II. Characters A variable may not be a non-numerical type Character is the most common non- numerical type in a programming.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Representing numbers and Basic MATLAB 1. Representing numbers  numbers used by computers do not behave the same as numbers used in mathematics  e.g.,
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.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
1 Number Systems Lecture 10 Digital Design and Computer Architecture Harris & Harris Morgan Kaufmann / Elsevier, 2007.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
ISBN Chapter 6 Data Types Introduction Primitive Data Types User-Defined Ordinal Types.
Computer Organization and Design Information Encoding II Montek Singh Wed, Aug 29, 2012 Lecture 3.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
Matlab Data types, input and output. Data types Char: >> a = ‘ Jim ’ Char: >> a = ‘ Jim ’ Numeric: uint8, uint16, uint32, uint64 int8, int16, int32, int64.
Computer Graphics Basic Maths for Graphics in C++ CO2409 Computer Graphics Week 4.
CSI 3125, Preliminaries, page 1 Data Type, Variables.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Introduction ABAP Fields and Variables. Slide 2 Fields (Introduction) In ABAP, fields (or data objects) are named locations in memory Variables store.
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.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Fixed-point and floating-point numbers Ellen Spertus MCS 111 October 4, 2001.
Objectives A data type is  A set of values AND  A set of operations on those values A data type is used to  Identify the type of a variable when the.
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.
Introduction To Computer Science
Chapter 6: Data Types Lectures # 10.
Tokens in C Keywords Identifiers Constants
Floating Point Numbers: x 10-18
Modified from Sharon Guffy
Data Structures Mohammed Thajeel To the second year students
Fundamental Data Types
Numerical Data Types.
Fundamentals of Programming I Number Systems
C++ Data Types Data Type
Lectures on Numerical Methods
Fundamental Data Types
Introduction to Java, and DrJava
Presentation transcript:

COMP 116: Introduction to Scientific Programming Lecture 28: Data types

So far…. Fundamentals of programming ◦ Conditional logic (is-else-end) ◦ Functions ◦ Loops What’s coming ◦ Application: minimization, animation … Today ◦ A wrap-up of the fundamentals: breaking out of loops, data types

The break command: for loops for var = vector commands end for var = vector commands1 if break; end commands2 end Break out of the loop when test1 evaluates to true

Find the first occurrence of a scalar b in the array A % Assume that A is an array and b is a scalar i=1; while (i <= length(A)) & (A(i) ~= b) i=i+1; end

Find the first occurrence of a scalar b in the array A % Assume that A is an array and b is a scalar for i=1:length(A) if A(i)==b break; end

Breaking out of while loops while commands; end while commands1; if break; end commands2; end

Exercise ISALPH_NUM returns True for alpha- numeric character, including '_‘ Write a function that given a string tests if only contains only alphabets, numbers or the ‘_’ character. i.e. No space, *, $, + etc.

DATA TYPES

What is a Data Type? Variables have more attributes than just value : data type, memory location where it is stored Data type: How to interpret a storage location to retrieve the correct value. Typical data types: Integer, Float, Logical, Char Other languages require you to explicitly specify the data type of variables MATLAB implicitly infers the data type from the first initialization via the specified expression. ◦ Defaults to ‘double’ (used to store real numbers)

Checking the type of a variable NameData Type SizeMemory Location (hidden from user) Value Radius single4 bytes0x1800F currKey char1 byte0x1800F049‘k’ firstName char6 bytes0x1800B0E0‘shawn;’ width int324 bytes0x1800CCE8800 type int81 byte0x1800CCE727 Use class() to find the type of a variable Use whos() to find the information in the above (except for memory location)

Representing numbers twenty-five = 2* *10 0 = twenty-five = 1* * * * *2 0 = Exercise: = ? 10 use base2dec() and dec2base() to convert between different representations

Fixed-point numbers With n bits, you can represent 2 n numbers 2 bits: 00, 01, 10, 11 If you have 8 bits (1 “byte”) ◦ 0 to 255 (unsigned) ◦ or -128 to 127 (signed) 32 bits gets you up to about 4.3 billion

Integer Number Representations conversion functions intmin, intmax 164 sign int64 64-Bit Integer uint64 {{ 132 sign int32 32-Bit Integer uint sign int16 16-Bit Integer uint16 int8 8-Bit Integer uint8 sign 18 { { [-2 7, ] = [-128, +127] [-32, ,767] [0 65,535] [0, ] [-2 31, ] [0, ] = [0, +255]

Fixed-point numbers Good: ◦ Simple, exact representation Bad: ◦ Range is too small! ◦ Only integers

Integer Issues Overflow, expression tries to create an integer value larger than allowed valid range [min,max] ◦ x = int8( 127 ) + 1 ◦ Saturate Arithmetic (MATLAB)  value clamped to min, max range ( x = 127 ) ◦ Wrapping Arithmetic (Most languages)  wraps back around to other end of range ( x = -128 ) Truncation, fractions not supported ◦ int16(1)/int16(4) = 0 not 0.25 ◦ Rounds result to nearest whole number

Floating-point numbers Like scientific notation for binary twenty-five = 2.5 * 10 1 twenty-five = = * 2 4 In general: ◦n = sign * mantissa * 2 exponent Good: ◦ Can represent non-integral numbers  -2.5 = -1 * 1.25 * 2 1 ◦ And very large numbers  = 1 * … * 2 332

Real Number Representations IEEE 754 Floating point standard Reals ◦ Sign bit ( 1 bit) ◦ Exponent ( 8 or 11 bits) ◦ Mantissa (fraction) ( 23 bits or 52 bits) ◦ Single ◦ Double

Real Issues (single, double) Precision Error ◦ Most numbers don’t get represented exactly ◦ Finite precision of IEEE floating point ◦ Represented by nearest real (floating point) number Numeric Stability (does error overwhelm?) ◦ Truncation Errors ◦ Accumulated error from repeated calculations

Datatypes in MATLAB Data TypeSize (Bytes) MinMaxNotes logical 1 0 (false)1 (true) int Numeric, signed, integer, Exact int Ditto int Ditto int Ditto char 2N/A Encoded character string Varies len+1 N/A String of encoded characters

Datatypes in MATLAB (contd.) Data TypeSize (Bytes) MinMaxNotes uint Numeric, signed, integer, Exact uint ,535 Ditto uint ,294,967,295 Ditto uint ,446,744,073,709,551,615 Ditto single e e+038 Numeric Real Approximate double e e+308 Ditto

doc datatypes Everything is double by default ◦ Except the result of imread(), which is uint8 Datatypes in MATLAB (contd.)

Conversion between types Conversion: Use cast function or type name >> ch = cast(97, ‘char’); % ch=‘a’ >> val = a+1;% val=98 >> ch2= char(val) % ch2=‘b’

Exercise Rot-13 encoding Write a function that given a string returns its rot-13 enconding You will to convert to and from char and may also need to use the mod command that returns the remainder