Skip to content

Instantly share code, notes, and snippets.

@trishume
Created June 8, 2012 01:33
Show Gist options
  • Save trishume/2892877 to your computer and use it in GitHub Desktop.
Save trishume/2892877 to your computer and use it in GitHub Desktop.
My Hand-Made ruby cosine function
def cos(num,its=1000)
def factorial(num)
return 1 if num == 0
num.downto(1).inject(:*)
end
sum = 0.0
sign = 1
its.times do |i|
exp = i*2
sum += sign * num**exp / factorial(exp)
sign *= -1.0
end
sum
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment