Computer Science 121 Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans.

Slides:



Advertisements
Similar presentations
1/1/ / faculty of Electrical Engineering eindhoven university of technology Introduction Part 2: Data types and addressing modes dr.ir. A.C. Verschueren.
Advertisements

The Binary Numbering Systems
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Types and Variables. Computer Programming 2 C++ in one page!
Chapter 4: The Building Blocks: Binary Numbers, Boolean Logic, and Gates Invitation to Computer Science, C++ Version, Third Edition.
Assembly Language and Computer Architecture Using C++ and Java
Introduction to Programming with Java, for Beginners
Signed Numbers.
Chapter 2 Data Types, Declarations, and Displays
Number Systems Lecture 02.
Chapter 2 Bits, Data Types, and Operations. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 2-2 How do we.
Introduction to Computing Systems from bits & gates to C & beyond Chapter 2 Bits, Data Types & Operations Integer Representation Floating-point Representation.
Dr. Bernard Chen Ph.D. University of Central Arkansas
Chapter 5 Data representation.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
(2.1) Fundamentals  Terms for magnitudes – logarithms and logarithmic graphs  Digital representations – Binary numbers – Text – Analog information 
Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text, Booleans.
Simple Data Type Representation and conversion of numbers
ACOE1611 Data Representation and Numbering Systems Dr. Costas Kyriacou and Dr. Konstantinos Tatas.
Binary Numbers. Why Binary? Maximal distinction among values  minimal corruption from noise Imagine taking the same physical attribute of a circuit,
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
2-1 Chapter 2 - Data Representation Principles of Computer Architecture by M. Murdocca and V. Heuring © 1999 M. Murdocca and V. Heuring Chapter Contents.
1 Digital Technology and Computer Fundamentals Chapter 1 Data Representation and Numbering Systems.
NUMBER REPRESENTATION CHAPTER 3 – part 3. ONE’S COMPLEMENT REPRESENTATION CHAPTER 3 – part 3.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Computer Science 111 Fundamentals of Programming I Number Systems.
Number Systems So far we have studied the following integer number systems in computer Unsigned numbers Sign/magnitude numbers Two’s complement numbers.
Data Representation and Computer Arithmetic
Number Systems Spring Semester 2013Programming and Data Structure1.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
Data Representation in Computer Systems
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
PART 2 Data Types 1. Integers 2. 2’s Complement 3. Conversion
Data Representation Dr. Ahmed El-Bialy Dr. Sahar Fawzy.
Lecture 5. Topics Sec 1.4 Representing Information as Bit Patterns Representing Text Representing Text Representing Numeric Values Representing Numeric.
Binary Numbers. Why Binary? Maximal distinction among values  minimal corruption from noise Imagine taking the same physical attribute of a circuit,
Bits, Data Types, and Operations Slides based on set prepared by Gregory T. Byrd, North Carolina State University.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Chapter 3 Number Representation. Convert a number from decimal to binary notation and vice versa. Understand the different representations of an integer.
CSC 221 Computer Organization and Assembly Language
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
CSPP58001 Floating Point Numbers. CSPP58001 Floating vs. fixed point Floating point refers to a binary decimal representation where there is not a fixed.
Number Systems & Operations
Instructor:Po-Yu Kuo 教師:郭柏佑
Irvine, Kip R. Assembly Language for Intel-Based Computers 6/e, Signed Integers The highest bit indicates the sign. 1 = negative, 0 = positive.
CS1Q Computer Systems Lecture 2 Simon Gay. Lecture 2CS1Q Computer Systems - Simon Gay2 Binary Numbers We’ll look at some details of the representation.
Binary Arithmetic.
Dr Mohamed Menacer College of Computer Science and Engineering Taibah University CE-321: Computer.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations Expressions.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
Number Systems and Computer Arithmetic Winter 2014 COMP 1380 Discrete Structures I Computing Science Thompson Rivers University.
©Brooks/Cole, 2003 Chapter 3 Number Representation.
Computing Systems Lecture 3 Binary Representation & Boolean Logic Binary and Logic 1.
Data Representation. Representation of data in a computer Two conditions: 1. Presence of a voltage – “1” 2. Absence of a voltage – “0”
1 CE 454 Computer Architecture Lecture 4 Ahmed Ezzat The Digital Logic, Ch-3.1.
Fundamentals of Computer Science
Programming and Data Structure
Data Representation Binary Numbers Binary Addition
Data Structures Mohammed Thajeel To the second year students
How to represent real numbers
Number Representation
Presentation transcript:

