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 35<br />

Let’s have a closer look at this function. The <strong>Julia</strong> function factorial(n) computes<br />

the factorial if n is an <strong>in</strong>teger, otherwise it computes the gamma function at n +1.If<br />

we call f <strong>with</strong> an <strong>in</strong>teger <strong>in</strong>put, then the above code will compute n n and factorial(n)<br />

us<strong>in</strong>g <strong>in</strong>teger arithmetic. Then it will divide these numbers, and to do so it will convert<br />

them to float<strong>in</strong>g-po<strong>in</strong>t numbers. Let’s compute f(n) = nn as n =1, ..., 17:<br />

n!<br />

In [2]: for n <strong>in</strong> 1:17<br />

pr<strong>in</strong>tln(f(n))<br />

end<br />

1.0<br />

2.0<br />

4.5<br />

10.666666666666666<br />

26.041666666666668<br />

64.8<br />

163.4013888888889<br />

416.1015873015873<br />

1067.6270089285715<br />

2755.731922398589<br />

7147.658895778219<br />

18613.926233766233<br />

48638.8461384701<br />

127463.00337621226<br />

334864.627690599<br />

0.0<br />

-8049.824661838414<br />

Notice that <strong>Julia</strong> computes 1616<br />

16!<br />

as 0.0, and passes the error to the next calculation.<br />

Where exactly does the error occur? <strong>Julia</strong> can compute 16! correctly, however 16 16<br />

results <strong>in</strong> overflow <strong>in</strong> <strong>in</strong>teger arithmetic. Below we compute 15 15 and 16 16 :<br />

In [3]: 15^15<br />

Out[3]: 437893890380859375

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

Saved successfully!

Ooh no, something went wrong!