[spam] Language learning: Julia
Karl Semich
0xloem at gmail.com
Sun Apr 24 03:17:11 PDT 2022
Chapter 2: variables
- names can contain utf-8
Chapter 3: numbers
see last post
Chapter 4: basic operations
- c-like operators with additions:
x ÷ y integer divide x / y, truncated to an integer
x \ y inverse divide equivalent to y / x
x ^ y power raises x to the yth power
x ⊻ y bitwise xor (exclusive or)
x ⊼ y bitwise nand (not and)
x ⊽ y bitwise nor (not or)
x >>> y logical shift right
x >> y arithmetic shift right
√
- binary operators are all available as in-place op= forms too. using this
may retype the lvalue.
÷ can be typed with \div<tab> in the repl, see also manual section
https://docs.julialang.org/en/v1/manual/unicode-input/#Unicode-Input or
called as div()
note: false * anything = 0, even NaN . This can be used to prevent
propagation of nans.
- boolean operators (&& ||) are short-circuiting
- xor() nand() nor() can be called by name as binary functions
Each operator has a .op form that performs elementwise operation on arrays.
julia> [1,2,3] .^ 3
3-element Vector{Int64}:
1
8
27
This is simply a use of the further . operator, which broadcasts functions
over array parameters:
julia> nand.([1,2,3],[4,3,2])
3-element Vector{Int64}:
-1
-3
-3
Multiple dot operators in a single expression, even in-place assignment,
are all fused into a single underlying loop over the combined datasize.
Comparisons can be chained:
julia> 1 < 2 <= 2 < 3 == 3 > 2 >= 1 == 1 < 3 != 5
true
Chaining works with elementwise comparison.
Operators can be referenced as values by preceding them with a : .
Details of operator precedence are canonically defined in the julia source
code https://github.com/JuliaLang/julia/blob/master/src/julia-parser.scm .
Precedence and associativity can be queried at runtime by passing an
operator to Base.operator_precedence or Base.operator_associativity .
To convert floats to integers without throwing, use round(), ceil(),
floor(), or trunc() . These can be passed a value: round(1.2), or a type
and then a value: round(UInt8, 1.2).
fld(x,y) floored division; quotient rounded towards -Inf
cld(x,y) ceiling division; quotient rounded towards +Inf
rem(x,y) remainder; satisfies x == div(x,y)*y + rem(x,y); sign matches x
mod(x,y) modulus; satisfies x == fld(x,y)*y + mod(x,y); sign matches y
mod1(x,y) mod with offset 1; returns r∈(0,y] for y>0 or r∈[y,0) for y<0,
where mod(r, y) == mod(x, y)
mod2pi(x) modulus with respect to 2pi; 0 <= mod2pi(x) < 2pi
divrem(x,y) returns (div(x,y),rem(x,y))
fldmod(x,y) returns (fld(x,y),mod(x,y))
gcd(x,y...) greatest positive common divisor of x, y,...
lcm(x,y...) least positive common multiple of x, y,...
Sign and absolute value functions
Function Description
abs(x) a positive value with the magnitude of x
abs2(x) the squared magnitude of x
sign(x) indicates the sign of x, returning -1, 0, or +1
signbit(x) indicates whether the sign bit is on (true) or off (false)
copysign(x,y) a value with the magnitude of x and the sign of y
flipsign(x,y) a value with the magnitude of x and the sign of x*y
Powers, logs and roots
Function Description
sqrt(x), √x square root of x
cbrt(x), ∛x cube root of x
hypot(x,y) hypotenuse of right-angled triangle with other sides of length x
and y
exp(x) natural exponential function at x
expm1(x) accurate exp(x)-1 for x near zero
ldexp(x,n) x*2^n computed efficiently for integer values of n
log(x) natural logarithm of x
log(b,x) base b logarithm of x
log2(x) base 2 logarithm of x
log10(x) base 10 logarithm of x
log1p(x) accurate log(1+x) for x near zero
exponent(x) binary exponent of x
significand(x) binary significand (a.k.a. mantissa) of a floating-point
number x
sin cos tan cot sec csc
sinh cosh tanh coth sech csch
asin acos atan acot asec acsc
asinh acosh atanh acoth asech acsch
sinc cosc
, with atan also accepting two arguments corresponding to a traditional
atan2 function.
sinpi(x) and cospi(x) are provided for more accurate computations of
sin(pi*x) and cos(pi*x) respectively.
In order to compute trigonometric functions with degrees instead of
radians, suffix the function with d.
sind cosd tand cotd secd cscd
asind acosd atand acotd asecd acscd
More functions:
https://github.com/JuliaMath/SpecialFunctions.jl
Homework: Write a julia script that produces a 2x2 matrix containing pixels
for a raytraced sphere.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/html
Size: 9290 bytes
Desc: not available
URL: <https://lists.cpunks.org/pipermail/cypherpunks/attachments/20220424/5be01e81/attachment.txt>
More information about the cypherpunks
mailing list