Download presentation
Presentation is loading. Please wait.
Published byBrandon Johnston Modified over 9 years ago
1
COMP 116: Introduction to Scientific Programming Lecture 28: Data types
2
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
3
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
4
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
5
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
6
Breaking out of while loops while commands; end while commands1; if break; end commands2; end
7
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.
8
DATA TYPES
9
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)
10
Checking the type of a variable NameData Type SizeMemory Location (hidden from user) Value Radius single4 bytes0x1800F0403.23 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)
11
Representing numbers twenty-five = 2*10 1 + 5*10 0 = 25 10 twenty-five = 1*2 4 + 1*2 3 + 0*2 2 + 0*2 1 + 1*2 0 = 11001 2 Exercise: 10001110 2 = ? 10 use base2dec() and dec2base() to convert between different representations
12
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
13
Integer Number Representations conversion functions intmin, intmax 164 sign int64 64-Bit Integer uint64 {{ 132 sign int32 32-Bit Integer uint32 116 sign int16 16-Bit Integer uint16 int8 8-Bit Integer uint8 sign 18 { { [-2 7, +2 7 -1] = [-128, +127] [-32,768 +32,767] [0 65,535] [0, +2 32 -1] [-2 31, +2 31 -1] [0, +2 8 -1] = [0, +255]
14
Fixed-point numbers Good: ◦ Simple, exact representation Bad: ◦ Range is too small! ◦ Only integers
15
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
16
Floating-point numbers Like scientific notation for binary twenty-five = 2.5 * 10 1 twenty-five = 11001 2 = 1.1001 2 * 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 10 100 = 1 * 1.142987… * 2 332
17
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
18
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
19
Datatypes in MATLAB Data TypeSize (Bytes) MinMaxNotes logical 1 0 (false)1 (true) int8 1-128+127 Numeric, signed, integer, Exact int16 2-32768+32767 Ditto int32 4-2147483648+2147483647 Ditto int64 8 -9223372036854775808+9223372036854775807 Ditto char 2N/A Encoded character string Varies len+1 N/A String of encoded characters
20
Datatypes in MATLAB (contd.) Data TypeSize (Bytes) MinMaxNotes uint8 10+255 Numeric, signed, integer, Exact uint16 20+65,535 Ditto uint32 40+4,294,967,295 Ditto uint64 8 0 +18,446,744,073,709,551,615 Ditto single 4 -3.4028e+038 +3.4028e+038 Numeric Real Approximate double 8 -1.7977e+308 +1.7977e+308 Ditto
21
doc datatypes Everything is double by default ◦ Except the result of imread(), which is uint8 Datatypes in MATLAB (contd.)
23
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’
24
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
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.