mirror of
https://github.com/nqrduck/ATM.git
synced 2024-11-29 21:33:15 +00:00
50 lines
856 B
Text
50 lines
856 B
Text
|
// BigNumber test: calculate natural logarithm
|
||
|
#include "BigNumber.h"
|
||
|
|
||
|
BigNumber one (1);
|
||
|
BigNumber two (2);
|
||
|
BigNumber half ("0.5");
|
||
|
|
||
|
BigNumber ln (BigNumber x)
|
||
|
{
|
||
|
|
||
|
// if x <= 0.5 or >= 2 take the square root and double the result
|
||
|
if (x <= half || x >= two)
|
||
|
{
|
||
|
BigNumber e = ln (x.sqrt ());
|
||
|
return e * two;
|
||
|
} // end of x <= 0.5 or >= 2
|
||
|
|
||
|
// some big numbers
|
||
|
BigNumber a = (x - one) / (x + one);
|
||
|
BigNumber e;
|
||
|
BigNumber t = a;
|
||
|
|
||
|
BigNumber i = one;
|
||
|
BigNumber E;
|
||
|
|
||
|
do
|
||
|
{
|
||
|
E = e;
|
||
|
e += t / i;
|
||
|
t *= a * a;
|
||
|
i += two;
|
||
|
} while (e != E);
|
||
|
|
||
|
return e * two;
|
||
|
} // end of function ln
|
||
|
|
||
|
void setup ()
|
||
|
{
|
||
|
Serial.begin (115200);
|
||
|
Serial.println ();
|
||
|
Serial.println ();
|
||
|
BigNumber::begin (45); // max around 45 on the Uno for ln (100)
|
||
|
|
||
|
Serial.println (ln (100));
|
||
|
|
||
|
} // end of setup
|
||
|
|
||
|
void loop () { }
|
||
|
|