{smcl} {bf:Mata course: Accuracy 1: multiplication and addition} {hline} {title:1. Base 10 vs. base 2 (binary)} 0 1/8 = .001 2/8 = .01 3/8 = .011 4/8 = .1 5/8 = .101 6/8 = .11 7/8 = .111 .1 = 0.00011001100110011001... .2 = 0.0011001100110011001... .3 = 0.010011001100110011... .4 = 0.011001100110011001... .5 = 0.1 .6 = 0.10011001100110011... .7 = 0.10110011001100110... .8 = 0.11001100110011001... .9 = 0.11000110011001100... {title:2. Floating-point arithmetic (decimal)} {p 4 4 2} Write number as {it:z} = ({it:s}, {it:x}, {it:y}) = {it:s}*{it:x}*10^{it:y} {p 4 4 2} where {it:s} = 1 or -1 0 <= {it:x} < 10 {p 4 4 2} Example: .5 = (1, 5, -1) = 5*10^(-1) {title:3. Fixed-precision floating-point arithmetic (decimal)} {p 4 4 2} {it:x} is recorded as {it:d} digits, say 5. {p 4 4 2} Example: .5 = (1, 5.0000, -1) = 5*10(^-1) 1/3 = (1, 3.3333, -1) = 3.3333*10^(-1) {title:4. Fixed-precision floating-point arithmetic (binary)} {p 4 4 2} Write number as {it:z} = ({it:s}, {it:x}, {it:y}) = {it:s}*{it:x}*2^{it:y} {p 4 4 2} where {it:s} = 1 or -1 0 <= {it:x} < 2 {p 4 4 2} {it:x} is recorded as {it:d} binary digits, say 24 (float) or 52 (double) {title:5. Multiplication} {p 4 4 2} Method: {it:z1} = {it:s1}*{it:x1}*{it:b}^{it:y1} {it:z2} = {it:s2}*{it:x2}*{it:b}^{it:y2} {it:z1}*{it:z2} = {it:s1}*{it:s2} * {it:x1}*{it:x2}*{it:b}^({it:y1}+{it:y2}) {p 8 8 2} The {it:x1}*{it:x2} part will have more than {it:d} digits.{break} Truncate or round or {it:d} digits. {p 4 4 2} Result: {p 8 8 2} In the worst case, the error is one binary digit. {p 8 8 2} If our precision is {it:d}, we have {it:d}-1 or {it:d} precision result. {title:6. Addition} {p 4 4 2} Method: {it:z1} = {it:s1}*{it:x1}*10^{it:y1} {it:z2} = {it:s2}*{it:x2}*10^{it:y2} {p 8 8 2} Add the usual way.{break} Truncate or round to {it:d} digits. {p 4 4 2} Example: 12.345 + .12345: 12.345 .12345 ---------- 12.46845 -- --- {p 4 4 2} Result: {p 8 8 2} Given {it:d} digits of precision, our result will have between 0 and {it:d} digits of precision. {title:7. Summary} {p 4 8 2} 1. Multiplication (and division) introduce little (next to none) inaccuracy. {p 4 8 2} 2. Addition (and subtraction) have the potential to introduce lots of inaccuracy. {p 4 8 2} {it:Example:} {p 8 8 2} Do not code {cmd:1 - normal(}{it:z}{cmd:)} {p 8 8 2} Code {cmd:normal(-}{it:z}{cmd:)} {title:8. Implication for complex operators} {p 4 8 2} 1. What is said above about addition applies complex addition. {p 4 8 2} 2. What is said above about addition applies to complex multiplication, because complex multiplication involves addition: ({it:a} + {it:b}{it:i})(c + d{it:i}) = ({it:ac}-{it:bd}) + ({it:ad}+{it:bc}){it:i} {title:9. Implication for matrix operators} {p 4 8 2} 1. What is said above about addition applies to matrices. {p 4 8 2} 2. What is said above about addition applies to matrix multiplication because matrix multiplication involves addition. {hline} {bf:{view talk.smcl:Top}}