I have quite a practical need to use the BigNumber library finally.
I need to calculate this simple formula:
// BigNumber library
#include "BigNumber.h"
void setup() {
Serial.begin(115200);
BigNumber::begin (); // initialize the library
BigNumber result;
unsigned long a, b;
unsigned int c, d; // or unsigned long
double e;
// an example of typical values (these values are not constants,
// they come from an external measurement system):
a = 23948398;
b = 8380334;
c = 135;
d = 12032;
e = 0.72348;
// the actual formula:
result = (a * b) / (c * 65536 + d + e);
// the result = 22653423.351809387532795061757447
Serial.println(result);
}
void loop() {
};
It compiles, gives 0 (or, when playing with types/castings - compile errors or results ignoring some variables values).
I've tried to use these libs, with all possible variations of operand's types, splitting the formula, etc.
Of course it does. The type that the result is to be stored in matters ONLY when the result is to be stored. Until then, the type of the value to be stored is that of the largest type in the expression. Since the largest type in the expression is float, the value will be 4 bytes. And, since a and b are longs, a * b will be long, too.
Casting is needed, OR make one or more of the variables involved BigNumber.
I did my best with various castings, splittings, reordering..
If somebody may show me the actual mod of the above code giving the correct result I would be very
// BigNumber library
#include "BigNumber.h"
void setup() {
Serial.begin(115200);
BigNumber::begin(); // initialize library
BigNumber result, ab, bb, cb, db, eb;
unsigned long a, b;
unsigned int c, d; // or unsigned long
double e;
// an example of typical values:
a = 23948398;
b = 8380334;
c = 7883;
d = 12032;
e = 0.72348;
ab = a;
bb = b;
cb = c;
db = d;
eb = e;
// the actual formula:
result = (ab * bb) / (cb * 65536 + db + eb);
Serial.println(result);
printBigNum(result);
}
void loop() {
};
//function to display a big number and free it afterwards
//modified println to print
void printBigNum (BigNumber n)
{
char * s = n.toString ();
Serial.print (s);
free (s);
} // end of printBignum
It does not compile, overloading issues:
D:\MyCode\Arduino\BigNumber_test\BigNumber_test.ino: In function 'void setup()':
D:\MyCode\Arduino\BigNumber_test\BigNumber_test.ino:35: error: ambiguous overload for 'operator*' in 'cb * 65536l'
D:\MyCode\Arduino\BigNumber_test\BigNumber_test.ino:35: note: candidates are: operator*(long int, long int) <built-in>
D:\MyCode\Arduino\libraries\BigNumber/BigNumber.h:67: note: BigNumber BigNumber::operator*(const BigNumber&) const
Failed compiling sketch
Which version of the * operator should the compiler use? It's telling you that it can't choose for you. You must tell it, by making 65536 a BigNumber variable, or by using a cast.
// BigNumber library
#include "BigNumber.h"
void setup() {
Serial.begin(115200);
BigNumber::begin(); // initialize library
BigNumber result, ab, bb, cb, db, eb, to16;
unsigned long a, b;
unsigned int c, d; // or unsigned long
double e;
to16 = "65536";
// an example of typical values:
a = 23948398;
b = 8380334;
c = 7883;
d = 12032;
e = 0.72348;
ab = a;
bb = b;
cb = c;
db = d;
eb = e;
// the actual formula:
result = (ab * bb) / (cb * to16 + db + eb);
Serial.println(result);
printBigNum(result);
}
void loop() {
};
//function to display a big number and free it afterwards
//modified println to print
void printBigNum (BigNumber n)
{
char * s = n.toString ();
Serial.print (s);
free (s);
} // end of printBignum
Instead of computing the whole equation in one step, break it into intermediate steps. What is ab * bb? What is cb * to16? What is that value + db? What is that value + eb?
// BigNumber library
#include "BigNumber.h"
void setup() {
Serial.begin(115200);
BigNumber::begin(); // initialize library
BigNumber result, ab, bb, cb, db, eb, to16;
unsigned long a, b;
unsigned int c, d; // or unsigned long
double e;
to16 = "65536";
// an example of typical values (vars, not constants):
a = 23948398;
b = 8380334;
c = 135;
d = 12032;
e = 0.72348;
ab = a;
bb = b;
cb = c;
db = d;
eb = e;
// the actual formula:
// result = (a * b) / (c * 65536 + d + e);
// shall be result = 22653423.351809387532795061757447
// casted to bignum
// result = (ab * bb) / (cb * to16 + db + eb);
// Serial.println(result);
// printBigNum(result);
BigNumber t1 = ab * bb; // ( a * b )
printBigNum(t1);
BigNumber t2 = cb * to16; // ( c * 65536 )
printBigNum(t2);
BigNumber t3 = t2 + db; // ( c * 65536 + d )
printBigNum(t3);
BigNumber t4 = t3 + eb; // ( c * 65536 + d + e )
printBigNum(t4);
BigNumber t5 = t1 / t4; // ( a * b ) / ( c * 65536 + d + e )
printBigNum(t5);
}
void loop() {
};
//function to display a big number and free it afterwards
//modified println to print
void printBigNum (BigNumber n)
{
char * s = n.toString ();
Serial.println (s);
free (s);
} // end of printBignum
Compiled and run on 1284p so no issue with ram space..
Not sure the BigNumber lib can work with float/int/long/long_long variables as the input to the calculation. Would be great if it could..
:~