Ruby と Python と Elixir と。

除算の違い。
[bash]
# Ruby
puts 3.to_f / 2 #=> 1.5
puts 3 / 2 #=> 1

# Python
print(3 / 2) # => 1.5
print(3 // 2) # => 1

# Elixir ※1.4以降
require Integer
IO.puts(3 / 2) # => 1.5
IO.puts(Integer.floor_div(3, 2)) # => 1
[/bash]

剰余の違い。
[bash]
# Ruby
puts 5 % 2 #=> 1

# Python
print(5 % 2) # => 1

# Elixir ※1.4以降
require Integer
IO.puts(Integer.mod(5, 1)) # => 1
[/bash]

べき乗の違い
[bash]
# Ruby
puts 2 ** 3 #=> 8

# Python
print(2 ** 3) # => 8

# Elixir ※Erlangの関数使用
IO.puts(:math.pow(2,3)|> round) # => 8
[/bash]

This entry was posted in Elixir, Python, Ruby, 技術情報. Bookmark the permalink.

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です