Your own math function : Math extensions « Development « Ruby






Your own math function


module Math
  RAD2DEG = 360.0/(2.0*PI)  # Radians to degrees
  RAD2GRAD = 400.0/(2.0*PI) # Radians to grads
end

def sin_d(theta)
  Math.sin(theta/Math::RAD2DEG)
end

def sin_g(theta)
  Math.sin(theta/Math::RAD2GRAD)
end

def atan2_d(y,x)
  Math.atan2(y,x)/Math::RAD2DEG
end

def atan2_g(y,x)
  Math.atan2(y,x)/Math::RAD2GRAD
end

def arcsin(x)
Math.atan2(x,Math.sqrt(1.0-x*x))
end

def arccos(x)
Math.atan2(Math.sqrt(1.0-x*x),x)
end

def arctan(x)
Math.atan2(x,1.0)
end

def sinh(x)
  (Math.exp(x)-Math.exp(-x))/2.0
end

def cosh(x)
  (Math.exp(x)+Math.exp(-x))/2.0
end

def tanh(x)
  sinh(x)/cosh(x)
end

def asinh(x)
  Math.log(x + Math.sqrt(1.0+x**2))
end

def acosh(x)
  2.0 *Math.log(Math.sqrt((x+1.0)/2.0)+Math.sqrt((x-1)/2.0))
end

def atanh(x)
  (Math.log(1.0+x) - Math.log(1.0-x))/2.0
end

 








Related examples in the same category

1.Add function to Math module
2.Get mean value
3.Get hmean value
4.Return the gmean value
5.Get the median value
6.Get the mode value
7.Get variance value
8.Get sigma value