#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 FACTOR_DOUBLE 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 pi = 1.0f;
    for (int i = 1; i <= iterations; i++) 
    {
        float numerator = (FACTOR * i) * (FACTOR * i);
        float denominator = ((FACTOR * i) - 1) * ((FACTOR * i) + 1);
        pi *= numerator / denominator;
    }
    float result = pi * 2.0f;
    return result;  
}


/**
 * @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 pi = 1.0;
    for (int i = 1; i <= iterations; i++) 
    {
        double numerator = (FACTOR_DOUBLE * i) * (FACTOR_DOUBLE * i);
        double denominator = ((FACTOR_DOUBLE * i) - 1) * ((FACTOR_DOUBLE * i) + 1);
        pi *= numerator / denominator;
    }
    double result = pi * 2.0;
    return result;  
}


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 iteration = 100000;
float singleCal = singlePrecisionForPi(iteration);
double doubleCal = doublePrecisionForPi(iteration);
double piDefult = 3.14159265359;

printf("pi calculated by using single precision is: %f\n", singleCal);
printf("single precision error is: %.60f\n", singleCal - piDefult);


printf("pi calculated by using single precision is: %lf\n", doubleCal);
printf("double precision error is: %.60f\n", doubleCal - piDefult);


    // Returning zero indicates everything went okay.
    return 0;
}
BOOTSELLED1239USBRaspberryPiPico©2020RP2-8020/21P64M15.00TTT