Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
Created September 13, 2017 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willmurphyscode/da2ea71cb6cc2eac467a8db5cdac54ad to your computer and use it in GitHub Desktop.
Save willmurphyscode/da2ea71cb6cc2eac467a8db5cdac54ad to your computer and use it in GitHub Desktop.
Calculate the fixed point of cosine in Ruby.
TOLERANCE = 0.00000000000000000001
def tolerance_equals?(a, b)
((a - b) * (a - b)) < TOLERANCE
end
def recursive_cosine_fixedpoint(a)
return a if tolerance_equals?(a, Math.cos(a))
recursive_cosine_fixedpoint(Math.cos(a))
end
puts "The fixed point of cosine is #{recursive_cosine_fixedpoint(1)}"
# The fixed point of cosine is 0.7390851331706995
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment