Skip to content

Commit 0c191ee

Browse files
tkelmanararslan
authored andcommitted
Merge pull request #20183 from fredrikekre/fe/manual-doctests
Doctests for the manual, part 1
1 parent d236462 commit 0c191ee

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

docs/src/index.md

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ system.
3333

3434
Here are some simple examples using arithmetic operators:
3535

36-
```julia
36+
```jldoctest
3737
julia> 1 + 2 + 3
3838
6
3939
@@ -65,7 +65,7 @@ are supported on all primitive integer types:
6565

6666
Here are some examples with bitwise operators:
6767

68-
```julia
68+
```jldoctest
6969
julia> ~123
7070
-124
7171
@@ -95,7 +95,7 @@ of the operation back into its left operand. The updating version of the binary
9595
by placing a `=` immediately after the operator. For example, writing `x += 3` is equivalent to
9696
writing `x = x + 3`:
9797

98-
```julia
98+
```jldoctest
9999
julia> x = 1
100100
1
101101
@@ -116,36 +116,44 @@ The updating versions of all the binary arithmetic and bitwise operators are:
116116
An updating operator rebinds the variable on the left-hand side. As a result, the type of the
117117
variable may change.
118118

119-
```julia
119+
```jldoctest
120120
julia> x = 0x01; typeof(x)
121121
UInt8
122122

123-
julia> x *= 2 #Same as x = x * 2
123+
julia> x *= 2 # Same as x = x * 2
124124
2
125125

126-
julia> isa(x, Int)
127-
true
126+
julia> typeof(x)
127+
Int64
128128
```
129129

130130
## [Vectorized "dot" operators](@id man-dot-operators)
131131

132132
For *every* binary operation like `^`, there is a corresponding
133133
"dot" operation `.^` that is *automatically* defined
134-
to perform `^` element-by-element on arrays. For example,
134+
to perform `^` element-by-element on arrays. For example,
135135
`[1,2,3] ^ 3` is not defined, since there is no standard
136136
mathematical meaning to "cubing" an array, but `[1,2,3] .^ 3`
137137
is defined as computing the elementwise
138138
(or "vectorized") result `[1^3, 2^3, 3^3]`.
139139

140+
```jldoctest
141+
julia> [1,2,3] .^ 3
142+
3-element Array{Int64,1}:
143+
1
144+
8
145+
27
146+
```
147+
140148
More specifically, `a .^ b` is parsed as the ["dot" call](@ref man-vectorized)
141149
`(^).(a,b)`, which performs a [broadcast](@ref Broadcasting) operation:
142150
it can combine arrays and scalars, arrays of the same size (performing
143151
the operation elementwise), and even arrays of different shapes (e.g.
144-
combining row and column vectors to produce a matrix). Moreover, like
152+
combining row and column vectors to produce a matrix). Moreover, like
145153
all vectorized "dot calls," these "dot operators" are
146-
*fusing*. For example, if you compute `2 .* A.^2 .+ sin.(A)` for an
154+
*fusing*. For example, if you compute `2 .* A.^2 .+ sin.(A)` for an
147155
array `A`, it performs a *single* loop over `A`, computing `2a^2 + sin(a)`
148-
for each element of `A`. In particular, nested dot calls like `f.(g.(x))`
156+
for each element of `A`. In particular, nested dot calls like `f.(g.(x))`
149157
are fused, and "adjacent" binary operators like `x .+ 3 .* x.^2` are
150158
equivalent to nested dot calls `(+).(x, (*).(3, (^).(x, 2)))`.
151159

@@ -173,7 +181,7 @@ Standard comparison operations are defined for all the primitive numeric types:
173181

174182
Here are some simple examples:
175183

176-
```julia
184+
```jldoctest
177185
julia> 1 == 1
178186
true
179187
@@ -219,7 +227,7 @@ are compared according to the [IEEE 754 standard](https://en.wikipedia.org/wiki/
219227

220228
The last point is potentially surprising and thus worth noting:
221229

222-
```julia
230+
```jldoctest
223231
julia> NaN == NaN
224232
false
225233
@@ -235,7 +243,7 @@ false
235243

236244
and can cause especial headaches with [Arrays](@ref):
237245

238-
```julia
246+
```jldoctest
239247
julia> [1 NaN] == [1 NaN]
240248
false
241249
```
@@ -252,20 +260,20 @@ situations like hash key comparisons:
252260

