COP 3223H meeting -*- Outline -*- * Python Numeric Types and Expressions A programming language always has a precise notation for everything, even numbers. For this we will use Python as a calculator... ** Numeric Types *** in Python ------------------------------------------ PYTHON NUMERIC TYPE HIERARCHY numbers.Number numbers.Integral int values: 0, 1, -1, 2, -2, 3, -3, ... operators: +, -, *, **, /, //, %, <<, >>, &, |, ^, ~ bool values: False, True operators: &, and, |, or, ^, ~, if else float (numbers.Real) values: 0.0, 3.14159, 0.2718e1, .112, 1e10, 5e-6 operators: +, -, *, **, /, //, % complex (numbers.Complex) values: 3.14j, 10j, 3.14+5.2j operators: +, -, *, **, /, //, % ------------------------------------------ False is treated as 0 in most contexts, True is like 1. The only difference is in conversion to strings (as in printing) We will deal with the Booleans in a future lecture Explain the meaning of the "e" or "E" in the floating point notation: 2.0e5 means 2 * 10**5 == 200000.0 3.0e-4 means 3 * 10**-4 == 0.0003 *** in C ------------------------------------------ NUMERIC TYPES IN C Several kinds of ints Signed (both positive and negative): short or short int int long or long int Unsigned (only positive): unsigned short or unsigned short int unsigned or unsigned int unsigned long or unsigned long int The number of bits varies by compiler but generally: short ~ 16 bits up to 32,767 int ~ 32 bits up to 2**31 ~ 2 billion long ~ 64 bits up to 2**63 ~ 10**18 int is usually most efficient for time ------------------------------------------ Q: How are integers different in Python? ------------------------------------------ FLOATING POINT TYPES IN C 2 kinds of floating point types in C: float ~ 32 bits double ~ 64 bits (like floats in Python) ------------------------------------------ *** Python numbers and operators Try the following in the interpreter Can have students guess the result... ------------------------------------------ EXAMPLES OF NUMBERS AND OPERATORS >>> -1 >>> 42 >>> 2.0e5 >>> 3.0e-4 >>> 0.2718e1 >>> 3.14159 * 1.0j >>> 7 + 3 >>> 7 - 3 >>> 2 * 3 >>> 8 * 5 ------------------------------------------ Q: So what does * do? multiplication ** Number representations and operators *** integer operators ------------------------------------------ MORE INTEGER OPERATORS >>> 7 / 3 >>> 7 // 3 >>> -7 / 3 >>> -7 // 3 >>> 3 // 2 >>> 3223 // 100 ------------------------------------------ Q: What is the difference between / and //? / gives an inexact result as a float, // gives the floor of the exact result as an int Note: in C, / on integers does something similar to // (same when both arguments are positive, whether to use floor or ceiling for negative args is implementation-dependent) ------------------------------------------ INTEGER MODULO OPERATOR (%) >>> 3 % 2 >>> (3//2)*2 + (3%2) >>> -3 // 2 >>> -3 % 2 >>> (-3 // 2)*2 + (-3 % 2) ------------------------------------------ For // the result is the floor of the inexact result, the greatest integer that is no greater than the inexact result, hence -7 // 3 is floor(-2.333) == -3, and 7 // 3 is floor(2.333) == 2. Q: Why is -3 // 2 equal to -2 and not -1? because the // operator returns the floor of -1.5. **** power operator ------------------------------------------ POWER OPERATOR >>> 2**3 >>> 5**2 >>> 5**3 >>> 10**3 >>> -3**2 >>> -3**3 >>> 1e3 ------------------------------------------ x**y is the same as pow(x,y) For integer arguments, this yields an integer. Q: What should Python do if we raise a number to a negative power? It should invert it, for y >= 0, x**(-y) = 1/(x**y) e.g., try 3**(-2) == 1/9 Note: you have to use a library function in C to do this, but it only works for floating point numbers. **** integer bitwise operators We sometimes want to manipulate bits directly, for example in order to represent set membership or to write low-level primitives on numbers ------------------------------------------ INTEGER REPRESENTATION Integers are (conceptually) represented by (infinite) bitstrings in 2's complement notation. 0 is represented by 000...000 1 is represented by 000...001 2 is represented by 000...010 3 is represented by 000...011 4 is represented by 000...100 -1 is represented by 111...111 -2 is represented by 111...110 -3 is represented by 111...100 The advantage of 2's complement notation is that the numbers can be treated as unsigned numbers by hardware ------------------------------------------ The representation of negative numbers has an infinite string of sign bits (conceptually). Q: What's the pattern for converting the binary representation to decimal? if bits is a positive number bits (without 1s at the left) then sum for i=0 to infinity: (bits[i]*(2**i)) (with bits numbered from the right with index 0) else # if the leftmost bits are all 1s 1 + (sum for i=0 to infinity: ((~bits[i])*2**i)) (the compliment of the number plus 1) ------------------------------------------ BITWISE OPERATORS ON INTEGERS ~ is the inversion of the bits of its argument >>> ~0 >>> ~1 >>> ~(-1) >>> ~2 >>> ~(-2) >>> ~3 >>> ~(~3) >>> ~(-4) ------------------------------------------ Q: What does ~ do? It returns the 2's compliment of the given argument This works out so that ~x is -(x+1) Note: same in C ------------------------------------------ THE & OPERATOR >>> 1 & 1 >>> 1 & -1 >>> 3 & 4 >>> 7 & 2 >>> 2 & 3 >>> 3 & 4 ------------------------------------------ Q: What does & do? Note: same in C ------------------------------------------ >>> 1 | 1 >>> 1 | -1 >>> 2 | 3 >>> 2 | 16 >>> 3 | 4 ------------------------------------------ Q: Why is 3|4 equal to the result shown? because of the 2's compliment representation 3 is 000...011 and 4 is 000...100 so the result is 000...111 which represents 7 Note: same in C *** floating point IEEE 754 is a floating point number standard C is more lax about what compilers can do, but generally doubles in C act like floats in Python. **** representation ------------------------------------------ FLOATING POINT NUMBERS Approximations of real numbers Represented (in IEEE 754 format) by: a sign bit (1 means negative) a binary exponent (11 bits representing -1022 to 1023) a mantissa (a 52 bit binary fraction, stored in 51 bits representing a fraction between 0.5 and 1.0) the meaning of S E M is (-1)**(S) * 2**(E) * M Can represent +/- 2**-1022 to +/- 2-(2**(-52))*2**1023 or about -1.0e308 to 1.0e308 >>> 1.0e308 >>> 1.0e309 >>> -1.0e309 >>> -1.0e308 >>> 1.0e-323 >>> 1.0e-324 >>> -1.0e-323 >>> -1.0e-324 ------------------------------------------ Since there is a sign bit, the ranges are symmetric around 0 floating point numbers are approximations of real numbers Q: What numbers cannot be represented as floats? numbers outside the range: those too big or too small as fractions ------------------------------------------ FLOATING POINT NUMBERS APPROXIMATE REALS They are not exact! >>> 1/9.232 * 9.232 >>> 1e-5 + 1e5 >>> 1e-9 + 1e9 >>> 1e-27 + 1e27 >>> 1 - 3*(4/3 - 1) >>> 0.1 + 0.1 + 0.1 ------------------------------------------ Note that while mathematically 1/9.232 * 9.232 should be 1.0, it is not Note the loss of precision in representing even 1e-9 + 1e9 Q: How many real numbers are there between 0 and 10**(308.25)? an uncountably infinite number Q: How many floats are there in that range? (2**2045)*(2**51) (plus a few more), which is big, but not infinite So each float represents a range of reals, not exactly. The approximate nature of floating point numbers means that some mathematical identities don't hold! Moral: if you need exact results (e.g., finance, proofs) use ints! The advantage of floats is that they are fast to compute with and for many problems we can live with the approximations ------------------------------------------ FLOATING POINT OPERATORS >>> 5.3 + 4.2 >>> 1.125 * 10.0 >>> 1.2345 / 10.0 >>> 12.5 // 10 ------------------------------------------ Q: What does // do for floats? same as for ints, it takes the floor of the result, but it returns a float, not an int ** errors ------------------------------------------ NUMERIC ERRORS >>> #Divison by zero: >>> 5 / 0 # error! >>> 1.0 / 0.0 # error! >>> 5 // 0 # error! >>> #Overflow: >>> 10.0**323 # error! >>> # However: >>> 10 ** 323 ------------------------------------------ Q: What's the difference in the last 2? ** Modeling with numbers ------------------------------------------ SOME QUESTIONS FOR YOU Calculate using Python: 1. How many steps do you need to take to walk 0.25 miles? 2. How many gallons are in a teaspoon? 3. How many acres is a baseball diamond? 4. How many acres is a soccer field? ------------------------------------------ notes: - a mile is 5280 feet - a teaspoon holds 1/6 of a fluid oz. there are 128 fluid oz in a gallon - the bases in baseball are 90 feet apart, an acre is 66 by 660 feet - the usual size of a soccer field is 105m x 68m or 115 yd x 74 yd.