Ahmed Ahmed Ahmed Ahmed - 1 year ago 50
Ruby Question

Math.acos gives me a different value than my calculator

In Ruby,

Math.acos(10/70) * 57.2958

should return approximately 80 degrees (according to my calculator).
However it is returning approximately 90 degrees. Why is that?


You need to add decimal points, or Ruby will assume integer division:

irb(main):006:0> Math.acos(10/70) / Math::PI * 180
=> 90.0
irb(main):007:0> Math.acos(10.0/70) / Math::PI * 180
=> 81.78678929826181

If you just use 10/70, this will round to 0, and you get Math.acos(0) instead of Math.acos(0.14..) which is what you want.