//#define WOKWI // Uncomment if running on Wokwi RP2040 emulator.
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "pico/float.h"
/**
* @brief EXAMPLE - HELLO_C
* Simple example to initialise the IOs and then
* print a "Hello World!" message to the console.
*
* @return int Application return code (zero for success).
*/
float wallis_productsp(int iter)
{
float product = 1.0f; // Intiliase the product to 1;
for (int n = 1; n <= iter; n++)
{
float term1 = (2.0f * n) / (2.0f * n - 1.0f);// First term: (2n)/(2n-1)
float term2 = (2.0f * n) / (2.0f * n + 1.0f);// Second term: (2n)/(2n+1)
product *= term1 * term2;// Multiply the terms into the product
}
return product * 2.0f; // Multiply by 2 to approximate π
}
double wallis_productdp(int iter)
{
double product = 1.0f; // Intiliase the product to 1;
for (int n = 1; n <= iter; n++)
{
double term1 = (2.0f * n) / (2.0f * n - 1.0f);// First term: (2n)/(2n-1)
double term2 = (2.0f * n) / (2.0f * n + 1.0f);// Second term: (2n)/(2n+1)
product *= term1 * term2;// Multiply the terms into the product
}
return product * 2.0f; // Multiply by 2 to approximate π
}
int main() {
stdio_init_all();
int iterations = 100000;
float pi_ref = 3.14159265359;
float pi_w = wallis_productsp(iterations);
float float_error = pi_w - pi_ref;
double pi_w2 = wallis_productdp(iterations);
double double_error = pi_w2 - pi_ref;
printf("PI using wallis product (float): %.10f",pi_w);
printf(" approx error: %.10f\n", float_error);
printf("PI using wallis product (double): %.10f",pi_w2);
printf(" approx error:: %.10f",double_error);
// returning 0 indicates everything went okay
return 0;
// Initialise the IO as we will be using the UART
// Only required for hardware and not needed for Wokwi
// Print a console message to inform user what's going on.
// Returning zero indicates everything went okay.
return 0;
}