COP 3223H meeting -*- Outline -*- * Friday Problems with Python Numeric Types and Expressions ** A few left over points *** floating point numbers are approximations ------------------------------------------ FLOATING POINT 'e' (or 'E') NOTATION >>> 0.2718e1 >>> 3.0e-4 ------------------------------------------ ... 2.718 == 2.718e0 ... 0.0003 Q: What does that e mean following the number? The exponent (following e) is a power of 10 i.e., eX means "times 10**X" ------------------------------------------ FLOATING POINT NUMBERS ARE APPROXIMATIONS NOT EXACT! 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 ------------------------------------------ Q: For each of these, what should the mathematical answer be? 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 ** Problems ------------------------------------------ SOME PROBLEMS FOR YOU Define functions to solve these problems: 1. How long would it take for you to drive a distance of D miles at a speed of V miles/hour? ------------------------------------------ It may help to think about the units! ------------------------------------------ MORE PROBLEMS 2. What is the energy (in Joules) needed to heat a given volume of water a given number of degrees Celsius? ------------------------------------------ Note: the heat-capacity of Water is 4.186 Joules/(grams * degC) and note that 1ml of water weighs 1 gram It is best to define the heat capacity of water in a variable: HEAT_CAPACITY = 4.186 # Joules/(grams * degC) ------------------------------------------ MORE PROBLEMS 3. Given the dimensions of a TV screen in inches, what is the diagonal measure of that screen? ------------------------------------------ Can use the Pythagorean theorem (a**2 + b**2 = c**2) to solve for c (the diagonal) ------------------------------------------ MORE PROBLEMS 4. Given an age, in years, return the number of miles that a person that age has traveled around the sun. ------------------------------------------ Note that 1 AU = distance from earth to the sun is 9.2955807e7 miles Assume the earth's orbit is roughly circular ------------------------------------------ MORE PROBLEMS 5. If you could stand on the surface of Pluto, what would a scale you brought with you say you weighed? ------------------------------------------ Pluto's mass is about 1.303e22 kg Pluto's radius is about 1187 km Netwon's grav. constant is 6.674e-11 N*m/kg**2 1 N = 0.224809 foot-pound Newton's law: F = G*(m1*m2/r**2)