# COMP2421 Assignment 1

MA Mingyu (Derek) | 14110562D | Sep 17, 2017
derek.ma | derek.ma@connect.polyu.hk

## Question 1

Convert the binary number to decimal number:
$01110001_{two} = 1*2^6+1*2^5+1*2^4+1*2^0 = 113_{ten}$. For 8-bit word, complement to $$2_{ten}^8 = 256_{ten}$$:
$256_{ten} - 113_{ten} = 143_{ten}$. Thus the value of 2's complement of $$01110001_{two}$$ in decimal is $$143$$.

## Question 2

According to the straightforward approach for making two's complement, take the bitwise complement of the value and then add 1:

Bitwise complement of $$10011101_{two}$$ is: $$01100010_{two}$$ ;
then add 1: $$01100010_{two} + 1 = 01100011_{two}$$.

Here, we get the 2's complement value of the given value in the question in binary, then convert it to decimal:

$01100011_{two} = 1*2^6+1*2^5+1*2^1+1*2^0 = 99_{ten}$. Thus, the value of 2's complement of $$10011101_{two}$$ in decimal is $$99$$.

## Question 3

The original real number is $$111.111_{ten}$$.

Get binary number for $$111_{ten}$$:
$$111 = 55*2+1$$
$$55 = 27*2+1$$
$$27 = 13*2+1$$
$$13 = 6*2+1$$
$$6 = 3*2+0$$
$$3 = 1*2+1$$
$$1 = 0*2+1$$

$111_{ten} = 1101111_{two}$

Get binary number for $$.111_{ten}$$:
$$0.111*2 = 0.222$$
$$0.222*2 = 0.444$$
$$0.444*2 = 0.888$$
$$0.888*2 = 1.776$$
$$0.776*2 = 1.552$$
$$0.552*2 = 1.104$$
$$0.104*2 = 0.208$$
$$0.208*2 = 0.416$$
...

$.111_{ten} = .00011100011010100111111011111001110..._{two}$

So we can know:
$111.111_{ten} = 1101111.00011100..._{two} = (-1)^0 *1.10111100011100011..._{two}*2^6$;
$$S=0$$;
$$Fraction=101111000111000110101001111...$$;
$$e=6$$;
$$Exponent = e+bias = 6+127 = 133_{ten} = 10000101_{two}$$.

So the single-precision float is:
0 10000101 10111100011100011010100.

After optional rounding process, it can be:
0 10000101 10111100011100011010101.

## Question 4

Original single-precision float is:

$1 01111111 01010101010101010101010$
, so we can get:
$S = 1$;
\begin{align} Fraction &= .01010101010101010101010_{two} \\ &= 2^{-2}+2^{-4}+2^{-6}+2^{-8}+2^{-10}+2^{-12}+2^{-14}+2^{-16}+2^{-18}+2^{-20}+2^{-22} \\ &= 0.333333254_{ten} \end{align};
$Exponent = 01111111_{two} = 127_{ten}$
. Then calculate value of $$e$$ by subtracting the bias:
$e = Exponent - 127 = 127-127 = 0$.

Finally, the real number is:
\begin{align} x &= (-1)^S * (1+Fraction)*2^{e} \\ &= (-1)^1* 1.333333254 * 2^0 \\ &= - 1.333333254_{ten} \end{align}

## Question 5

### Represent pi using single-precision

The original real number is $$3.1415926535_{ten}$$.

Get binary number for $$3_{ten}$$:
$$3 = 1*2+1$$
$$1 = 0*2+1$$

$3_{ten} = 11_{two}$

Get binary number for $$0.1415926535_{ten}$$:
$$.1415926535*2 = .283185307$$
$$.283185307*2 = .566370614$$
$$.566370614*2 = 1.132741228$$
$$.132741228*2 = .265482456$$
$$.265482456*2 = .530964912$$
$$.530964912*2 = 1.061929824$$
$$.061929824*2 = .123859648$$
...

$.1415926535_{ten} = 0.001001000011111101101010100..._{two}$

So we can know:
\begin{align} 3.1415926535_{ten} &= 11.001001000011111101101..._{two} \\ &= (-1)^0 *1.1001001000011111101101..._{two}*2^1 \end{align};
$$S=0$$;
$$Fraction=1001001000011111101101010100010000010001011...$$;
$$e=1$$;
$$Exponent = e+bias = 1+127 = 128_{ten} = 10000000_{two}$$.

So the single-precision float for pi with 10 decimal places is:
0 10000000 10010010000111111011010
; while after rounding the last digit of the fraction, it's:
0 10000000 10010010000111111011011.

### Evaluate accuracy

#### Accuracy for not rounded single-precision float

Convert the above single-precision float back to a real number:
$$.10010010000111111011010_{two} = 0.5707962512969970703125$$
\begin{align} x &= (-1)^0*1.5707962512969970703125*2^{128-127} \\ &= 3.14159250259 \end{align}

The single-precision value without rounding is: $$3.14159250259$$. Then calculate the accuracy:

\begin{align} Error &= |single-precision value - actual value| * 100\% / actual value \\ &= \frac{|3.14159250259 - 3.1415926535|}{3.1415926535} *100\% \\ &= \frac{1.5091e-7}{3.1415926535} *100\% \\ &= 0.0000048036145 \% \end{align}.

Thus we can know the accuracy for using single-precision to express Pi value without rounding with 10 decimal places: the error is $$0.0000048036145\%$$, the accuracy is $$99.9999951964\%$$.

#### Accuracy for rounded single-precision float

Convert the above rounded single-precision float back to a real number:

$$.10010010000111111011011_{two} = 0.57079637050628662109375$$
\begin{align} x &= (-1)^0*1.57079637050628662109375*2^{128-127} \\ &= 3.1415927410125732421875 \end{align}.

The single-precision value is: $$3.1415927410125732421875$$. Then calculate the accuracy:

\begin{align} Error &= |single-precision value - actual value| * 100\% / actual value \\ &= \frac{|3.1415927410125732421875 - 3.1415926535|}{3.1415926535} *100\% \\ &= \frac{.0000000875125732}{3.1415926535} *100\% \\ &= 0.00000278561172\% \end{align}.

Thus we can know the accuracy for using single-precision to express Pi value with 10 decimal places: the error is $$0.00000278561172\%$$, the accuracy is $$99.9999972144\%$$.

Notes: we can found that the accuracy after rounding is actually better than the solution without rounding.