#include <BigNumber.h>
BigNumber pi;
const byte precision = 20;
// function to display a big number and free it afterwards
void printBignum (BigNumber n)
{
char * s = n.toString ();
Serial.print (s);
free (s);
} // end of printBignum
BigNumber sine (BigNumber x, BigNumber p)
{
BigNumber one = 1;
BigNumber two = 2;
BigNumber val = one;
while (p > 0)
{
val = one - val * x * x / (two * p) / (two * p + one);
p = p - one;
}
val = x * val;
return val;
} // end of sine
BigNumber cosine (BigNumber x, BigNumber p)
{
BigNumber sin = sine (x, p);
BigNumber sinSquared = sin * sin;
BigNumber oneMinusSine = BigNumber (1) - sinSquared;
return oneMinusSine.sqrt ();
} // end of cosine
BigNumber tangent (BigNumber x, BigNumber p)
{
return (sine (x, p) / cosine (x, p));
} // end of tangent
void setup ()
{
BigNumber::begin (); // initialize library
pi = BigNumber ("3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706");
Serial.begin (115200);
Serial.println ();
Serial.println ("Starting ...");
BigNumber::setScale (20);
for (BigNumber foo = "0.1"; foo <= pi / BigNumber (2); foo += BigNumber ("0.1"))
{
Serial.print ("Sine of ");
printBignum (foo);
Serial.print (" = ");
printBignum (sine (foo, precision));
Serial.println ();
} // end of for
for (BigNumber foo = "0.1"; foo <= pi / BigNumber (2); foo += BigNumber ("0.1"))
{
Serial.print ("Cosine of ");
printBignum (foo);
Serial.print (" = ");
printBignum (cosine (foo, precision));
Serial.println ();
} // end of for
for (BigNumber foo = "0.1"; foo < pi / BigNumber (2); foo += BigNumber ("0.1"))
{
Serial.print ("Tangent of ");
printBignum (foo);
Serial.print (" = ");
printBignum (tangent (foo, precision));
Serial.println ();
} // end of for
} // end of setup
void loop () {}
Output:
Starting ...
Sine of 0.1 = 0.09983341664682815230
Sine of 0.20000000000000000000 = 0.19866933079506121546
Sine of 0.30000000000000000000 = 0.29552020666133957510
Sine of 0.40000000000000000000 = 0.38941834230865049166
Sine of 0.50000000000000000000 = 0.47942553860420300027
Sine of 0.60000000000000000000 = 0.56464247339503535720
Sine of 0.70000000000000000000 = 0.64421768723769105367
Sine of 0.80000000000000000000 = 0.71735609089952276163
Sine of 0.90000000000000000000 = 0.78332690962748338846
Sine of 1.00000000000000000000 = 0.84147098480789650666
Sine of 1.10000000000000000000 = 0.89120736006143533995
Sine of 1.20000000000000000000 = 0.93203908596722634967
Sine of 1.30000000000000000000 = 0.96355818541719296470
Sine of 1.40000000000000000000 = 0.98544972998846018066
Sine of 1.50000000000000000000 = 0.99749498660405443095
Cosine of 0.1 = 0.99500416527802576609
Cosine of 0.20000000000000000000 = 0.98006657784124163112
Cosine of 0.30000000000000000000 = 0.95533648912560601964
Cosine of 0.40000000000000000000 = 0.92106099400288508280
Cosine of 0.50000000000000000000 = 0.87758256189037271612
Cosine of 0.60000000000000000000 = 0.82533561490967829724
Cosine of 0.70000000000000000000 = 0.76484218728448842626
Cosine of 0.80000000000000000000 = 0.69670670934716542092
Cosine of 0.90000000000000000000 = 0.62160996827066445649
Cosine of 1.00000000000000000000 = 0.54030230586813971739
Cosine of 1.10000000000000000000 = 0.45359612142557738778
Cosine of 1.20000000000000000000 = 0.36235775447667357764
Cosine of 1.30000000000000000000 = 0.26749882862458740701
Cosine of 1.40000000000000000000 = 0.16996714290024093862
Cosine of 1.50000000000000000000 = 0.07073720166770290998
Tangent of 0.1 = 0.10033467208545054505
Tangent of 0.20000000000000000000 = 0.20271003550867248332
Tangent of 0.30000000000000000000 = 0.30933624960962323303
Tangent of 0.40000000000000000000 = 0.42279321873816176197
Tangent of 0.50000000000000000000 = 0.54630248984379051324
Tangent of 0.60000000000000000000 = 0.68413680834169231707
Tangent of 0.70000000000000000000 = 0.84228838046307944812
Tangent of 0.80000000000000000000 = 1.02963855705036401275
Tangent of 0.90000000000000000000 = 1.26015821755033913712
Tangent of 1.00000000000000000000 = 1.55740772465490223055
Tangent of 1.10000000000000000000 = 1.96475965724865195088
Tangent of 1.20000000000000000000 = 2.57215162212631893539
Tangent of 1.30000000000000000000 = 3.60210244796797815106
Tangent of 1.40000000000000000000 = 5.79788371548288964359