// Testing float16 library
// https://github.com/RobTillaart/float16
// For: https://forum.arduino.cc/t/a-2-byte-float/1170014
// This Wokwi project: https://wokwi.com/projects/376313228108456961


#include <TinyDebug.h>    // a feature of Wokwi, an internal serial output
#include <float16.h>

void setup() 
{
  Debug.begin();
  Debug.println("Test sketch for float16");

  int iterations = 20;

  Debug.println(PiGregoryLeibniz16(iterations));
  Debug.println(PiGregoryLeibniz32(iterations),10);
}

void loop() {}


// The Gregory-Leibniz method
float16 PiGregoryLeibniz16(int n)
{
  float16 pi(1);
  const float16 one(1);
  const float16 two(2);
  unsigned int count = 3;
  for(int i=0; i<n; i++)
  {
    pi -= one / float16(count);
    count += 2;
    pi += one / float16(count);
    count += 2;
  }
  return(float16(4)*pi);
}


// The Gregory-Leibniz method
float PiGregoryLeibniz32(int n)
{
  float pi = 1.0;
  unsigned int count = 3;
  for(int i=0; i<n; i++)
  {
    pi -= 1.0 / (float) count;
    count += 2.0;
    pi += 1.0 / (float) count;
    count += 2.0;
  }
  return(4.0 * pi);
}
ATTINY8520PU
tiny:PB5
tiny:PB3
tiny:PB4
tiny:GND
tiny:PB0
tiny:PB1
tiny:PB2
tiny:VCC