1 / 24

COMP 116: Introduction to Scientific Programming

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.

viola
Télécharger la présentation

COMP 116: Introduction to Scientific Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 forvar= vector commands end forvar= vector commands1 if <test1> 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 end

  6. Breaking out of while loops while <test> commands; end while <test1> commands1; if <test2> 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 • 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*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

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

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

  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 * 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

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

  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

  20. Datatypes in MATLAB (contd.)

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

  22. 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’

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

More Related