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

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

Embed Size (px)

Citation preview

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

COMP 116: Introduction to Scientific Programming

Lecture 28: Data types

Page 2: 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

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

The break command: for loopsfor var = vector

commandsendfor var = vector

commands1 if <test1>

break; end

commands2end

Break out of the loop when test1 evaluates to true

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

Find the first occurrence of a scalar b in the array A

% Assume that A is an array and b is a scalari=1;while (i <= length(A)) & (A(i) ~= b) i=i+1;end

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

Find the first occurrence of a scalar b in the array A

% Assume that A is an array and b is a scalarfor i=1:length(A) if A(i)==b

break; endend

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

Breaking out of while loopswhile <test>commands;

end

while <test1>commands1;if <test2>

break; end commands2;

end

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

ExerciseISALPH_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.

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

DATA TYPES

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

What is a Data Type?Variables have more attributes than just value

: data type, memory location where it is storedData type: How to interpret a storage location

to retrieve the correct value.Typical data types: Integer, Float, Logical, CharOther languages require you to explicitly

specify the data type of variablesMATLAB implicitly infers the data type from the

first initialization via the specified expression.◦ Defaults to ‘double’ (used to store real numbers)

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

Checking the type of a variable

Name Data Type

Size Memory Location(hidden from user)

Value

Radius single 4 bytes 0x1800F040 3.23

currKey char 1 byte 0x1800F049 ‘k’

firstName

char 6 bytes 0x1800B0E0 ‘shawn;’

width int32 4 bytes 0x1800CCE8 800

type int8 1 byte 0x1800CCE7 27Use class() to find the type of a variableUse whos() to find the information in the

above (except for memory location)

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

Representing numbers twenty-five

= 2*101 + 5*100

= 2510

twenty-five = 1*24 + 1*23 + 0*22 + 0*21 + 1*20

= 110012

Exercise:100011102 = ?10 use base2dec() and dec2base() to convert

between different representations

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

Fixed-point numbersWith n bits, you can represent 2n 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

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

Integer Number Representationsconversion functions intmin, intmax

164

sign

int6464-Bit Integer

uint64{

{ 132

sign

int3232-Bit Integer

uint32

116

sign

int1616-Bit Integer

uint16

int88-Bit Integer

uint8

sign

18

{

{[-27, +27-1] = [-128, +127]

[-32,768 +32,767]

[0 65,535]

[0, +232-1]

[-231, +231-1]

[0, +28-1] = [0, +255]

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

Fixed-point numbers Good:

◦ Simple, exact representation

Bad:◦ Range is too small!◦ Only integers

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

Integer IssuesOverflow, 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

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

Floating-point numbersLike scientific notation for binary

twenty-five = 2.5 * 101 twenty-five = 110012

= 1.10012 * 24

In general:◦ n = sign * mantissa * 2exponent

Good: ◦ Can represent non-integral numbers

-2.5 = -1 * 1.25 * 21

◦ And very large numbers 10100 = 1 * 1.142987… * 2332

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

Real Number RepresentationsIEEE 754 Floating point standard

Reals◦Sign bit (1 bit)

◦Exponent (8 or 11 bits)

◦Mantissa (fraction) (23 bits or 52 bits)

◦Single

◦Double

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

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

tionrepresentaactualError

computedanswertrueError _

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

Datatypes in MATLABData Type

Size (Bytes)

Min Max Notes

logical 1 0 (false) 1 (true)

int8 1 -128 +127 Numeric, signed, integer,Exact

int16 2 -32768 +32767 Dittoint32 4 -2147483648 +2147483647 Dittoint64 8 -9223372036854775808

+9223372036854775807 Ditto

char 2 N/A N/A Encoded character

string Varieslen+1

N/A N/A String of encoded characters

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

Datatypes in MATLAB (contd.)

Data Type

Size (Bytes)

Min Max Notes

uint8 1 0 +255 Numeric, signed, integer,Exact

uint16 2 0 +65,535 Dittouint32 4 0 +4,294,967,295 Dittouint64 8 0 +18,446,744,073,709,551,615 Dittosingle 4 -3.4028e+038 +3.4028e+038 Numeric

RealApproximate

double 8 -1.7977e+308 +1.7977e+308 Ditto

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

doc datatypes

Everything is double by default◦Except the result of imread(), which is

uint8

Datatypes in MATLAB (contd.)

Page 22: COMP 116: Introduction to Scientific Programming Lecture 28: Data types
Page 23: COMP 116: Introduction to Scientific Programming Lecture 28: Data types

Conversion between typesConversion: Use cast function or type

name>> ch = cast(97, ‘char’); % ch=‘a’

>> val = a+1; % val=98

>> ch2= char(val) % ch2=‘b’

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

Exercise Rot-13 encodingWrite 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