#define WOKWI
//#include <stdio.h>
#include <stdlib.h>
//#include "pico/stdlib.h"
//#include <math.h>
//#include "pico/float.h"
//#include "pico/double.h"
// Define the true value of PI for error calculation
#define pi_True 3.14159265359
/**
* @brief Computes PI using the Wallis product algorithm (single precision).
*
* This function calculates the approximation of PI using the Wallis product algorithm for single-precision (float) numbers.
*
* @param iteration Number of iterations to perform in the product.
* @return float The approximation of PI using single precision.
*/
float singleFloat(float iteration){
// Initialize the value of PI
float pi=2.0;
// Loop through the iterations of the Wallis product algorithm
for (int i = 2; i < iteration ; i+=2)
{
// Multiply the current value of PI by the next product in the series
pi = pi* ((float)i/(i-1)*(float)i/(i+1));
}
// Return the computed value of PI
return pi;
}
/**
* @brief Computes PI using the Wallis product algorithm (double precision).
*
* This function calculates the approximation of PI using the Wallis product algorithm for double-precision (double) numbers.
*
* @param iteration Number of iterations to perform in the product.
* @return double The approximation of PI using double precision.
*/
double doubleFloat(double iteration){
// Initialize the value of PI
double pi=2.0;
// Loop through the iterations of the Wallis product algorithm
for (int i = 2; i < iteration; i+=2)
{
// Multiply the current value of PI by the next product in the series
pi = pi* ((double)i/(i-1)*(double)i/(i+1));
}
// Return the computed value of PI
return pi;
}
/**
* @brief Main function that calculates PI using both single and double precision.
*
* This function calls the `singleFloat` and `doubleFloat` functions to calculate PI using the Wallis product algorithm for both single and double precision.
* It then calculates the error in both approximations compared to the true value of PI.
* Finally, it prints the computed PI values and their corresponding errors.
*
* @return int Return code: 0 for success.
*/
int main() {
//#ifndef WOKWI
// Initialise the IO as we will be using the UART
// Only required for hardware and not needed for Wokwi
//stdio_init_all();
//#endif
// Number of iterations
int iteration = 100000;
// Calculate PI using single precision
float pi_float = singleFloat(iteration);
// Calculate PI using double precision
double pi_double = doubleFloat(iteration);
// Calculate the error for single precision approximation
float singleError = pi_float - pi_True;
// Calculate the error for double precision approximation
double doubleError = pi_double - pi_True;
// Print the results for single precision
printf("Single precision: %f\n", pi_float);
printf("Error(float):%f\n",singleError);
// Print the results for double precision
printf("Double precision: %lf\n", pi_double);
printf("Error(double):%lf\n",doubleError);
// Returning zero indicates everything went okay.
return 0;
}