@@ -33,7 +33,7 @@ system.
33
33
34
34
Here are some simple examples using arithmetic operators:
35
35
36
- ``` julia
36
+ ``` jldoctest
37
37
julia> 1 + 2 + 3
38
38
6
39
39
@@ -65,7 +65,7 @@ are supported on all primitive integer types:
65
65
66
66
Here are some examples with bitwise operators:
67
67
68
- ``` julia
68
+ ``` jldoctest
69
69
julia> ~123
70
70
-124
71
71
@@ -95,7 +95,7 @@ of the operation back into its left operand. The updating version of the binary
95
95
by placing a ` = ` immediately after the operator. For example, writing ` x += 3 ` is equivalent to
96
96
writing ` x = x + 3 ` :
97
97
98
- ``` julia
98
+ ``` jldoctest
99
99
julia> x = 1
100
100
1
101
101
@@ -116,36 +116,44 @@ The updating versions of all the binary arithmetic and bitwise operators are:
116
116
An updating operator rebinds the variable on the left-hand side. As a result, the type of the
117
117
variable may change.
118
118
119
- ```julia
119
+ ```jldoctest
120
120
julia> x = 0x01; typeof(x)
121
121
UInt8
122
122
123
- julia> x *= 2 #Same as x = x * 2
123
+ julia> x *= 2 # Same as x = x * 2
124
124
2
125
125
126
- julia> isa(x, Int )
127
- true
126
+ julia> typeof(x )
127
+ Int64
128
128
```
129
129
130
130
## [ Vectorized "dot" operators] (@id man-dot-operators)
131
131
132
132
For * every* binary operation like ` ^ ` , there is a corresponding
133
133
"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,
135
135
` [1,2,3] ^ 3 ` is not defined, since there is no standard
136
136
mathematical meaning to "cubing" an array, but ` [1,2,3] .^ 3 `
137
137
is defined as computing the elementwise
138
138
(or "vectorized") result ` [1^3, 2^3, 3^3] ` .
139
139
140
+ ``` jldoctest
141
+ julia> [1,2,3] .^ 3
142
+ 3-element Array{Int64,1}:
143
+ 1
144
+ 8
145
+ 27
146
+ ```
147
+
140
148
More specifically, ` a .^ b ` is parsed as the [ "dot" call] (@ref man-vectorized)
141
149
` (^).(a,b) ` , which performs a [ broadcast] (@ref Broadcasting) operation:
142
150
it can combine arrays and scalars, arrays of the same size (performing
143
151
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
145
153
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
147
155
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)) `
149
157
are fused, and "adjacent" binary operators like ` x .+ 3 .* x.^2 ` are
150
158
equivalent to nested dot calls ` (+).(x, (*).(3, (^).(x, 2))) ` .
151
159
@@ -173,7 +181,7 @@ Standard comparison operations are defined for all the primitive numeric types:
173
181
174
182
Here are some simple examples:
175
183
176
- ``` julia
184
+ ``` jldoctest
177
185
julia> 1 == 1
178
186
true
179
187
@@ -219,7 +227,7 @@ are compared according to the [IEEE 754 standard](https://en.wikipedia.org/wiki/
219
227
220
228
The last point is potentially surprising and thus worth noting:
221
229
222
- ``` julia
230
+ ``` jldoctest
223
231
julia> NaN == NaN
224
232
false
225
233
@@ -235,7 +243,7 @@ false
235
243
236
244
and can cause especial headaches with [ Arrays] ( @ref ) :
237
245
238
- ``` julia
246
+ ``` jldoctest
239
247
julia> [1 NaN] == [1 NaN]
240
248
false
241
249
```
@@ -252,20 +260,20 @@ situations like hash key comparisons:
252
260
253
261
[ ` isequal() ` ] ( @ref ) considers ` NaN ` s equal to each other:
254
262
255
- ``` julia
256
- julia> isequal (NaN ,NaN )
263
+ ``` jldoctest
264
+ julia> isequal(NaN, NaN)
257
265
true
258
266
259
267
julia> isequal([1 NaN], [1 NaN])
260
268
true
261
269
262
- julia> isequal (NaN ,NaN32 )
270
+ julia> isequal(NaN, NaN32)
263
271
true
264
272
```
265
273
266
- [ ` isequal() ` ] ( @ref ) can also be used to distinguish signed zeros:
274
+ ` isequal() ` can also be used to distinguish signed zeros:
267
275
268
- ``` julia
276
+ ``` jldoctest
269
277
julia> -0.0 == 0.0
270
278
true
271
279
@@ -276,7 +284,7 @@ false
276
284
Mixed-type comparisons between signed integers, unsigned integers, and floats can be tricky. A
277
285
great deal of care has been taken to ensure that Julia does them correctly.
278
286
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
280
288
equality for your own types then you only need to add a [ ` ==() ` ] ( @ref ) method. If you define
281
289
your own equality function, you should probably define a corresponding [ ` hash() ` ] ( @ref ) method
282
290
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)`.
286
294
Unlike most languages, with the [ notable exception of Python] ( https://en.wikipedia.org/wiki/Python_syntax_and_semantics#Comparison_operators ) ,
287
295
comparisons can be arbitrarily chained:
288
296
289
- ``` julia
297
+ ``` jldoctest
290
298
julia> 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
291
299
true
292
300
```
@@ -298,7 +306,7 @@ are true where the corresponding elements of `A` are between 0 and 1.
298
306
299
307
Note the evaluation behavior of chained comparisons:
300
308
301
- ``` julia
309
+ ``` jldoctest
302
310
julia> v(x) = (println(x); x)
303
311
v (generic function with 1 method)
304
312
@@ -366,29 +374,29 @@ conversions.
366
374
367
375
The following examples show the different forms.
368
376
369
- ``` julia
377
+ ``` jldoctest
370
378
julia> Int8(127)
371
379
127
372
380
373
381
julia> Int8(128)
374
382
ERROR: InexactError()
375
- in Int8 ( :: Int64 ) at ./ sysimg . jl : 66
376
- ...
383
+ Stacktrace:
384
+ [1] Int8(::Int64) at ./sysimg.jl:24
377
385
378
386
julia> Int8(127.0)
379
387
127
380
388
381
389
julia> Int8(3.14)
382
390
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
386
394
387
395
julia> Int8(128.0)
388
396
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
392
400
393
401
julia> 127 % Int8
394
402
127
@@ -401,9 +409,9 @@ julia> round(Int8,127.4)
401
409
402
410
julia> round(Int8,127.6)
403
411
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
407
415
```
408
416
409
417
See [ Conversion and Promotion] (@ref conversion-and-promotion) for how to define your own conversions and promotions.
0 commit comments