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

Similar documents
Built-in Types of Data

The type of all data used in a C (or C++) program must be specified

Basic types and definitions. Chapter 3 of Thompson

Introduction to Computer Programming in Python Dr. William C. Bulko. Data Types

COMP Overview of Tutorial #2

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Number Systems. Decimal numbers. Binary numbers. Chapter 1 <1> 8's column. 1000's column. 2's column. 4's column

Number Systems. Binary Numbers. Appendix. Decimal notation represents numbers as powers of 10, for example

Lecture 3. Input, Output and Data Types

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

A complement number system is used to represent positive and negative integers. A complement number system is based on a fixed length representation

Inf2C - Computer Systems Lecture 2 Data Representation

ENGR 102 Engineering Lab I - Computation

Mentor Graphics Predefined Packages

Chapter 3: Arithmetic for Computers

ECE232: Hardware Organization and Design

Introduction to Computers and Programming. Numeric Values

Lecture 1. Types, Expressions, & Variables

The type of all data used in a C++ program must be specified

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Introduction to TURING

COMP1730/COMP6730 Programming for Scientists. Data: Values, types and expressions.

CHW 261: Logic Design

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

CS 115 Lecture 4. More Python; testing software. Neil Moore

Long (LONGMATH) variables may be used the same as short variables. The syntax is the same. A few limitations apply (see below).

Birkbeck (University of London) Department of Computer Science and Information Systems. Introduction to Computer Systems (BUCI008H4)

CHAPTER V NUMBER SYSTEMS AND ARITHMETIC

MACHINE LEVEL REPRESENTATION OF DATA

COMP2611: Computer Organization. Data Representation

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Outline. Data and Operations. Data Types. Integral Types

Numerical Representations On The Computer: Negative And Rational Numbers

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

Birkbeck (University of London) Department of Computer Science and Information Systems. Introduction to Computer Systems (BUCI008H4)

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Numerical Representations On The Computer: Negative And Rational Numbers

More about Binary 9/6/2016

Objectives. Connecting with Computer Science 2

Computing Fundamentals

Using the um-fpu with the Javelin Stamp

Long (or LONGMATH ) floating-point (or integer) variables (length up to 1 million, limited by machine memory, range: approx. ±10 1,000,000.

FLOATING POINT NUMBERS

Declaration and Memory

Chapter 4. Operations on Data

Computer System and programming in C

Arithmetic and Logic Blocks

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Exponential Numbers ID1050 Quantitative & Qualitative Reasoning

x= suppose we want to calculate these large values 1) x= ) x= ) x=3 100 * ) x= ) 7) x=100!

COMP-202: Foundations of Programming

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Floating Point Numbers

Chapter 2. Data Representation in Computer Systems

Chapter 4: Data Representations

Number Systems CHAPTER Positional Number Systems

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Floating-point Arithmetic. where you sum up the integer to the left of the decimal point and the fraction to the right.

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

Set Theory in Computer Science. Binary Numbers. Base 10 Number. What is a Number? = Binary Number Example

C NUMERIC FORMATS. Overview. IEEE Single-Precision Floating-point Data Format. Figure C-0. Table C-0. Listing C-0.

Lecture 1. Course Overview, Python Basics

IT 1204 Section 2.0. Data Representation and Arithmetic. 2009, University of Colombo School of Computing 1

Basic data types. Building blocks of computation

Module 2: Computer Arithmetic

Chapter 2 Data Representations

10.1. Unit 10. Signed Representation Systems Binary Arithmetic

Lecture 1. Course Overview, Python Basics

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Objectives. You will learn how to process data in ABAP

The. Binary. Number System

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Physics 306 Computing Lab 5: A Little Bit of This, A Little Bit of That

Data Representations & Arithmetic Operations

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Unit 3. Constants and Expressions

Math 340 Fall 2014, Victor Matveev. Binary system, round-off errors, loss of significance, and double precision accuracy.

Single row numeric functions

Number Systems. Both numbers are positive

Number System. Introduction. Decimal Numbers

Package Brobdingnag. R topics documented: March 19, 2018

Digital Fundamentals

Experimental Methods I

Language Reference Manual simplicity

4 Operations On Data 4.1. Foundations of Computer Science Cengage Learning

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

Divide: Paper & Pencil

CSCI 402: Computer Architectures. Arithmetic for Computers (3) Fengguang Song Department of Computer & Information Science IUPUI.

Computer (Literacy) Skills. Number representations and memory. Lubomír Bulej KDSS MFF UK

Bits, Words, and Integers

Floating Point. CSE 351 Autumn Instructor: Justin Hsia

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

UNIT 7A Data Representation: Numbers and Text. Digital Data

Julia Calculator ( Introduction)

Programming Using C Homework 4

Sketchpad Graphics Language Reference Manual. Zhongyu Wang, zw2259 Yichen Liu, yl2904 Yan Peng, yp2321

Chapter 2. Positional number systems. 2.1 Signed number representations Signed magnitude

Python Intro GIS Week 1. Jake K. Carr

Chapter 5 : Computer Arithmetic

Transcription:

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 2N 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., 1 1 0 1 2 =??? 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., 1 1 0 1 2 =??? 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., 1 1 0 1 2 =??? 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., 1 1 0 1 2 =??? 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., 1 1 0 1 2 =??? 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., 1 1 0 1 2 =??? 10 1 * 2 0 = 1 + 0 * 2 1 = 0 + 1 * 2 2 = 4 + 1 * 2 3 = 8 13

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. 13 mod 2 = 1 13 2 = 6 6 mod 2 = 0 6 2 = 3 3 mod 2 = 1 3 2 = 1 1 mod 2 = 1 1 2 = 0 1 1 0 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., 00001111 2 = 15 10 ; 10001111 = -15 10 Yields 128 negative, 128 positive values but this means we have +/- 0 (10000000, 00000000), wasting one value! So use two s complement

Two s Complement Notation To negate a binary number: Flip the bits Add 1 00001111 11110000 11110001 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 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1

Two s Complement Subtraction 15 15 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 0

Two s Complement Subtraction 15 15 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 1 0 0

Two s Complement Subtraction 15 15 1 1 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 0 0 0

Two s Complement Subtraction 15 15 1 1 1 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 0 0 0 0

Two s Complement Subtraction 15 15 1 1 1 1 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 0 0 0 0 0

Two s Complement Subtraction 15 15 1 1 1 1 1 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 0 0 0 0 0 0

Two s Complement Subtraction 15 15 1 1 1 1 1 1 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0

Two s Complement Subtraction 15 15 1 1 1 1 1 1 1 0 0 0 0 1 1 1 1 + 1 1 1 1 0 0 0 1 0 0 0 0 0 0 0 0 (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 = 6.626196e-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') > 100000000000000000000 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 3.141592653589793 >>> "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 3.141592653589793 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.0625, 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 (1815-1864)

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