forked from CalculusWithJulia/CalculusWithJulia.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumbers_types.html
1215 lines (927 loc) · 60.8 KB
/
numbers_types.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"
rel="stylesheet">
<style>
.julia {display: block; font-family: "Source Code Pro";
color:#0033CC;
}
.hljl {font-family: "Source Code Pro";
color:#0033CC;
}
body { padding-top: 60px; }
h5:before {content:"\2746\ ";}
h6:before {content:"\2742\ ";}
pre {display: block;}
th, td {
padding: 15px;
text-align: left;
border-bottom: 1px solid #ddd;
}
tr:hover {background-color: #f5f5f5;}
.admonition-title:before {content:"\2746\ ";}
.admonition-title { color:#0033CC}
</style>
<!-- .julia:before {content: "julia> "} -->
<style></style>
<script src="https://code.jquery.com/jquery.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
tex2jax: {
inlineMath: [ ["\$","\$"], ["\\(","\\)"]]
},
displayAlign: "left",
displayIndent: "5%"
});
</script>
<!-- not TeX-AMS-MML_HTMLorMML-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-MML-AM_CHTML" async></script>
</script>
<script>
window.PlotlyConfig = {MathJaxConfig: 'local'}
</script>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script type="text/javascript">
$( document ).ready(function() {
$("h1").each(function(index) {
var title = $( this ).text()
$("#page_title").html("<strong>" + title + "</strong>");
document.title = title
});
$( "h2" ).each(function( index ) {
var nm = $( this ).text();
var id = $.trim(nm).replace(/ /g,'');
this.id = id
$("#page_dropdown").append("<li><a href='#" + id + "'>" + nm + "</a></li>");
});
$('[data-toggle="popover"]').popover();
});
</script>
</head>
<body data-spy="scroll" >
<nav class="navbar navbar-default navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li><a href="#" id="page_title"></a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Jump to... <span class="caret"></span></a>
<ul class="dropdown-menu" role="menu" id="page_dropdown"></ul>
</li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
</nav>
<header>
</header>
<div class="title">
</div>
<div class="container-fluid">
<div class="span10 offset1">
<h1>Number systems</h1>
<p>In mathematics, there are many different number systems in common use. For example by the end of pre-calculus, all of the following have been introduced:</p>
<ul>
<li><p>The integers, <span class="math">$\{\dots, -3, -2, -1, 0, 1, 2, 3, \dots\}$</span>;</p>
</li>
<li><p>The rational numbers, <span class="math">$\{p/q: p, q \text{ are integers}, q \neq 0\}$</span>;</p>
</li>
<li><p>The real numbers, <span class="math">$\{x: -\infty < x < \infty\}$</span>;</p>
</li>
<li><p>The complex numbers, <span class="math">$\{a + bi: a,b \text{ are real numbers and } i^2=-1\}$</span>.</p>
</li>
</ul>
<p>On top of these, we have special subsets, such as the natural numbers <span class="math">$\{0, 1, 2, \dots\}$</span>, the even numbers, the odd numbers, positive numbers, non-negative numbers, etc.</p>
<p>Mathematically, these number systems are naturally nested within each other as integers are rational numbers which are real numbers, which can be viewed as part of the complex numbers.</p>
<p>Calculators typically have just one type of number–-floating point values. These model the real numbers. <code>Julia</code>, on other other hand, has a rich type system, and within that has many different number types. There are types that model each of the four main systems above, and within each type, specializations for how these values are stored.</p>
<p>Most of the details will not be of interest to all, and will be described later.</p>
<p>For now, let's consider the number 1. It can be viewed as either an integer, rational, real, or complex number. To construct "1" in each type within <code>Julia</code> we have these different styles:</p>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-n'>im</span>
</pre>
<pre class="output">
(1, 1.0, 1//1, 1 + 0im)
</pre>
<p>The basic number types in <code>Julia</code> are <code>Int</code>, <code>Float64</code>, <code>Rational</code> and <code>Complex</code>, though in fact there are many more, and the last two aren't even <em>concrete</em> types. This distinction is important, as the type of number dictates how it will be stored and how precisely the stored value can be expected to be to the mathematical value it models.</p>
<p>Though there are explicit constructors for these types, these notes avoid them unless necessary, as <code>Julia</code>'s parser can distinguish these types through an easy to understand syntax:</p>
<ul>
<li><p>integers have no decimal point;</p>
</li>
<li><p>floating point numbers have a decimal point (or are in scientific notation);</p>
</li>
<li><p>rationals are constructed from integers using the double division operator, <code>//</code>; and</p>
</li>
<li><p>complex numbers are formed by including a term with the imaginary unit, <code>im</code>.</p>
</li>
</ul>
<div class="alert alert-success" role="alert">
<div class="markdown"><p>Heads up, the difference between <code>1</code> and <code>1.0</code> is subtle (and even more so, as <code>1.</code> will parse as <code>1.0</code>).</p>
</div>
</div>
<p>Similarly, each type is printed slightly differently.</p>
<p>The key distinction is between integers and floating points. While floating point values include integers, and so can be used exclusively on the calculator, the difference is that an integer is guaranteed to be an exact value, whereas a floating point value is typically just an <em>approximate</em> value–-used to advantage, as floating point values can model a much wider set of numbers.</p>
<p>Now in nearly all cases, the differences are not noticed. Take for instance this simple calculation involving mixed types.</p>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nfB'>1.25</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-oB'>//</span><span class='hljl-ni'>2</span>
</pre>
<pre class="output">
3.75
</pre>
<p>The sum of an integer, a floating point number and rational number returns a floating point number without a complaint.</p>
<p>This is because behind the scenes, <code>Julia</code> will often "promote" a type to match, so for example to compute <code>1 + 1.25</code> the integer <code>1</code> will be promoted to a floating point value and the two values are then added. Similarly, with <code>2.25 + 3//2</code>, where the fraction is promoted to the floating point value <code>1.5</code> and addition is carried out.</p>
<p>As floating point numbers are approximations, some values are not quite what they would be mathematically:</p>
<pre class='hljl'>
<span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-nf'>sin</span><span class='hljl-p'>(</span><span class='hljl-n'>pi</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
(4.440892098500626e-16, 1.2246467991473532e-16)
</pre>
<p>These values are <em>very</em> small numbers, but not exactly <span class="math">$0$</span>, as they are mathematically.</p>
<hr />
<p>The only common issue is with powers. <code>Julia</code> tries to keep a predictable output from the input types (not their values). Here are the two main cases that arise where this can cause unexpected results:</p>
<ul>
<li><p>integer bases and integer exponents can <em>easily</em> overflow. Not only <code>m^n</code> is always an integer, it is always an integer with a fixed storage size computed from the sizes of <code>m</code> and <code>n</code>. So the powers can quickly get too big. This can be especially noticeable on older <span class="math">$32$</span>-bit machines, where too big is <span class="math">$2^{32} = 4,294,967,296$</span>. On <span class="math">$64$</span>-bit machines, this limit is present but much bigger.</p>
</li>
</ul>
<p>Rather than give an error though, <code>Julia</code> gives seemingly arbitrary answers, as can be seen in this example on a <span class="math">$64$</span>-bit machine:</p>
<pre class='hljl'>
<span class='hljl-ni'>2</span><span class='hljl-oB'>^</span><span class='hljl-ni'>62</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>^</span><span class='hljl-ni'>63</span>
</pre>
<pre class="output">
(4611686018427387904, -9223372036854775808)
</pre>
<p>This could be worked around, but it isn't, as it would slow down this basic computation. So, it is up to the user to be aware of cases where their integer values can grow to big. Again, use floating point numbers in this domain, as they have more room, at the cost of often being approximate values.</p>
<ul>
<li><p>the <code>sqrt</code> function will give a domain error for negative values:</p>
</li>
</ul>
<pre class='hljl'>
<span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-oB'>-</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span>
</pre>
<pre class="julia-error">
ERROR: DomainError with -1.0:
sqrt will only return a complex result if called with a complex argument. Try sqrt(Complex(x)).
</pre>
<p>This is because for real-valued inputs <code>Julia</code> expects to return a real-valued output. Of course, this is true in mathematics until the complex numbers are introduced. Similarly in <code>Julia</code>–-to take square roots of negative numbers, start with complex numbers:</p>
<pre class='hljl'>
<span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-oB'>-</span><span class='hljl-nfB'>1.0</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-n'>im</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
0.0 + 1.0im
</pre>
<div class="alert alert-info" role="alert">
<div class="markdown"><p>At one point, <code>Julia</code> had an issue with a third type of power: integer bases and negative integer exponents. For example <code>2^(-1)</code>. This is now special cased. Historically, the desire to keep a predictable type for the output (integer) led to defining this case as a domain error.</p>
</div>
</div>
<h2>Some more details.</h2>
<p>What follows is only needed for those seeking more background.</p>
<p>Julia has <em>abstract</em> number types <code>Integer</code>, <code>Real</code>, and <code>Number</code>. All four types described above are of type <code>Number</code>, but <code>Complex</code> is not of type <code>Real</code>.</p>
<p>However, a specific value is an instance of a <em>concrete</em> type. A concrete type will also include information about how the value is stored. For example, the <em>integer</em> <code>1</code> could be stored using <span class="math">$64$</span> bits as a signed integers, or, should storage be a concern, as an <span class="math">$8$</span> bits signed or even unsigned integer, etc.. If storage isn't an issue, but exactness at all scales is, then it can be stored in a manner that allows for the storage to grow using "big" numbers.</p>
<p>These distinctions can be seen in how <code>Julia</code> parses these three values:</p>
<ul>
<li><p><code>1234567890</code> will be a <span class="math">$64$</span>-bit integer (on newer machines), <code>Int64</code></p>
</li>
<li><p><code>12345678901234567890</code> will be a <span class="math">$128$</span> bit integer, <code>Int128</code></p>
</li>
<li><p><code>1234567890123456789012345678901234567890</code> will be a big integer, <code>BigInt</code></p>
</li>
</ul>
<p>Having abstract types allows programmers to write functions that will work over a wide range of input values that are similar, but have different implementation details.</p>
<h3>Integers</h3>
<p>Integers are often used casually, as they come about from parsing. As with a calculator, floating point numbers <em>could</em> be used for integers, but in <code>Julia</code>–-and other languages–-it proves useful to have numbers known to have <em>exact</em> values. In <code>Julia</code> there are built-in number types for integers stored in <span class="math">$8$</span>, <span class="math">$16$</span>, <span class="math">$32$</span>, <span class="math">$64$</span>, and <span class="math">$128$</span> bits and <code>BigInt</code>s if the previous aren't large enough. (<span class="math">$8$</span> bits can hold <span class="math">$8$</span> binary values representing <span class="math">$1$</span> of <span class="math">$256=2^8$</span> possibilities, whereas the larger <span class="math">$128$</span> bit can hold one of <span class="math">$2^{128}$</span> possibilities.) Smaller values can be more efficiently used, and this is leveraged at the system level, but not a necessary distinction with calculus where the default size along with an occasional usage of <code>BigInt</code> suffice.</p>
<h3>Floating point numbers</h3>
<p><a href="http://en.wikipedia.org/wiki/Floating_point">Floating point</a> numbers are a computational model for the real numbers. For floating point numbers, <span class="math">$64$</span> bits are used by default for both <span class="math">$32$</span>- and <span class="math">$64$</span>-bit systems, though other storage sizes can be requested. This gives a large–-but still finite–-set of real numbers that can be represented. However, there are infinitely many real numbers just between <span class="math">$0$</span> and <span class="math">$1$</span>, so there is no chance that all can be represented exactly on the computer with a floating point value. Floating point then is <em>necessarily</em> an approximation for all but a subset of the real numbers. Floating point values can be viewed in normalized <a href="http://en.wikipedia.org/wiki/Scientific_notation">scientific notation</a> as <span class="math">$a\cdot 2^b$</span> where <span class="math">$a$</span> is the <em>significand</em> and <span class="math">$b$</span> is the <em>exponent</em>. Save for special values, the significand <span class="math">$a$</span> is normalized to satisfy <span class="math">$1 \leq \lvert a\rvert < 2$</span>, the exponent can be taken to be an integer, possibly negative.</p>
<p>As per IEEE Standard 754, the <code>Float64</code> type gives 52 bits to the precision (with an additional implied one), 11 bits to the exponent and the other bit is used to represent the sign. Positive, finite, floating point numbers have a range approximately between <span class="math">$10^{-308}$</span> and <span class="math">$10^{308}$</span>, as 308 is about <span class="math">$\log_{10}\cdot 2^{1023}$</span>. The numbers are not evenly spread out over this range, but, rather, are much more concentrated closer to <span class="math">$0$</span>.</p>
<div class="alert alert-success" role="alert">
<div class="markdown"><p>You can discover more about the range of floating point values provided by calling a few different functions.</p>
<ul>
<li><p><code>typemax(0.0)</code> gives the largest value for the type (<code>Inf</code> in this case).</p>
</li>
<li><p><code>prevfloat(Inf)</code> gives the largest finite one, in general <code>prevfloat</code> is the next smallest floating point value.</p>
</li>
<li><p><code>nextfloat(-Inf)</code>, similarly, gives the smallest finite floating point value, and in general returns the next largest floating point value.</p>
</li>
<li><p><code>nextfloat(0.0)</code> gives the closest positive value to 0.</p>
</li>
<li><p><code>eps()</code> gives the distance to the next floating point number bigger than <code>1.0</code>. This is sometimes referred to as machine precision.</p>
</li>
</ul>
</div>
</div>
<h4>Scientific notation</h4>
<p>Floating point numbers may print in a familiar manner:</p>
<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>1.23</span>
</pre>
<pre class="output">
1.23
</pre>
<p>or may be represented in scientific notation:</p>
<pre class='hljl'>
<span class='hljl-nfB'>6.23</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-nfB'>10.0</span><span class='hljl-oB'>^</span><span class='hljl-ni'>23</span>
</pre>
<pre class="output">
6.23e23
</pre>
<p>The special coding <code>aeb</code> (or if the exponent is negative <code>ae-b</code>) is used to represent the number <span class="math">$a \cdot 10^b$</span> (<span class="math">$1 \leq a < 10$</span>). This notation can be used directly to specify a floating point value:</p>
<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>6.23e23</span>
</pre>
<pre class="output">
6.23e23
</pre>
<p>The first way of representing this number required using <code>10.0</code> and not <code>10</code> as the integer power will return an integer and even for 64-bit systems is only valid up to <code>10^18</code>. Using scientific notation avoids having to concentrate on such limitations.</p>
<h5>Example</h5>
<p>Floating point values in scientific notation will always be normalized. This is easy for the computer to do, but tedious to do by hand. Here we see:</p>
<pre class='hljl'>
<span class='hljl-nfB'>4e30</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-nfB'>3e40</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-nfB'>3e40</span><span class='hljl-t'> </span><span class='hljl-oB'>/</span><span class='hljl-t'> </span><span class='hljl-nfB'>4e30</span>
</pre>
<pre class="output">
(1.2000000000000001e71, 7.5e9)
</pre>
<p>The power in the first is 71, not 70 = 30+40, as the product of 3 and 4 as 12 or <code>1.2e^1</code>. (We also see the artifact of <code>1.2</code> not being exactly representable in floating point.)</p>
<h4>Special values: Inf, -Inf, NaN</h4>
<p>The coding of floating point numbers also allows for the special values of <code>Inf</code>, <code>-Inf</code> to represent positive and negative infinity. As well, a special value <code>NaN</code> ("not a number") is used to represent a value that arises when an operation is not closed (e.g., <span class="math">$0.0/0.0$</span> yields <code>NaN</code>). Except for negative bases, the floating point numbers with the addition of <code>Inf</code> and <code>NaN</code> are closed under the operations <code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, and <code>^</code>. Here are some computations that produce <code>NaN</code>:</p>
<pre class='hljl'>
<span class='hljl-ni'>0</span><span class='hljl-oB'>/</span><span class='hljl-ni'>0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span><span class='hljl-oB'>/</span><span class='hljl-n'>Inf</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span>
</pre>
<pre class="output">
(NaN, NaN, NaN, NaN)
</pre>
<p>Whereas, these produce an infinity</p>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-oB'>/</span><span class='hljl-ni'>0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span>
</pre>
<pre class="output">
(Inf, Inf, Inf)
</pre>
<p>Finally, these are mathematically undefined, but still yield a finite value with <code>Julia</code>:</p>
<pre class='hljl'>
<span class='hljl-ni'>0</span><span class='hljl-oB'>^</span><span class='hljl-ni'>0</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>Inf</span><span class='hljl-oB'>^</span><span class='hljl-ni'>0</span>
</pre>
<pre class="output">
(1, 1.0)
</pre>
<h4>Floating point numbers and real numbers</h4>
<p>Floating point numbers are an abstraction for the real numbers. For the most part this abstraction works in the background, though there are cases where one needs to have it in mind. Here are a few:</p>
<ul>
<li><p>For real and rational numbers, between any two numbers <span class="math">$a < b$</span>, there is another real number in between. This is not so for floating point numbers which have a finite precision. (Julia has some functions for working with this distinction.)</p>
</li>
<li><p>Floating point numbers are approximations for most values, even simple rational ones like <span class="math">$1/3$</span>. This leads to oddities such as this value not being <span class="math">$0$</span>:</p>
</li>
</ul>
<pre class='hljl'>
<span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-oB'>*</span><span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span>
</pre>
<pre class="output">
4.440892098500626e-16
</pre>
<p>It is no surprise that an irrational number, like <span class="math">$\sqrt{2}$</span>, can't be represented <strong>exactly</strong> within floating point, but it is perhaps surprising that simple numbers can not be, so <span class="math">$1/3$</span>, <span class="math">$1/5$</span>, <span class="math">$\dots$</span> are approximated. Here is a surprising-at-first consequence:</p>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>==</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span>
</pre>
<pre class="output">
false
</pre>
<p>That is adding <code>1/10</code> and <code>2/10</code> is not exactly <code>3/10</code>, as expected mathematically. Such differences are usually very small and are generally attributed to rounding error. The user needs to be mindful when testing for equality, as is done above with the <code>==</code> operator.</p>
<ul>
<li><p>Floating point addition is not necessarily associative, that is the property <span class="math">$a + (b+c) = (a+b) + c$</span> may not hold exactly. For example:</p>
</li>
</ul>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>==</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-oB'>/</span><span class='hljl-ni'>10</span>
</pre>
<pre class="output">
false
</pre>
<ul>
<li><p>For real numbers subtraction of similar-sized numbers is not exceptional, for example <span class="math">$1 - \cos(x)$</span> is positive if <span class="math">$0 < x < \pi/2$</span>, say. This will not be the case for floating point values. If <span class="math">$x$</span> is close enough to <span class="math">$0$</span>, then <span class="math">$\cos(x)$</span> and <span class="math">$1$</span> will be so close, that they will be represented by the same floating point value, <code>1.0</code>, so the difference will be zero:</p>
</li>
</ul>
<pre class='hljl'>
<span class='hljl-nfB'>1.0</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-nf'>cos</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1e-8</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
0.0
</pre>
<h3>Rational numbers</h3>
<p>Rational numbers can be used when the exactness of the number is more important than the speed or wider range of values offered by floating point numbers. In <code>Julia</code> a rational number is comprised of a numerator and a denominator, each an integer of the same type, and reduced to lowest terms. The operations of addition, subtraction, multiplication, and division will keep their answers as rational numbers. As well, raising a rational number to a positive, integer value will produce a rational number.</p>
<p>As mentioned, these are constructed using double slashes:</p>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>//</span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>6</span><span class='hljl-oB'>//</span><span class='hljl-ni'>4</span>
</pre>
<pre class="output">
(1//2, 2//1, 3//2)
</pre>
<p>Rational numbers are exact, so the following are identical to their mathematical counterparts:</p>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>==</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span>
</pre>
<pre class="output">
true
</pre>
<p>and associativity:</p>
<pre class='hljl'>
<span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>==</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-oB'>//</span><span class='hljl-ni'>10</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
true
</pre>
<p>Here we see that the type is preserved under the basic operations:</p>
<pre class='hljl'>
<span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>3</span><span class='hljl-t'> </span><span class='hljl-oB'>*</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>4</span><span class='hljl-t'> </span><span class='hljl-oB'>/</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>^</span><span class='hljl-t'> </span><span class='hljl-ni'>6</span>
</pre>
<pre class="output">
1771561//2985984
</pre>
<p>For powers, a non-integer exponent is converted to floating point, so this operation is defined, though will always return a floating point value:</p>
<pre class='hljl'>
<span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-oB'>^</span><span class='hljl-p'>(</span><span class='hljl-ni'>1</span><span class='hljl-oB'>//</span><span class='hljl-ni'>2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-cs'># the first parentheses are necessary as `^` will be evaluated before `//`.</span>
</pre>
<pre class="output">
0.7071067811865476
</pre>
<h5>Example: different types of real numbers</h5>
<p>This table shows what attributes are implemented for the different types.</p>
<div class="table-responsive">
<table class="table table-hover">
<tr><th>Attributes</th><th>Integer</th><th>Rational</th><th>FloatingPoint</th></tr>
<tr><td><div class="markdown"><p>construction</p>
</div></td><td><div class="markdown"><p><code>1</code></p>
</div></td><td><div class="markdown"><p><code>1//1</code></p>
</div></td><td><div class="markdown"><p><code>1.0</code></p>
</div></td></tr>
<tr><td><div class="markdown"><p>exact</p>
</div></td><td><div class="markdown"><p>true</p>
</div></td><td><div class="markdown"><p>true</p>
</div></td><td><div class="markdown"><p>not usually</p>
</div></td></tr>
<tr><td><div class="markdown"><p>wide range</p>
</div></td><td><div class="markdown"><p>false</p>
</div></td><td><div class="markdown"><p>false</p>
</div></td><td><div class="markdown"><p>true</p>
</div></td></tr>
<tr><td><div class="markdown"><p>has infinity</p>
</div></td><td><div class="markdown"><p>false</p>
</div></td><td><div class="markdown"><p>false</p>
</div></td><td><div class="markdown"><p>true</p>
</div></td></tr>
<tr><td><div class="markdown"><p>has <code>-0</code></p>
</div></td><td><div class="markdown"><p>false</p>
</div></td><td><div class="markdown"><p>false</p>
</div></td><td><div class="markdown"><p>true</p>
</div></td></tr>
<tr><td><div class="markdown"><p>fast</p>
</div></td><td><div class="markdown"><p>true</p>
</div></td><td><div class="markdown"><p>false</p>
</div></td><td><div class="markdown"><p>true</p>
</div></td></tr>
<tr><td><div class="markdown"><p>closed under</p>
</div></td><td><div class="markdown"><p><code>+</code>, <code>-</code>, <code>*</code>, <code>^</code> (non-negative exponent)</p>
</div></td><td><div class="markdown"><p><code>+</code>, <code>-</code>, <code>*</code>, <code>/</code> (non zero denominator),<code>^</code> (integer power)</p>
</div></td><td><div class="markdown"><p><code>+</code>, <code>-</code>, <code>*</code>, <code>/</code> (possibly <code>NaN</code>, <code>Inf</code>),<code>^</code> (non-negative base)</p>
</div></td></tr>
</table>
</div>
<h2>Complex numbers</h2>
<p>Complex numbers in <code>Julia</code> are stored as two numbers, a real and imaginary part, each some type of <code>Real</code> number. The special constant <code>im</code> is used to represent <span class="math">$i=\sqrt{-1}$</span>. This makes the construction of complex numbers fairly standard:</p>
<pre class='hljl'>
<span class='hljl-ni'>1</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-n'>im</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-ni'>3</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nfB'>4.0</span><span class='hljl-n'>im</span>
</pre>
<pre class="output">
(1 + 2im, 3.0 + 4.0im)
</pre>
<p>(These two aren't exactly the same, the <code>3</code> is promoted from an integer to a float to match the <code>4.0</code>. Each of the components must be of the same type of number.)</p>
<p>Mathematically, complex numbers are needed so that certain equations can be satisfied. For example <span class="math">$x^2 = -2$</span> has solutions <span class="math">$-\sqrt{2}i$</span> and <span class="math">$\sqrt{2}i$</span> over the complex numbers. Finding this in <code>Julia</code> requires some attention, as we have both <code>sqrt(-2)</code> and <code>sqrt(-2.0)</code> throwing a <code>DomainError</code>, as the <code>sqrt</code> function expects non-negative real arguments. However first creating a complex number does work:</p>
<pre class='hljl'>
<span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-oB'>-</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-n'>im</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
0.0 + 1.4142135623730951im
</pre>
<p>For complex arguments, the <code>sqrt</code> function will return complex values (even if the answer is a real number).</p>
<p>This means, if you wanted to perform the quadratic equation for any real inputs, your computations might involve something like the following:</p>
<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-p'>,</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>3</span><span class='hljl-t'> </span><span class='hljl-cs'>## x^2 + 2x + 3</span><span class='hljl-t'>
</span><span class='hljl-n'>discr</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>b</span><span class='hljl-oB'>^</span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-n'>a</span><span class='hljl-oB'>*</span><span class='hljl-n'>c</span><span class='hljl-t'>
</span><span class='hljl-p'>(</span><span class='hljl-oB'>-</span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-n'>discr</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-n'>im</span><span class='hljl-p'>))</span><span class='hljl-oB'>/</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-n'>a</span><span class='hljl-p'>),</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-oB'>-</span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-nf'>sqrt</span><span class='hljl-p'>(</span><span class='hljl-n'>discr</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>0</span><span class='hljl-n'>im</span><span class='hljl-p'>))</span><span class='hljl-oB'>/</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span>
</pre>
<pre class="output">
(-1.0 + 1.4142135623730951im, -1.0 - 1.4142135623730951im)
</pre>
<p>When learning calculus, the only common usage of complex numbers arises when solving polynomial equations for roots, or zeros, though they are very important for subsequent work using the concepts of calculus.</p>
<h2>Type stability</h2>
<p>One design priority of <code>Julia</code> is that it should be fast. How can <code>Julia</code> do this? In a simple model, <code>Julia</code> is an interface between the user and the computer's processor(s). Processors consume a set of instructions, the user issues a set of commands. <code>Julia</code> is in charge of the translation between the two. Ultimately <code>Julia</code> calls a compiler to create the instructions. A basic premise is the shorter the instructions, the faster they are to process. Shorter instructions can come about by being more explicit about what types of values the instructions concern. Explicitness means, there is no need to reason about what a value can be. When <code>Julia</code> can reason about the type of value involved without having to reason about the values themselves, it can work with the compiler to produce shorter lists of instructions.</p>
<p>So knowing the type of the output of a function can be a big advantage. In <code>Julia</code> this is known as <em>type stability</em>. In the standard <code>Julia</code> library, this is a primary design consideration.</p>
<h5>Example: closure</h5>
<p>To motivate this a bit, we discuss how mathematics can be shaped by a desire to stick to simple ideas. A desirable algebraic property of a set of numbers and an operation–-<em>closure</em>. That is, if one takes an operation like <code>+</code> and then uses it to add two numbers in a set, will that result also be in the set? If this is so for any pair of numbers, then the set is closed with respect to the operation addition.</p>
<p>Lets suppose we start with the <em>natural numbers</em>: <span class="math">$1,2, \dots$</span>. Natural, in that we can easily represent small values in terms of fingers. This set is closed under addition–-as a child learns when counting using their fingers. However, if we started with the odd natural numbers, this set would <em>not</em> be closed under addition–-<span class="math">$3+3=6$</span>.</p>
<p>The natural numbers are not all the numbers we need, as once a desire for subtraction is included, we find the set isn't closed. There isn't a <span class="math">$0$</span>, needed as <span class="math">$n-n=0$</span> and there aren't negative numbers. The set of integers are needed for closure under addition and subtraction.</p>
<p>The integers are also closed under multiplication, which for integer values can be seen as just regrouping into longer additions.</p>
<p>However, the integers are not closed under division–-even if you put aside the pesky issue of dividing by <span class="math">$0$</span>. For that, the rational numbers must be introduced. So aside from division by <span class="math">$0$</span>, the rationals are closed under addition, subtraction, multiplication, and division. There is one more fundamental operation though, powers.</p>
<p>Powers are defined for positive integers in a simple enough manner</p>
<p class="math">\[
~
a^n=a\cdot a \cdot a \cdots a \text{ (n times); } a, n \text{ are integers, $n$ is positive}.
~
\]</p>
<p>We can define <span class="math">$a^0$</span> to be <span class="math">$1$</span>, except for the special case of <span class="math">$0^0$</span>, which is left undefined mathematically (though it is also defined as <code>1</code> within <code>Julia</code>). We can extend the above to include negative values of <span class="math">$a$</span>, but what about negative values of <span class="math">$n$</span>? We can't say the integers are closed under powers, as the definition consistent with the rules that <span class="math">$a^{(-n)} = 1/a^n$</span> requires rational numbers to be defined.</p>
<p>Well, in the above <code>a</code> could be a rational number, is <code>a^n</code> closed for rational numbers? No again. Though it is fine for <span class="math">$n$</span> as an integer (save the odd case of <span class="math">$0$</span>, simple definitions like <span class="math">$2^{1/2}$</span> are not answered within the rationals. For this, we need to introduce the <em>real</em> numbers. It is mentioned that <a href="http://tinyurl.com/bpqbkap">Aristotle</a> hinted at the irrationality of the square root of <span class="math">$2$</span>. To define terms like <span class="math">$a^{1/n}$</span> for integer values <span class="math">$a,n > 0$</span> a reference to a solution to an equation <span class="math">$x^n-a$</span> is used. Such solutions require the irrational numbers to have solutions in general. Hence the need for the real numbers (well, algebraic numbers at least, though once the exponent is no longer a rational number, the full set of real numbers are needed.)</p>
<p>So, save the pesky cases, the real numbers will be closed under addition, subtraction, multiplication, division, and powers–-provided the base is non-negative.</p>
<p>Finally for that last case, the complex numbers are introduced to give an answer to <span class="math">$\sqrt{-1}$</span>.</p>
<hr />
<p>How does this apply with <code>Julia</code>?</p>
<p>The point is, if we restrict our set of inputs, we can get more precise values for the output of basic operations, but to get more general inputs we need to have bigger output sets.</p>
<p>A similar thing happens in <code>Julia</code>. For addition say, the addition of two integers of the same type will be an integer of that type. This speed consideration is not solely for type stability, but also to avoid checking for overflow.</p>
<p>Another example, the division of two integers will always be a number of the same type–-floating point, as that is the only type that ensures the answer will always fit within. (The explicit use of rationals notwithstanding.) So even if two integers are the input and their answer <em>could</em> be an integer, in <code>Julia</code> it will be a floating point number, (cf. <code>2/1</code>).</p>
<p>Hopefully this helps explain the subtle issues around powers: in <code>Julia</code> an integer raised to an integer should be an integer, for speed, though certain cases are special cased, like <code>2^(-1)</code>. However since a real number raised to a real number makes sense always when the base is non-negative, as long as real numbers are used as outputs, the expressions <code>2.0^(-1)</code> and <code>2^(-1.0)</code> are computed and real numbers (floating points) are returned. For type stability, even though <span class="math">$2.0^1$</span> could be an integer, a floating point answer is returned.</p>
<p>As for negative bases, <code>Julia</code> could always return complex numbers, but in addition to this being slower, it would be irksome to users. So user's must opt in. Hence <code>sqrt(-1.0)</code> will be an error, but the more explicit–-but mathematically equivalent–-<code>sqrt(-1.0 + 0im)</code> will not be a domain error, but rather a complex value will be returned.</p>
<h2>Questions</h2>
<h6>Question</h6>
<p>The number created by <code>pi/2</code> is?</p>
<form name="WeaveQuestion" data-id="CFCjZ5zb" data-controltype="radio">
<div class="form-group ">
<div class="radio">
<label>
<input type="radio" name="radio_CFCjZ5zb" value="1"><div class="markdown"><p>Integer</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_CFCjZ5zb" value="2"><div class="markdown"><p>Rational</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_CFCjZ5zb" value="3"><div class="markdown"><p>Floating point</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_CFCjZ5zb" value="4"><div class="markdown"><p>Complex</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_CFCjZ5zb" value="5"><div class="markdown"><p>None, an error occurs</p>
</div>
</label>
</div>
<div id="CFCjZ5zb_message"></div>
</div>
</form>
<script text="text/javascript">
$("input:radio[name='radio_CFCjZ5zb']").on("change", function() {
correct = this.value == 3;
if(correct) {
$("#CFCjZ5zb_message").html("<div class='alert alert-success'><span class='glyphicon glyphicon-thumbs-up'> Correct</span></div>");
} else {
$("#CFCjZ5zb_message").html("<div class='alert alert-warning'><span class='glyphicon glyphicon-thumbs-down'> Incorrect</span></div>");
}
});
</script>
<h6>Question</h6>
<p>The number created by <code>2/2</code> is?</p>
<form name="WeaveQuestion" data-id="xh79DpQj" data-controltype="radio">
<div class="form-group ">
<div class="radio">
<label>
<input type="radio" name="radio_xh79DpQj" value="1"><div class="markdown"><p>Integer</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_xh79DpQj" value="2"><div class="markdown"><p>Rational</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_xh79DpQj" value="3"><div class="markdown"><p>Floating point</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_xh79DpQj" value="4"><div class="markdown"><p>Complex</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_xh79DpQj" value="5"><div class="markdown"><p>None, an error occurs</p>
</div>
</label>
</div>
<div id="xh79DpQj_message"></div>
</div>
</form>
<script text="text/javascript">
$("input:radio[name='radio_xh79DpQj']").on("change", function() {
correct = this.value == 3;
if(correct) {
$("#xh79DpQj_message").html("<div class='alert alert-success'><span class='glyphicon glyphicon-thumbs-up'> Correct</span></div>");
} else {
$("#xh79DpQj_message").html("<div class='alert alert-warning'><span class='glyphicon glyphicon-thumbs-down'> Incorrect</span></div>");
}
});
</script>
<h6>Question</h6>
<p>The number created by <code>2//2</code> is?</p>
<form name="WeaveQuestion" data-id="1Rxl4pdA" data-controltype="radio">
<div class="form-group ">
<div class="radio">
<label>
<input type="radio" name="radio_1Rxl4pdA" value="1"><div class="markdown"><p>Integer</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_1Rxl4pdA" value="2"><div class="markdown"><p>Rational</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_1Rxl4pdA" value="3"><div class="markdown"><p>Floating point</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_1Rxl4pdA" value="4"><div class="markdown"><p>Complex</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_1Rxl4pdA" value="5"><div class="markdown"><p>None, an error occurs</p>
</div>
</label>
</div>
<div id="1Rxl4pdA_message"></div>
</div>
</form>
<script text="text/javascript">
$("input:radio[name='radio_1Rxl4pdA']").on("change", function() {
correct = this.value == 2;
if(correct) {
$("#1Rxl4pdA_message").html("<div class='alert alert-success'><span class='glyphicon glyphicon-thumbs-up'> Correct</span></div>");
} else {
$("#1Rxl4pdA_message").html("<div class='alert alert-warning'><span class='glyphicon glyphicon-thumbs-down'> Incorrect</span></div>");
}
});
</script>
<h6>Question</h6>
<p>The number created by <code>1 + 1//2 + 1/3</code> is?</p>
<form name="WeaveQuestion" data-id="WrSODspj" data-controltype="radio">
<div class="form-group ">
<div class="radio">
<label>
<input type="radio" name="radio_WrSODspj" value="1"><div class="markdown"><p>Integer</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_WrSODspj" value="2"><div class="markdown"><p>Rational</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_WrSODspj" value="3"><div class="markdown"><p>Floating point</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_WrSODspj" value="4"><div class="markdown"><p>Complex</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_WrSODspj" value="5"><div class="markdown"><p>None, an error occurs</p>
</div>
</label>
</div>
<div id="WrSODspj_message"></div>
</div>
</form>
<script text="text/javascript">
$("input:radio[name='radio_WrSODspj']").on("change", function() {
correct = this.value == 3;
if(correct) {
$("#WrSODspj_message").html("<div class='alert alert-success'><span class='glyphicon glyphicon-thumbs-up'> Correct</span></div>");
} else {
$("#WrSODspj_message").html("<div class='alert alert-warning'><span class='glyphicon glyphicon-thumbs-down'> Incorrect</span></div>");
}
});
</script>
<h6>Question</h6>
<p>The number created by <code>2^3</code> is?</p>
<form name="WeaveQuestion" data-id="FxoMrQlf" data-controltype="radio">
<div class="form-group ">
<div class="radio">
<label>
<input type="radio" name="radio_FxoMrQlf" value="1"><div class="markdown"><p>Integer</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_FxoMrQlf" value="2"><div class="markdown"><p>Rational</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_FxoMrQlf" value="3"><div class="markdown"><p>Floating point</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_FxoMrQlf" value="4"><div class="markdown"><p>Complex</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_FxoMrQlf" value="5"><div class="markdown"><p>None, an error occurs</p>
</div>
</label>
</div>
<div id="FxoMrQlf_message"></div>
</div>
</form>
<script text="text/javascript">
$("input:radio[name='radio_FxoMrQlf']").on("change", function() {
correct = this.value == 1;
if(correct) {
$("#FxoMrQlf_message").html("<div class='alert alert-success'><span class='glyphicon glyphicon-thumbs-up'> Correct</span></div>");
} else {
$("#FxoMrQlf_message").html("<div class='alert alert-warning'><span class='glyphicon glyphicon-thumbs-down'> Incorrect</span></div>");
}
});
</script>
<h6>Question</h6>
<p>The number created by <code>sqrt(im)</code> is?</p>
<form name="WeaveQuestion" data-id="mQhlrpUN" data-controltype="radio">
<div class="form-group ">
<div class="radio">
<label>
<input type="radio" name="radio_mQhlrpUN" value="1"><div class="markdown"><p>Integer</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_mQhlrpUN" value="2"><div class="markdown"><p>Rational</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_mQhlrpUN" value="3"><div class="markdown"><p>Floating point</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_mQhlrpUN" value="4"><div class="markdown"><p>Complex</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_mQhlrpUN" value="5"><div class="markdown"><p>None, an error occurs</p>
</div>
</label>
</div>
<div id="mQhlrpUN_message"></div>
</div>
</form>
<script text="text/javascript">
$("input:radio[name='radio_mQhlrpUN']").on("change", function() {
correct = this.value == 4;
if(correct) {
$("#mQhlrpUN_message").html("<div class='alert alert-success'><span class='glyphicon glyphicon-thumbs-up'> Correct</span></div>");
} else {
$("#mQhlrpUN_message").html("<div class='alert alert-warning'><span class='glyphicon glyphicon-thumbs-down'> Incorrect</span></div>");
}
});
</script>
<h6>Question</h6>
<p>The number created by <code>2^(-1)</code> is?</p>
<form name="WeaveQuestion" data-id="GoWPEq2k" data-controltype="radio">
<div class="form-group ">
<div class="radio">
<label>
<input type="radio" name="radio_GoWPEq2k" value="1"><div class="markdown"><p>Integer</p>
</div>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="radio_GoWPEq2k" value="2"><div class="markdown"><p>Rational</p>
</div>