253261
[`isequal()`](@ref) considers `NaN`s equal to each other:
254262

255-
```julia
256-
julia> isequal(NaN,NaN)
263+
```jldoctest
264+
julia> isequal(NaN, NaN)
257265
true
258266
259267
julia> isequal([1 NaN], [1 NaN])
260268
true
261269
262-
julia> isequal(NaN,NaN32)
270+
julia> isequal(NaN, NaN32)
263271
true
264272
```
265273

266-
[`isequal()`](@ref) can also be used to distinguish signed zeros:
274+
`isequal()` can also be used to distinguish signed zeros:
267275

268-
```julia
276+
```jldoctest
269277
julia> -0.0 == 0.0
270278
true
271279
@@ -276,7 +284,7 @@ false
276284
Mixed-type comparisons between signed integers, unsigned integers, and floats can be tricky. A
277285
great deal of care has been taken to ensure that Julia does them correctly.
278286

279-
For other types, [`isequal()`](@ref) defaults to calling [`==()`](@ref), so if you want to define
287+
For other types, `isequal()` defaults to calling [`==()`](@ref), so if you want to define
280288
equality for your own types then you only need to add a [`==()`](@ref) method. If you define
281289
your own equality function, you should probably define a corresponding [`hash()`](@ref) method
282290
to ensure that `isequal(x,y)` implies `hash(x) == hash(y)`.
@@ -286,7 +294,7 @@ to ensure that `isequal(x,y)` implies `hash(x) == hash(y)`.
286294
Unlike most languages, with the [notable exception of Python](https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Comparison_operators),
287295
comparisons can be arbitrarily chained:
288296

289-
```julia
297+
```jldoctest
290298
julia> 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
291299
true
292300
```
@@ -298,7 +306,7 @@ are true where the corresponding elements of `A` are between 0 and 1.
298306

299307
Note the evaluation behavior of chained comparisons:
300308

301-
```julia
309+
```jldoctest
302310
julia> v(x) = (println(x); x)
303311
v (generic function with 1 method)
304312
@@ -366,29 +374,29 @@ conversions.
366374

367375
The following examples show the different forms.
368376

369-
```julia
377+
```jldoctest
370378
julia> Int8(127)
371379
127
372380
373381
julia> Int8(128)
374382
ERROR: InexactError()
375-
in Int8(::Int64) at ./sysimg.jl:66
376-
...
383+
Stacktrace:
384+
[1] Int8(::Int64) at ./sysimg.jl:24
377385
378386
julia> Int8(127.0)
379387
127
380388
381389
julia> Int8(3.14)
382390
ERROR: InexactError()
383-
in convert(::Type{Int8}, ::Float64) at ./float.jl:635
384-
in Int8(::Float64) at ./sysimg.jl:66
385-
...
391+
Stacktrace:
392+
[1] convert(::Type{Int8}, ::Float64) at ./float.jl:654
393+
[2] Int8(::Float64) at ./sysimg.jl:24
386394
387395
julia> Int8(128.0)
388396
ERROR: InexactError()
389-
in convert(::Type{Int8}, ::Float64) at ./float.jl:635
390-
in Int8(::Float64) at ./sysimg.jl:66
391-
...
397+
Stacktrace:
398+
[1] convert(::Type{Int8}, ::Float64) at ./float.jl:654
399+
[2] Int8(::Float64) at ./sysimg.jl:24
392400
393401
julia> 127 % Int8
394402
127
@@ -401,9 +409,9 @@ julia> round(Int8,127.4)
401409
402410
julia> round(Int8,127.6)
403411
ERROR: InexactError()
404-
in trunc(::Type{Int8}, ::Float64) at ./float.jl:628
405-
in round(::Type{Int8}, ::Float64) at ./float.jl:332
406-
...
412+
Stacktrace:
413+
[1] trunc(::Type{Int8}, ::Float64) at ./float.jl:647
414+
[2] round(::Type{Int8}, ::Float64) at ./float.jl:333
407415
```
408416

409417
See [Conversion and Promotion](@ref conversion-and-promotion) for how to define your own conversions and promotions.

0 commit comments

Comments
 (0)