Ahmed Ahmed Ahmed Ahmed - 2 years ago 85
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?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download