How to resolve the algorithm Trigonometric functions step by step in the Ruby programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Trigonometric functions step by step in the Ruby programming language

Table of Contents

Problem Statement

If your language has a library or built-in functions for trigonometry, show examples of: using the same angle in radians and degrees. For the non-inverse functions,   each radian/degree pair should use arguments that evaluate to the same angle   (that is, it's not necessary to use the same angle for all three regular functions as long as the two sine calls use the same angle). For the inverse functions,   use the same number and convert its answer to radians and degrees. If your language does not have trigonometric functions available or only has some available,   write functions to calculate the functions based on any   known approximation or identity.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Trigonometric functions step by step in the Ruby programming language

. Trigonometry Functions in Ruby - Math Library

The provided Ruby code showcases the usage of trigonometric functions and their inverse counterparts from Ruby's Math library. It also includes a custom implementation for tangent, arcsine, and arccosine using the BigDecimal library for more precise calculations.

Math Library Functions:

  • deg2rad(d): Converts a degree measure to radians (d * pi / 180).

  • rad2deg(r): Converts a radian measure to degrees (r * 180 / pi).

  • sine: Calculates the sine of an angle in radians or degrees.

  • cosine: Calculates the cosine of an angle in radians or degrees.

  • tangent: Calculates the tangent of an angle in radians or degrees.

  • arcsine: Calculates the arcsine of a value in the range [-1, 1] in radians.

  • arccosine: Calculates the arccosine of a value in the range [-1, 1] in radians.

  • arctangent: Calculates the arctangent of a value in the range [-inf, inf] in radians.

Custom Functions Using BigDecimal Library:

  • tan(x, prec): Calculates the tangent of x with prec decimal precision.
  • asin(y, prec): Calculates the arcsine of y with prec decimal precision.
  • acos(x, prec): Calculates the arccosine of x with prec decimal precision.

Code Explanation:

  • The code first sets up some constants and defines utility functions for converting between radians and degrees.
  • It then demonstrates the usage of standard Math library functions for sine, cosine, tangent, arcsine, arccosine, and arctangent with both radians and degrees.
  • It loads the BigDecimal and BigMath libraries for high-precision calculations.
  • It redefines the tangent, arcsine, and arccosine functions to use BigDecimal and BigMath for more precision.
  • The code then sets up a precision of 52 decimal places and pi with that precision.
  • Finally, it prints out the results of trigonometric calculations using both radians and degrees, demonstrating the high precision possible with the BigDecimal library.

Source code in the ruby programming language

radians = Math::PI / 4
degrees = 45.0

def deg2rad(d)
  d * Math::PI / 180
end

def rad2deg(r)
  r * 180 / Math::PI
end

#sine
puts "#{Math.sin(radians)} #{Math.sin(deg2rad(degrees))}"
#cosine
puts "#{Math.cos(radians)} #{Math.cos(deg2rad(degrees))}"
#tangent
puts "#{Math.tan(radians)} #{Math.tan(deg2rad(degrees))}"
#arcsine
arcsin = Math.asin(Math.sin(radians))
puts "#{arcsin} #{rad2deg(arcsin)}"
#arccosine
arccos = Math.acos(Math.cos(radians))
puts "#{arccos} #{rad2deg(arccos)}"
#arctangent
arctan = Math.atan(Math.tan(radians))
puts "#{arctan} #{rad2deg(arctan)}"


require 'bigdecimal'       # BigDecimal
require 'bigdecimal/math'  # BigMath

include BigMath  # Allow sin(x, prec) instead of BigMath.sin(x, prec).

# Tangent of _x_.
def tan(x, prec)
  sin(x, prec) / cos(x, prec)
end

# Arcsine of _y_, domain [-1, 1], range [-pi/2, pi/2].
def asin(y, prec)
  # Handle angles with no tangent.
  return -PI / 2 if y == -1
  return PI / 2 if y == 1
  
  # Tangent of angle is y / x, where x^2 + y^2 = 1.
  atan(y / sqrt(1 - y * y, prec), prec)
end

# Arccosine of _x_, domain [-1, 1], range [0, pi].
def acos(x, prec)
  # Handle angle with no tangent.
  return PI / 2 if x == 0
  
  # Tangent of angle is y / x, where x^2 + y^2 = 1.
  a = atan(sqrt(1 - x * x, prec) / x, prec)
  if a < 0
    a + PI(prec)
  else
    a
  end
end


prec = 52
pi = PI(prec)
degrees = pi / 180  # one degree in radians

b1 = BigDecimal.new "1"
b2 = BigDecimal.new "2"
b3 = BigDecimal.new "3"

f = proc { |big| big.round(50).to_s('F') }
print("Using radians:",
      "\n  sin(-pi / 6) = ", f[ sin(-pi / 6, prec) ],
      "\n  cos(3 * pi / 4) = ", f[ cos(3 * pi / 4, prec) ],
      "\n  tan(pi / 3) = ", f[ tan(pi / 3, prec) ],
      "\n  asin(-1 / 2) = ", f[ asin(-b1 / 2, prec) ],
      "\n  acos(-sqrt(2) / 2) = ", f[ acos(-sqrt(b2, prec) / 2, prec) ],
      "\n  atan(sqrt(3)) = ", f[ atan(sqrt(b3, prec), prec) ],
      "\n")
print("Using degrees:",
      "\n  sin(-30) = ", f[ sin(-30 * degrees, prec) ],
      "\n  cos(135) = ", f[ cos(135 * degrees, prec) ],
      "\n  tan(60) = ", f[ tan(60 * degrees, prec) ],
      "\n  asin(-1 / 2) = ",
      f[ asin(-b1 / 2, prec) / degrees ],
      "\n  acos(-sqrt(2) / 2) = ",
      f[ acos(-sqrt(b2, prec) / 2, prec) / degrees ],
      "\n  atan(sqrt(3)) = ",
      f[ atan(sqrt(b3, prec), prec) / degrees ],
      "\n")


  

You may also check:How to resolve the algorithm Loops/Foreach step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Strip whitespace from a string/Top and tail step by step in the Stata programming language
You may also check:How to resolve the algorithm Hofstadter Q sequence step by step in the ARM Assembly programming language
You may also check:How to resolve the algorithm Catamorphism step by step in the Haskell programming language
You may also check:How to resolve the algorithm Averages/Mean angle step by step in the R programming language