CS195, Class 12: Representing Reals Topics: * Representing values other than integers * Simple techniques for representing reals * Standard mechanisms for representing reals * Variants * Play with Java Notes: * b2d and d2b are due today (BEFORE midnight). * Survey on time taken. * New assignment: Read K&R 2 What did you learn from this assignment? * "C is stupid" * "I like Java" * "Daren Brantley is God" * "God is Nice" * How do you represent numbers greater than 2^31 using regular ints? + Use longs internally + Use unsigned ints internally * "Ask Sam questions early and he's likely to fix the stupid parts of the assignment" ---------------------------------------- Moving on to a related topic: How do we represent values that are not integers? * Fractional values * Complex numbers * Real numbers * Characters and strings * Booleans * All sorts of programmer-defined constructs * Multimedia Some of these values can be mapped onto the integers and just use the integer representation and we can use our integer encoding. * Most common experience: Characters * ASCII: A mapping of characters onto the integers * UNICODE: More characters, more bits * EBCDIC Strings? * Do it character by character * How do you know when you hit the end? + Character number 0 represents the end (C's technique) Not much space. Flexible. Time consuming to find length. (Is this a common operation?) + Use a length as the first byte or so (Pascal's technique) Requires some prior knowledge Gives a maximum length (the largest length you can rep.) + Choose a fixed maximum length and use that every time. Inflexible (requires maximum length) Lots of wasted space. Reals * There is no natural mapping of the reals onto the integers. * Observation: If you only have a fixed number of bits for reals, then you'll need to approximate most reals. * Sam's claim: If you use a fixed number of bits, you are unlikely to represent irrational numbers. * Hence, all representable reals are "rational": Can be expressed as fractions. * Idea: Represent reals as fractions N bits for numerator (signed integer) M bits for denominator (unsigned integer) * Strengths of this technique? + 1.2 is hard to represent as a fraction? 12/10 * Weaknesses of this technique? + Might give bizarre results in approximating extreme values. + Potentially hard to do operations - Addition now requires multiplication + M can potentially be 0. + Incredibly redundant * Fixed point numbers Some number of bits represent "whole part" Some number of bits represent "fractional part" An implicit "binary point" resides between the two * Strengths + Fixed storage + Easy to understand + Addition is pretty easy (as long as we've handled negatives) * Weaknesses + Does a bad job on small numbers. + Too much precision for large numbers. + Comparatively small range of numbers. + Doesn't work nicely for 1.2 4's 2's 1's . 1/2's 1/4's 1/8's What's the solution? Don't try to solve the problem on our own. "Scientists" use scientific notation 1.231231 x 10^2342 Four parts: * An exponent * Base of exponentiation * "Mantissa": Number with both whole and fractional parts * Signs (of nubmer and exponent) -3.5 * 10^-21 IEEE Standards for Single-Precision and Double-Precision Floating Point Numbers Single-Precision: 32 bits 1 bit for sign of mantissa 8 bits for exponent (-126 to 127): 254 23 bits for mantissa Make the base of exponentiation 2 To represent numbers from -126 to 127, uses "bias-127" + Take the number to represent, add 127, represent unsigned Some people let you write the following in scientific notation 4232.121 x 10^123 If we allow different numbers of digits before the binary point, we'll raise a host of problems. 11101.1001 x 2^12 Interesting restriction: 1.0 <= mantissa < 2.0 1.11011001 x 2^16 Since the leftmost bit (the one before the decimal point) is always 1, you don't need to represent it. "The hidden bit" Problems: * How do you represent 0? * There are 254 valid exponents Two special exponents require different interpretation Exponent: 00000000: Use 2^-126 Interpret mantissa as having no "hidden bit", all following the "binary point" Mantissa is 100000000000...000 1/2 * 2^-126 = 5 * 2^-127 Mantissa and exponent all 0's: 0 Exponent: 11111111: "Agh, something's screwy" * Mantissa all 0's: Infinity * Anything else: NaN