Created
September 13, 2017 11:10
-
-
Save willmurphyscode/dda57d3c5753a2d070281f365b691dc8 to your computer and use it in GitHub Desktop.
Calculate the fixed point of cosine in Rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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