------------------------- Basic Arithmetic Operations Terminology ------------------------- -Ian! D. Allen - idallen@idallen.ca - www.idallen.com Remember this from grade school? augend + addend = sum minuend - subtrahend = difference multiplicand * multiplier = product dividend / divisor = quotient (+ remainder) Remember how to do math with numbers with exponents? Notation: 2**10 means "2 to the power 10" Notation: 10**3 means "10 to the power 3" Notation: 2**-10 means "2 to the power -10, or: 1 divided by 2**10" 2**-9 = 1 / 2**9 [reciprocal] 2**10 * 2**15 = 2**25 [add the exponents] 2**15 / 2**10 = 2**15 * 2**-10 = 2**5 2**10 / 2**12 = 2**10 * 2**-12 = 2**-2 = 1 / 2**2 = 1/4 = 0.25 (2**3)**4 = 2**3 * 2**3 * 2**3 * 2**3 = 2**12 [multiply the exponents] 8 * 16**3 = 2**3 * (2**4)**3 [rewrite as powers of two] = 2**3 * 2**12 [multiply exponents] = 2**15 [add exponents] = 32,768 [a famous number in computer science] New and old prefixes: http://en.wikipedia.org/wiki/Binary_prefix SI prefixes (e.g. 5 kilometres, 100 megahertz): 10**3 = 1,000 = Kilo 10**6 = 1,000,000 = Mega 10**9 = 1,000,000,000 = Giga 10**12 = 1,000,000,000,000 = Tera 10**15 = 1,000,000,000,000,000 = Peta There are 1,000,000 Mega in a Tera: 10**12 / 10**6 = 10**6 10**-3 = 1/1,000 = Milli 10**-6 = 1/1,000,000 = Micro 10**-9 = 1/1,000,000,000 = Nano 10**-12 = 1/1,000,000,000,000 = Pico There are 1,000,000 Pico in a Micro: 10**-6 / 10**-12 = 10**6 The SI prefixes and abbreviations have also been used historically in a confusing way to indicate binary multipliers, e.g. 8 GB of memory should really be written as 8 GiB not 8 GB. Eventually, someone decided to stop confusing the two, and new "binary only" prefixes were invented, but are not widely used yet: New binary prefixes (e.g. 8 GiB RAM is more correct than 8 GB RAM): 2**10 = 1,024 = Kibi (Kilo-Binary) 2**20 = 2**10 * 2**10 = Kibi * Kibi = 1,048,576 = Mebi (Mega-Binary) 2**30 = 2**10 * 2**20 = Kibi * Mebi = 1,073,741,824 = Gibi (Giga-Binary) 2**40 = 2**10 * 2**30 = Kibi * Gibi = 1,099,511,627,776 = Tebi (Tera-Binary) Computer memory is always measured using binary powers of two, even though history has us writing the measurement using SI power-of-ten. An 8 GB memory module is actually 8 GiB (power-of-two), and you should write 8 GiB not 8 GB. File sizes may be measured using either method; many operating systems show file sizes using power-of-two units incorrectly labelled with SI abbreviations. A 175 KB file is really 175 KiB. Disk drive manufacturers always use SI power-of-ten units, but since operating systems often use binary units you may find that your 2 TB (power-of-ten) disk drive displays as only 1.8 TiB (power-of-two) when you plug it in. Confusingly, your computer will incorrectly display the disk size using the historic SI abbreviation 1.8 TB instead of the correct binary 1.8 TiB. Everything else uses standard SI prefixes. 2**32 = 2**2 * 2**30 = 4 Gibi [because 2**30 -> Gibi] A 2 terabyte disk drive (or file) may appear to be only 1.8 Ti bytes when displayed by an operating system that is using the larger binary prefixes instead of the SI (power of ten) prefixes. Decimal and Binary fractions: Fractions are simply negative powers of the base: 1.0000 decimal is 1 * 10**0 or 1 0.1000 decimal is 1 * 10**-1 or 1/10 or 0.1 decimal 0.0100 decimal is 1 * 10**-2 or 1/100 or 0.01 decimal 0.0010 decimal is 1 * 10**-3 or 1/1000 or 0.001 decimal 0.0001 decimal is 1 * 10**-4 or 1/10000 or 0.0001 decimal ...etc... 1.0000 binary is 1 * 2**0 or 1 0.1000 binary is 1 * 2**-1 or 1/2 or 0.5 decimal 0.0100 binary is 1 * 2**-2 or 1/4 or 0.25 decimal 0.0010 binary is 1 * 2**-3 or 1/8 or 0.125 decimal 0.0001 binary is 1 * 2**-4 or 1/16 or 0.0625 decimal ...etc... Normalized scientific notation: Notation: 123.456E7 means 123.456 times 10**7 (10 to the power 7) 123.456e7 means the same (lower-case E) A normalized number has only one non-zero digit on the left of the decimal, e.g. 123.456e7 normalized is 1.23455e9 and 0.0123e5 normalized is 1.23e3. (Zero is an exception: 0.0e0.) In binary floating point, 1101.111(2) normalized is 1.101111 times 2**3 (2 to the power 3). 0.01010(2) normalized is 1.010 times 2**-2 (2 to the power -2, or 1/(2**2)). The digit on the left is always a 1 in normalized binary, except for the zero case. Every floating-point number has a unique normalized representation. Expressing binary using hexadecimal constants in computer programs You may write a hexadecimal integer constant F5EA in a computer program using the 0x syntax "0xF5EA", e.g. int x = 0xf5ea; You may write a hexadecimal floating-point constant "1.F5EA times 2 to the power 5" using the 0x and "p" exponent notation "0x1.F5EAP5" e.g. double x = 0x1.f5eap5; or float x = 0x1.f5eap5f; Constants may use either upper-case or lower-case hexadecimal letters. -- | Ian! D. Allen - idallen@idallen.ca - Ottawa, Ontario, Canada | Home Page: http://idallen.com/ Contact Improv: http://contactimprov.ca/ | College professor (Free/Libre GNU+Linux) at: http://teaching.idallen.com/ | Defend digital freedom: http://eff.org/ and have fun: http://fools.ca/