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