Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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