Skip to content

Instantly share code, notes, and snippets.

@willmurphyscode
Created September 13, 2017 11:10
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/dda57d3c5753a2d070281f365b691dc8 to your computer and use it in GitHub Desktop.
Save willmurphyscode/dda57d3c5753a2d070281f365b691dc8 to your computer and use it in GitHub Desktop.
Calculate the fixed point of cosine in Rust.
const TOLERANCE: f64 = 0.00000000000000000001;
fn tolerance_equals(a: f64, b: f64) -> bool {
((a - b) * (a - b)) < TOLERANCE
}
fn recursive_cosine_fixedpoint(a: f64) -> f64 {
if tolerance_equals(a, a.cos()) {
a
} else {
recursive_cosine_fixedpoint(a.cos())
}
}
fn main() {
println!("The fixed point of cosine is {}", recursive_cosine_fixedpoint(1f64));
}
// 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