#define WOKWI // Uncomment if running on Wokwi RP2040 emulator.
#include <stdio.h>
#include <stdlib.h>
// #include "pico/stdlib.h"
#include "pico/float.h"
#include "pico/double.h"
#include <math.h>
#define FACTOR 2.0f
#define DOUBLE_FACTOR 2.0
/**
* @brief Calculate Pi by using Wallis product in Single precision
* this function implements calculating the approximate value of pi by using single precision float points
*
* @param precision the iteration number of the calculation which is the precision
* @return Calculated value of PI in single precision.
*/
float singlePrecisionForPi(int iterations)
{
float halfPi = 1.0f; //initialize half pi as 1 but a float 1.0
for (int i = 1; i <= iterations; i++) //loop number of iterations times
{
float numerator = (FACTOR * i) * (FACTOR * i); //calculate numerator by using Wallis product formula
float denominator = ((FACTOR * i) - 1) * ((FACTOR * i) + 1); // (2n/2n-1)*(2/2n+1) such that n is from 1 to the number of iterations
halfPi *= numerator / denominator; //cumulative multiplication by using formula
}
float result = halfPi * 2.0f; //calculate pi value by multiply halpPi by 2
return result; //calculated Pi value
}
/**
* @brief Calculate Pi by using 1 product in Double precision
* this function implements calculating the approximate value of pi by using Double precision double
*
* @param precision the iteration number of the calculation which is the precision
* @return Calculated value of PI in double precision.
*/
double doublePrecisionForPi(int iterations)
{
double halfPi = 1.0; //initialize half pi as 1
for (int i = 1; i <= iterations; i++) //loop number of iterations times
{
double numerator = (DOUBLE_FACTOR * i) * (DOUBLE_FACTOR * i); //calculate numerator by using Wallis product formula
double denominator = ((DOUBLE_FACTOR * i) - 1) * ((DOUBLE_FACTOR * i) + 1); // (2n/2n-1)*(2/2n+1) such that n is from 1 to the number of iterations
halfPi *= numerator / denominator; //cumulative multiplication by using formula
}
double result = halfPi * 2.0; //calculate pi value by multiply halpPi by 2
return result; //calculated Pi value
}
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
int n_Value = 100000;
float singleCal = singlePrecisionForPi(n_Value); //calculate pi by using single precision
double doubleCal = doublePrecisionForPi(n_Value); //calculate pi by using double precision
double piDefult = 3.14159265359; //defult value of pi
printf("pi calculated by using single precision is: %.55f\n", singleCal); //print calculated pi by using single precision
printf("approximation error for single precision is: %.55f\n", fabs(singleCal - piDefult));//get positive error value
printf("pi calculated by using double precision is: %.55f\n", doubleCal); //print calculated pi by using single precisio
printf("approximation error for double precision is: %.55f\n", fabs(doubleCal - piDefult));//get positive error value
// Returning zero indicates everything went okay.
return 0;
}