//#define WOKWI // Uncomment if running on Wokwi RP2040 emulator.
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.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).
*/
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
// call function wallis
wallis();
// Returning zero indicates everything went okay.
wallisDouble();
return 0;
}
// function wallis returns the digits of pi using floats
void wallis (){
//pi=1, this enables us to add onto pi through multiplication (x * 1 = still x)
float pi=1;
//for loop with 100000 iterations each adding to pi
for(int i=1; i<=100000; i++){
// formula for pi=(4.0i^2/4.0i^2-1) pi*pi each iteration
pi= pi* ((4.0* i * i)/((4.0* i * i)-1));
}
//real value
float piReal = 3.14159265359;
//times by 2 to get value of pi
pi=pi*2.0;
//print statements
printf("for Floats\n");
printf("my calculated value of PI is %f\n",pi);
pi=piReal - pi;
printf("the approximate error is %f\n",pi*2.0);
}
// function wallis returns the digits of pi using doubles
void wallisDouble (){
//pi=1, this enables us to add onto pi through multiplication (x * 1 = still x)
double pi=1;
//for loop with 100000 iterations each adding to pi
for(int i=1; i<=100000; i++){
// formula for pi=(4.0i^2/4.0i^2-1) pi*pi each iteration
pi= pi* ((4.0* i * i)/((4.0* i * i)-1));
}
//real value
double piReal = 3.14159265359;
//times by 2 to get value of pi
pi=pi*2.0;
//print statements
printf("for Doubles\n");
printf("my calculated value of PI is %f\n",pi);
pi=piReal - pi;
printf("the approximate error is %f\n",pi*2);
}