LoginSignup
5
5

More than 5 years have passed since last update.

ラジアンと度の変換をするRubyコード

Posted at

サンプルコード

# 度からラジアンを求める
def radian(degree)
  degree * Math::PI / 180
end

# ラジアンから度を求める
def degree(radian)
  radian * 180 / Math::PI
end

a = [-360, -315, -270, -225, -180, -90, -45, 0, 45, 90, 180, 225, 270, 315, 360]
a.each{|v|
  r = radian(v) # 度をラジアンに変換
  d = degree(r) # ラジアンを度に変換
  puts "#{v} -> #{r} -> #{d}"
}

実行結果

-360 -> -6.283185307179586 -> -360.0
-315 -> -5.497787143782138 -> -315.0
-270 -> -4.71238898038469 -> -270.0
-225 -> -3.9269908169872414 -> -225.0
-180 -> -3.141592653589793 -> -180.0
-90 -> -1.5707963267948966 -> -90.0
-45 -> -0.7853981633974483 -> -45.0
0 -> 0.0 -> 0.0
45 -> 0.7853981633974483 -> 45.0
90 -> 1.5707963267948966 -> 90.0
180 -> 3.141592653589793 -> 180.0
225 -> 3.9269908169872414 -> 225.0
270 -> 4.71238898038469 -> 270.0
315 -> 5.497787143782138 -> 315.0
360 -> 6.283185307179586 -> 360.0

参考資料

5
5
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5