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

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

Transcript of Computer Science 121 Scientific Computing Winter 2014 Chapter 3 Simple Types: Numbers, Text,...

Computer Science 121

Scientific ComputingWinter 2014

Chapter 3Simple 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 = ??? 101 * 20 = 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 = ??? 101 * 20 = 1

+ 0 * 21 = 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 = ??? 101 * 20 = 1

+ 0 * 21 = 0+ 1 * 22 = 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 = ??? 101 * 20 = 1

+ 0 * 21 = 0+ 1 * 22 = 4+ 1 * 23 = 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 = ??? 101 * 20 = 1

+ 0 * 21 = 0+ 1 * 22 = 4+ 1 * 23 = 8

____________

13

13 mod 2 = 113 ÷ 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

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 → 28 = 256 possible values

● Can use leftmost bit for sign (+/-)● E.g., 000011112 = 1510; 10001111 = -1510

● 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

0 0 0 0 1 1 1 1

+ 1 1 1 1 0 0 0 1________________________________

• 15 – 15

• 15 – 15

Two’s Complement Subtraction

0 0 0 0 1 1 1 1

+ 1 1 1 1 0 0 0 1________________________________

0

• 15 – 15

Two’s Complement Subtraction

1

0 0 0 0 1 1 1 1

+ 1 1 1 1 0 0 0 1________________________________

0 0

• 15 – 15

Two’s Complement Subtraction

1 1

0 0 0 0 1 1 1 1

+ 1 1 1 1 0 0 0 1________________________________

0 0 0

• 15 – 15

Two’s Complement Subtraction

1 1 1

0 0 0 0 1 1 1 1

+ 1 1 1 1 0 0 0 1________________________________

0 0 0 0

• 15 – 15

Two’s Complement Subtraction

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

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

• 15 – 15

Two’s Complement Subtraction

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

• 15 – 15

Two’s Complement Subtraction

1 1 1 1 1 1 1

0 0 0 0 1 1 1 1

+ 1 1 1 1 0 0 0 1________________________________

0 00 0 0 0 0 0

• 15 – 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 = 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: too big>> 1 / 0

ans = Inf

NaN: undefined>> 0 / 0

ans = 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

ans = 3.14159…

>> ‘pi’

ans = pi

3.2 Text (Strings)

• For apostrophe, use two single quotes: >> ‘Why can’’t anything be simple?’ans = Why can’t anything be simple?

• Don’t try to put a newline into quoted text: >> ‘Four score and seven years ago ourError: A MATLAB string constant is not terminated properly.

3.2 Text (Strings)

• Internally, Matlab (computer) stores text as sequence of numbers (sequence of sequence of bits), each representing a character:

>> ‘foo’ + ‘bar’

ans = 200 208 225

• With 1 byte, can represent 256 unique characters – okay for English, but not for other languages (e.g., thousands of Chinese characters).

• Unicode uses 31 bits, yielding ~65,000 chars.

3.3 Collections of Numbers and Plotting

• Sequences of numbers – a.k.a. vectors – are the most common kind of data in scientific computing

• Matlab uses square brackets to represent vectors:>> [1, 5, 7, 9]

ans = 1 5 7 9

3.3 Collections of Numbers and Plotting

• Commas are optional:>> [1 5 7 9]

ans = 1 5 7 9

• Matlab’s fundamental power : operations on entire vectors at once: >> 3 * [1 5 7 9]

ans = 3 15 21 27

A Note on Notation

• Q.: What is the difference between:

>> 3 * [1 5 7 9]

and

>> 3 .* [1 5 7 9]

• A.: None, because 3 is a scalar (single number)

• Dot (.) in front operator indicates “element-by-element” operation.

• With scalar and vector, element-by-element is automatic, so dot isn’t needed

• With two vectors, dot is necessary for element-by-element:

>> [1 5 7 9].^2

ans = 1 25 49 81

• Q: How many tokens in [1 5 7 9].^2

Useful Vector Operations

>> a = [1, 5, 7, 9];>> length(a)ans = 4>> sum(a)ans = 22>> prod(a)ans = 315

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: rem , mod

Plotting

>> time = [0 1 2 3.5 7];>> temp = [93.5 90.6 ... 87.7 83.9 76.6];>> plot(time, temp, ‘-o’)

(additional values shown)

Constructing Sequences of Numbers

• Colon operator:

>> colon(1,.5, 5)ans = 1.000 1.500 2.000 2.500 3.000 3.500 4.000 4.500 5.000

>> [1:.5:5]ans = % (same)

>> colon(.1, .5, 2)ans = .1 .6 1.1 1.6 % what about 2?

• Default increment is 1:

>> [1:5]ans = 1 2 3 4 5

• Goin’ down:>> [3:-.5:1]ans = 3.000 2.500 2.000 1.500 1.000

• Concatenating vectors:

>> [1:5 7:9]ans = 1 2 3 4 5 7 8 9

• The empty vector:>> [1:5 []]ans = 1 2 3 4 5

• Scalar as length-one vector:>> length(5)ans = 1

3.4: Booleans: True or False

• Boolean (true/false) values (bits) are useful everywhere in computer science

• Matlab reports true as 1, false as 0

>> isprime(7)

ans = 1

• Certain operators are inherently Boolean>> [ 3 < 4, 3 > 4, 3 <= 4, 3 >= 4, 3 == 4]

ans = [1 0 1 0 0]

Boolean Operations on Vectors and Strings

• Can compare vectors, as long as they’re same length:

>> [1 2 3] == [3 2 1]ans = [0 1 0]>> [1 2 3 4] == [1 2 3]Error: …

Boolean Operations on Vectors and Strings

• For string comparisons, want to compare whole string, not just letters

>> ‘foo’ == ‘moo’ans = [0 1 1]strcmp(‘foo’, ‘moo’)ans = 0>> strcmp(‘bar’, ‘bar’)ans = 1

• Unequal lengths → false (not error):

>> strcmp(‘foo’, ‘foobar’)ans = 0

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) & (myhum >= 4)ans = 0>> (mymath >= 1) & (mycsci >=1 | mymath >=2)ans = 1

Logical Operations