Computer Science 121 Scientific Computing Winter 2016 Chapter 3 Simple Types: Numbers, Text, Booleans

3.1 The Organization of Computer Memory Computers store information as bits : sequences of zeros and ones –0 / 1 –true / false –on / off –yes/no Why base 2 (binary) - vs. base 10 (decimal)? (Note: book misleadingly uses “Arabic” to mean base 10) For an N-bit sequence, we have 2 N possible values

Binary-to-Decimal Conversion To convert from binary to decimal – Start from right – Multiply 0,1 by powers of two (1, 2, 4, 8, …) – Sum of these products is decimal equivalent E.g., = ??? 10

Binary-to-Decimal Conversion To convert from binary to decimal – Start from right – Multiply 0,1 by powers of two (1, 2, 4, 8, …) – Sum of these products is decimal equivalent E.g., = ??? 10 1 * 2 0 = 1

Binary-to-Decimal Conversion To convert from binary to decimal – Start from right – Multiply 0,1 by powers of two (1, 2, 4, 8, …) – Sum of these products is decimal equivalent E.g., = ??? 10 1 * 2 0 = 1 +0 * 2 1 = 0

Binary-to-Decimal Conversion To convert from binary to decimal – Start from right – Multiply 0,1 by powers of two (1, 2, 4, 8, …) – Sum of these products is decimal equivalent E.g., = ??? 10 1 * 2 0 = 1 +0 * 2 1 = 0 +1 * 2 2 = 4

Binary-to-Decimal Conversion To convert from binary to decimal – Start from right – Multiply 0,1 by powers of two (1, 2, 4, 8, …) – Sum of these products is decimal equivalent E.g., = ??? 10 1 * 2 0 = 1 +0 * 2 1 = 0 +1 * 2 2 = 4 +1 * 2 3 = 8

Binary-to-Decimal Conversion To convert from binary to decimal – Start from right – Multiply 0,1 by powers of two (1, 2, 4, 8, …) – Sum of these products is decimal equivalent E.g., = ??? 10 1 * 2 0 = 1 +0 * 2 1 = 0 +1 * 2 2 = 4 +1 * 2 3 = 8 ____________ 13

13mod 2 = 1 13 ÷ 2 = 6 6mod 2 = 0 6 ÷ 2 = 3 3mod 2 = 1 3 ÷ 2 = 1 1mod 2 = 1 1 ÷ 2 = 0 ___________ Decimal-to-Binary Conversion To convert from decimal to binary 1. Take remainder of decimal number / 2 2. Write down remainders right-to-left 3. If decimal number is zero, we’re done 4. Divide decimal number by 2 5. Go to step 1.

Sign/Magnitude Notation Bit sequences are typically organized into eight-bit chunks called bytes : 8 bits → 2 8 = 256 possible values Can use leftmost bit for sign (+/-) E.g., = ; = Yields 128 negative, 128 positive values – but this means we have +/- 0 ( , ), wasting one value! So use two’s complement

Two’s Complement Notation To negate a binary number: Flip the bits Add Nice features Leftmost 1 still means negative Don’t waste a value (256 unique values, one zero) Can do subtraction as addition

Two’s Complement Subtraction ________________________________ 15 – 15

Two’s Complement Subtraction ________________________________ 0

15 – 15 Two’s Complement Subtraction ________________________________ 0 0

15 – 15 Two’s Complement Subtraction ________________________________ 000

15 – 15 Two’s Complement Subtraction ________________________________ 0000

15 – 15 Two’s Complement Subtraction ________________________________ 00000

Two’s Complement Subtraction ________________________________ – 15

Two’s Complement Subtraction ________________________________ – 15

Two’s Complement Subtraction ________________________________ – 15 (Leftmost carry disappears)

Floating-Point Numbers Numbers containing a decimal point Original decimal-point notation had “fixed” point (e.g., two digits from right for $) With floating-point, decimal point “floats”

Floating-Point Numbers General form: mantissa e exponent avogadrosNumber = 6.023e23 plancksConstant = e-34 Double precision float (a.k.a. double): 53 bits for mantissa, 11 for exponent (IEEE 754 standard) Default exponent = 0 ( 3.14 = 3.14e0 )

