06.09.2021 Views

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 1. INTRODUCTION 34<br />

and realize that the first 8 digits of the sum (from right to left), which is what the computer<br />

can only represent (ignor<strong>in</strong>g the red digit 1), is (00000000) 2 . So just like 12 + (−12) = 0 <strong>in</strong><br />

base 10, the sum of the representations of these numbers is also 0.<br />

We can repeat these calculations <strong>with</strong> 64-bits, us<strong>in</strong>g <strong>Julia</strong>. The function bitstr<strong>in</strong>g outputs<br />

the digits of an <strong>in</strong>teger, us<strong>in</strong>g two’s complement for negative numbers:<br />

In [1]: bitstr<strong>in</strong>g(12)<br />

Out[1]: "0000000000000000000000000000000000000000000000000000000000001100"<br />

In [2]: bitstr<strong>in</strong>g(-12)<br />

Out[2]: "1111111111111111111111111111111111111111111111111111111111110100"<br />

You can verify that the sum of these representations is 0, when truncated to 64-digits.<br />

Here is another example illustrat<strong>in</strong>g the advantages of two’s complement. Consider −3<br />

and 5, <strong>with</strong> representations,<br />

−3 = (11111101) 2 and 5 = (00000101) 2 .<br />

The sum of −3 and 5 is 2; what about the b<strong>in</strong>ary sum of their representations? We have<br />

(11111101) 2 + (00000101) 2 =(100000010) 2<br />

and if we ignore the n<strong>in</strong>th bit <strong>in</strong> red, the result is (10) 2 , which is <strong>in</strong>deed 2. Notice that if<br />

we followed the same approach used <strong>in</strong> the float<strong>in</strong>g-po<strong>in</strong>t representation and allocated the<br />

leftmost bit to the sign of the <strong>in</strong>teger, we would not have had this property.<br />

We will not discuss <strong>in</strong>teger representations and <strong>in</strong>teger arithmetic any further. However<br />

one useful fact to keep <strong>in</strong> m<strong>in</strong>d is the follow<strong>in</strong>g: <strong>in</strong> two’s complement, us<strong>in</strong>g 64<br />

bits, one can represent <strong>in</strong>tegers between −2 63 = −9223372036854775808 and 2 63 − 1 =<br />

9223372036854775807. Any <strong>in</strong>teger below or above yields underflow or overflow error.<br />

n<br />

Example 10. From Calculus, we know that lim n<br />

n−>∞ = ∞. Therefore, comput<strong>in</strong>g nn for<br />

n! n!<br />

large n will cause overflow at some po<strong>in</strong>t. Here is a <strong>Julia</strong> code for this calculation:<br />

In [1]: f(n)=n^n/factorial(n)<br />

Out[1]: f (generic function <strong>with</strong> 1 method)

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!