240 likes | 375 Views
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.
E N D
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 forvar= vector commands end forvar= vector commands1 if <test1> 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 end
Breaking out of while loops while <test> commands; end while <test1> commands1; if <test2> 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.
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 • 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*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
Fixed-point numbers • With n bits, you can represent 2nnumbers • 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 Representationsconversion functions intmin, intmax sign int8 8-Bit Integer uint8 { [-27, +27-1] = [-128, +127] 8 1 [0, +28-1] = [0, +255] sign sign int16 16-Bit Integer uint16 { [-32,768 +32,767] int32 32-Bit Integer uint32 { 16 1 32 1 [0 65,535] sign [-231, +231-1] int64 64-Bit Integer uint64 { 64 [0, +232-1] 1
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 * 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
Real Number RepresentationsIEEE 754 Floating point standard • Reals • Sign bit (1 bit) • Exponent (8or 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 (contd.) • doc datatypes • Everything is double by default • Except the result of imread(), which is uint8
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