// Sketch from here:
// https://forum.arduino.cc/t/trying-to-understand-floating-point-accuracy/948268
// Test floating point calculations and division
#include "BigNumber.h"
// The compiler uses int and long and so on for the constants.
// With large constant values, the BigNumber has trouble
// to convert them.
// So the constants are written down as text string.
BigNumber num1 = "10000";
BigNumber num2 = "3333";
BigNumber num3 = "100000";
BigNumber num4 = "33333";
BigNumber num5 = "1000000";
BigNumber num6 = "333333";
void setup() {
BigNumber::begin(100); // 100 digits after the dot
Serial.begin (9600);
Serial.println ("Start Calculating");
Serial.println ("--------");
Serial.println (num1); // should print 1000000.00000
num1 = (num1 / BigNumber(3));
Serial.println (num1); // should print 33333.33333
num1 = num1 - num2;
Serial.println (num1); // should print 0.33333
num1 = num1 * BigNumber(3);
Serial.println (num1); // should print 0.99999
Serial.println ("--------");
Serial.println (num3); // should print 100000.00000
num3 = (num3 / BigNumber(3));
Serial.println (num3); // should print 33333.33333
num3 = num3 - num4;
Serial.println (num3); // should print 0.33333
num3 = num3 * BigNumber(3);
Serial.println (num3); // should print 0.99999
Serial.println ("--------");
Serial.println (num5); // should print 1000000.00000
num5 = (num5 / BigNumber(3));
Serial.println (num5); // should print 333333.33333
num5 = num5 - num6;
Serial.println (num5); // should print 0.33333
num5 = num5 * BigNumber(3);
Serial.println (num5); // should print 0.99999
}
void loop() {
}