Skip to content

Instantly share code, notes, and snippets.

@yuyosy
Created September 25, 2018 13:39
Show Gist options
  • Save yuyosy/43cae100451f4772aa7df764a5e20ff8 to your computer and use it in GitHub Desktop.
Save yuyosy/43cae100451f4772aa7df764a5e20ff8 to your computer and use it in GitHub Desktop.
Julia <calc>
x = 2
y = 3
println("x = $x, y = $y")
# 変数を含む計算 (*の省略)
println("2x+1 = ", 2x+1)
println("(x+2)x = ", (x+2)x) # xを括弧の前にするのは不可 x(x+2)
# 表記が関数と混在するため
# 除算
println("4/3 = ", 4/3) # 結果は実数 1.3333333...
println("div(4, 3) = ", div(4, 3)) # 結果は整数 1
println("2\\3 = ", 2\3) # 3/2 と同等
# 余り
println("8%3 = ", 8%3) # rem(8,3) と同等
# 累乗
println("2^10 = ", 2^10)
println()
# 複素数
i1 = 2 + 4im
i2 = 4 - 3im
println("i1 + i2 = ", i1 + i2)
# 小数表記
println(.018) # 0.018
println(3.29e-2) # 0.0329
println(8.19e3) # 8190.0
println()
# 各要素に演算を適用
# Juliaでは、一部を除くほとんどの演算子は関数である。
# そのため(1, 2, 3)などのタプルに対して演算を適用することができる
println(+(1, 2, 3, 4, 5)) # 1+2+3+4+5
println(*(+(1, 1, 2), +(2,3))) # (1+1+2)*(2+3)
# 値入れ替え(スワップ)
println("x, y = ", x, ", ", y)
x,y = y,x
println("x, y = ", x, ", ", y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment