Your own math function : Math extensions « Development « Ruby

Home
Ruby
1.ActiveRecord
2.Array
3.CGI
4.Class
5.Collections
6.Database
7.Date
8.Design Patterns
9.Development
10.File Directory
11.GUI
12.Hash
13.Language Basics
14.Method
15.Network
16.Number
17.Rails
18.Range
19.Reflection
20.Statement
21.String
22.Threads
23.Time
24.Tk
25.Unit Test
26.Windows Platform
27.XML
Ruby » Development » Math extensions 




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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.