Special Floating-Point Values inf : bigger than any actual number Python can handle: >>> float('inf') > True >>> 1e99999 inf nan : “not a number”; i.e., undefined >>> float('inf') / float('inf') nan

3.2 Text (Strings) Bits can be interpreted any way we want –Sign/magnitude integer –Two’s-complement integer –IEEE 754 double-precision –Integer representing an entry in a table of characters (Google on ascii table)

3.2 Text (Strings) Need to distinguish text from program code: use single quotes (c.f. English: “He said ‘hello’ to everyone in the room.”) >>> pi >>> "pi" 'pi'

Double vs. single quotes Both will work, as long as you're consistent For apostrophe, use single quote inside double quotes: >>> "Why can't anything be simple?" "Why can't anything be simple?" Don’t try to put a newline into quoted text: >>> "Four score and seven years ago our SyntaxError: EOL while scanning string literal

String formatting Concatenation via plus: >>> "Hello " + "and goodbye" Hello and goodbye Mixing in numbers with str : >>> "NumPy uses " + str(pi) + " for pi." 'NumPy uses for pi.'

3.3 Collections of Numbers and Plotting Sequences of numbers – a.k.a. arrays – are the most common kind of data in scientific computing NumPy uses array() function with square brackets to represent arrays: >>> array([1, 5, 7, 9]) array([1, 5, 7, 9])

The Fundamental Power of NumPy Operations on entire vectors at once: >>> 3 * array([1, 5, 7, 9]) array([ 3, 15, 21, 27]) >>> x = linspace(0,1,5) >>> x array([ 0., 0.25, 0.5, 0.75, 1.]) >>> x**2 array([ 0., , 0.25,0.5625, 1.])

Useful Vector Operations >>> len(x) 5 >> sum(x) 2.5 >> prod(array([1,3,5,7,9])) 945

Other Common Operations Sign-related: abs, sign Exponential: exp, log, log10, log2 Trig: sin, cos, tan, asin, acos, atan, sinh, cosh, tanh Fraction-to-integer: round, floor, ceil, fix Remainders: mod, % >>> mod(3,2) 1 >>> 3 % 2 1

Plotting with Matplotlib >>> from matplotlib.pyplot import * >>> time = array([0, 1, 2, 3.5, 7]) >>> temp = [93.5, 90.6, 87.7, 83.9, 76.6] >>> plot(time, temp, "-o"), show() (additional values shown)

Constructing Sequences of Numbers linspace : >>> linspace(1,.5, 5) array([1., 0.875, 0.75, 0.625, 0.5]) arange : >>> arange(6) array([0, 1, 2, 3, 4, 5]) >>> arange(5,10) array([5, 6, 7, 8, 9]) WTF?

Constructing Sequences of Numbers linspace : >>> linspace(1,.5, 5) array([1., 0.875, 0.75, 0.625, 0.5]) arange : >>> arange(6) array([0, 1, 2, 3, 4, 5]) >>> arange(5,10) array([5, 6, 7, 8, 9]) Off-by-One will you be!

Goin’ down: >>> linspace(5,0,4) array([ 5., 3.333, 1.667, 0.]) >>> arange(3,0,-1) array([3, 2, 1]) Concatenating with append : >>> a = linspace(1,2,3) >>> append(a, array([4,5])) array([ 1., 1.5, 2., 4., 5. ]) >>> a array([ 1., 1.5, 2. ])

3.4: Booleans: True or False Boolean (true/false) values are useful everywhere in computer science: >>> 3 < 7 True >>> pi > 2*pi False G. Boole ( )

Booleans with Arrays and Strings Arrays: >>> arange(5) < 2 array([ True, True, False, False, False], dtype=bool) >>> linspace(0,1,3) == array([0,.5,.99]) array([ True, True, False,], dtype=bool) Strings: >>> "Hello" == "Goodbye" False >>> "Hello" > "Goodbye" True

Logical Operations Often need to combine several comparisons –“at least 2 quantitative courses and at least 4 humanities courses” –“one MATH course and another math or CSCI course >>> mymath = 1; mycsci = 2 >>> myart = 1; mymusic = 1; myfrench = 1 >>> myquant = mymath + mycsci >>> myhum = myart + mymusic + myfrench >>> (myquant >= 2) and (myhum >= 4) False >>> (mymath >= 1) and (mycsci >=1 or mymath >=2) True

Logical Operations