Ruby 언어로 역삼각함수, 역쌍곡선함수 값을 구하는 예제
역삼각함수란 삼각함수의 역함수를 의미하고,
역쌍곡선함수란 쌍곡선함수의 역함수를 의미한다.
수학에서 sin 함수의 역함수는 arcsin 으로 표기되는데,
Ruby 언어에서는 Math::asin() 함수로 구현되어 있다.
다음 소스는 Ruby, JRuby 중 어느 것으로 실행해도 같은 결과를 얻는다.
# Filename: testArcSine.rb
#
# Execute: ruby testArcSine.rb
#
# Or
#
# Execute: jruby testArcSine.rb
#
# Date: 2013. 1. 1.
# Copyright (c) pkim _AT_ scripts.pe.kr
def sin(x)
y = Math::sin(x)
return y
end
def asin(x)
y = Math::asin(x)
return y
end
def sinh(x)
y = Math::sinh(x)
return y
end
def cosh(x)
y = Math::cosh(x)
return y
end
def asinh(x)
y = Math::log(x + Math::sqrt(x*x + 1))
return y
end
def acosh(x)
y = Math::log(x + Math::sqrt(x*x - 1))
return y
end
x = -0.9
y = asin(x)
print "y = asin(%.1g) = %.9f\n" % [x, y]
print "sin(y) = sin(%.9f) = %.1g\n" % [y, sin(y)]
print "\n"
x = 1.1
u = acosh(x)
print "u = acosh(%3.2g) = %.10f\n" % [x, u]
v = asinh(x)
print "v = asinh(%3.2g) = %.10f\n" % [x, v]
print "cosh(u) = cosh(%.10f) = %3.2g\n" % [u, cosh(u)]
print "sinh(v) = sinh(%.10f) = %3.2g\n" % [v, sinh(v)]
=begin
Output:
y = asin(-0.9) = -1.119769515
sin(y) = sin(-1.119769515) = -0.9
u = acosh(1.1) = 0.4435682544
v = asinh(1.1) = 0.9503469298
cosh(u) = cosh(0.4435682544) = 1.1
sinh(v) = sinh(0.9503469298) = 1.1
=end