/* Sketch to test posit library
 *
 *
 */
//#define ES8 2 // number of bits in exponent field (default zero)
#include "Posit.h"

char cs[80]; // C string for table
double numbersList[16]= {0,NAN,1.0,-2.0,3.0,7.0,-11.0,12.0,50.0,-333.0,0.5,NAN,-0.05,0.005,0.01};

void setup() {
  Serial.begin(9600);
  Serial.println("Test of Posit library\n");

  /*Serial.println("Creation of all posit8 values from raw integer");  
  for (int raw = 0; raw<256 ; raw ++) {
    Posit8 rawPosit ((int8_t)raw); // signed byte since posit are signed
    Serial.print("Raw8 : "); Serial.print(rawPosit.value, BIN);
    Serial.print(" "); Serial.println(posit2float(rawPosit),5);
  } 
  Serial.println(); //*/
 
 /*Serial.println("Creation of many posit16 values from raw integer");  
  for (long raw = 0; raw<65535 ; raw += sqrt(random(50000))) {
    Posit16 rawPosit ((uint16_t)raw); // signed byte since posit are signed
    Serial.print("Raw16 : "); Serial.print(rawPosit.value, BIN);
    Serial.print(" "); Serial.println(posit2float(rawPosit),12);
  } 
  Serial.println(); //*/

  Serial.println("Creation of two posit8 values from input strings"); 
  Serial.println("First Posit? ");
  while (Serial.available() == 0) {
   }
  float floatValue = Serial.parseFloat();
  Serial.println(floatValue); 
  Posit8 firstPosit (floatValue);
  while (Serial.available() > 0) Serial.read(); // Eliminate extra chars
  Serial.println("Second Posit? ");
  while (Serial.available() == 0) { 
  }
  Posit8 secondPosit (Serial.parseFloat());
  while (Serial.available() > 0) Serial.read(); // Eliminate extra chars
  
  Serial.print("First(");
  Serial.print(firstPosit.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(firstPosit),4);
  Serial.print("Second(");
  Serial.print(secondPosit.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(secondPosit),4); //*/

  Serial.println("Test of operations");  
  Posit8 sum = Posit8::posit8_add(firstPosit, secondPosit);
  //Posit8 sum = firstPosit + secondPosit;  // doesn"t work yet
  Serial.print("Sum(");
  Serial.print(sum.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(sum),4);

	Posit8 sub = Posit8::posit8_sub(firstPosit, secondPosit);  
  //Posit8 sub = firstPosit - secondPosit;  // doesn"t work yet
  Serial.print("Sub(");
  Serial.print(sub.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(sub),5); 	
  
  Posit8 mul = posit8_mul(firstPosit, secondPosit);  
  //Posit8 mul = firstPosit * secondPosit;  // doesn"t work yet
  Serial.print("Mul(");
  Serial.print(mul.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(mul),4); 	
  
  Posit8 div = posit8_div(firstPosit, secondPosit);  
  //Posit8 div = firstPosit / secondPosit;  // doesn"t work yet
  Serial.print("Div(");
  Serial.print(div.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(div),5); //*/

  /*Serial.println("Creation of two posit16 values from input strings"); 
  Serial.println("First Posit? ");
  while (Serial.available() == 0) {
   }
  Posit16 firstP16 (Serial.parseFloat());
  while (Serial.available() > 0) Serial.read(); // Eliminate extra chars
  Serial.println("Second Posit? ");
  while (Serial.available() == 0) { 
  }
  Posit16 secondP16 (Serial.parseFloat());
  
  Serial.print("First(");
  Serial.print(firstP16.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(firstP16),10);
  Serial.print("Second(");
  Serial.print(secondP16.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(secondP16),10); //*/

  /*Serial.println("Test of operations");  
  Posit16 sum16 = Posit16::posit16_add(firstP16, secondP16);
  //Posit16 sum = firstP16 + secondP16;  // doesn"t work yet
  Serial.print("Sum(");
  Serial.print(sum16.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(sum16),10);

  Posit16 sub16 = Posit16::posit16_sub(firstP16, secondP16);
  //Posit16 sub16 = firstP16 - secondP16;  // doesn"t work yet
  Serial.print("Sub(");
  Serial.print(sub16.value, BIN);
  Serial.print(") ");
  Serial.println(posit2float(sub16),10); //*/
  

  Serial.println("Table of random results"); 
  randomSeed(millis());
  char as[10],bs[10];
  Serial.println("   A   :   B   :   abin  :   bbin  :   sum   :   sub   :   mul   :   div");
  Serial.println("-------+-------+---------+---------+---------+---------+---------+---------");
  for (int j=0; j<16; j++) {
      Posit8 firstPosit=Posit8(numbersList[random(16)]); 
      Posit8 secondPosit=Posit8(numbersList[random(16)]); 
      Posit8 sum = Posit8::posit8_add(firstPosit, secondPosit);
    	Posit8 sub = Posit8::posit8_sub(firstPosit, secondPosit);  
      Posit8 mul = posit8_mul(firstPosit, secondPosit);  
      Posit8 div = posit8_div(firstPosit, secondPosit); 

      Serial.print(posit2float(firstPosit),3); // No way to align numbers in table
      Serial.print(" + ");
      Serial.print(posit2float(secondPosit),3);
      Serial.print(" + ");
      Serial.print(firstPosit.value,BIN);
      Serial.print(" + ");
      Serial.print(secondPosit.value,BIN);
      Serial.print(" + ");
      Serial.print(posit2float(sum),3);
      Serial.print(" + ");
      Serial.print(posit2float(sub),3);
      Serial.print(" + ");
      Serial.print(posit2float(mul),3);
      Serial.print(" + ");
      Serial.println(posit2float(div),3);
    } //*/
}

void loop() { // Nothing here
}
uno:A5.2
uno:A4.2
uno:AREF
uno:GND.1
uno:13
uno:12
uno:11
uno:10
uno:9
uno:8
uno:7
uno:6
uno:5
uno:4
uno:3
uno:2
uno:1
uno:0
uno:IOREF
uno:RESET
uno:3.3V
uno:5V
uno:GND.2
uno:GND.3
uno:VIN
uno:A0
uno:A1
uno:A2
uno:A3
uno:A4
uno:A5