-
-
Save RandyMcMillan/4cbab8186ea4a2df65acf8838b1908db to your computer and use it in GitHub Desktop.
viete_pi_hex.rs
This file contains hidden or 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
| use std::f64::consts::PI; | |
| /// Calculates an approximation of Pi using Viète's formula. | |
| /// | |
| /// The formula is based on an infinite product of terms involving nested square roots. | |
| /// This function approximates the product up to a given number of iterations. | |
| /// | |
| /// # Arguments | |
| /// | |
| /// * `iterations` - The number of terms to include in the product. More iterations | |
| /// lead to a more precise approximation. | |
| /// | |
| /// # Returns | |
| /// | |
| /// The calculated approximation of Pi as a `f64`. | |
| fn viete_pi_approximation(iterations: u32) -> f64 { | |
| let mut product = 1.0; | |
| let mut current_sqrt_term = 0.0; | |
| for _ in 0..iterations { | |
| // Recursively calculate the next nested square root term. | |
| // For the first iteration, this is sqrt(2 + 0) = sqrt(2). | |
| // For subsequent iterations, it's sqrt(2 + previous_term). | |
| current_sqrt_term = (2.0f64 + current_sqrt_term).sqrt(); | |
| // Multiply the next factor into the running product. | |
| // The factor is 2 divided by the current nested square root term. | |
| product *= 2.0 / current_sqrt_term; | |
| } | |
| // According to the formula, Pi is 2 times the infinite product. | |
| 2.0 * product | |
| } | |
| fn main() { | |
| let iterations = u32::MAX / (u32::MAX / 999999999u32); | |
| let pi_approx = viete_pi_approximation(iterations); | |
| println!("Calculating Pi with Viète's formula:"); | |
| println!("Number of iterations: {}", iterations); | |
| println!("Approximation of Pi: {}", pi_approx); | |
| println!("Actual Pi (from `std::f64::consts::PI`): {}", PI); | |
| println!("Difference: {}", (PI - pi_approx).abs()); | |
| { | |
| let iterations = u32::MAX / (u32::MAX / 999999999u32); | |
| let pi_approx = viete_pi_approximation(iterations); | |
| println!("Calculating Pi with Viète's formula:"); | |
| println!("Number of iterations: {}", iterations); | |
| println!("Approximation of Pi: {}", pi_approx); | |
| println!("Actual Pi (from `std::f64::consts::PI`): {}", PI); | |
| println!("Difference: {}\n", (PI - pi_approx).abs()); | |
| // --- Requested Hex and Binary Output --- | |
| // Get the raw 64-bit unsigned integer representation of PI | |
| let pi_bits = PI.to_bits(); | |
| println!("--- IEEE 754 Double-Precision (f64) Representation of PI ---"); | |
| // :X prints the number in uppercase hexadecimal format | |
| println!("PI in Hexadecimal: {:X}", pi_bits); | |
| // :064b prints the number in binary, padded with leading zeros to 64 bits | |
| println!("PI in Binary (IEEE 754): {:064b}", pi_bits); | |
| // 0 [Sign] 10000000001 [Exponent] 0010010001000011111101101010100010001000010110100011000 [Fraction] | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5764098aa008fc8c9808ed4d3ba857b0