Skip to content

Instantly share code, notes, and snippets.

@ednisley ednisley/BigNumTest.ino
Last active May 1, 2017

Embed
What would you like to do?
Arduino source code: BigNumber library exercise
// BigNumber exercise
#include "BigNumber.h"
//-- Helper routine for printf()
int s_putc(char c, FILE *t) {
Serial.write(c);
}
void setup ()
{
Serial.begin (115200);
fdevopen(&s_putc,0); // set up serial output for printf()
Serial.println ("BigNumber exercise");
Serial.println ("Ed Nisley - KE4ZNU - April 2017");
#define WHOLES 10
#define FRACTS 10
printf("Fraction digits: %d\n",FRACTS);
BigNumber::begin (FRACTS);
char *pBigNumber;
#define BUFFLEN (WHOLES + FRACTS)
char NumString[BUFFLEN];
BigNumber Tenth = "0.1"; // useful constants
BigNumber Half = "0.5";
BigNumber One = 1;
BigNumber Two = 2;
BigNumber ThirtyTwoBits = Two.pow(32);
Serial.println(ThirtyTwoBits);
BigNumber Oscillator = "125000000";
Serial.println(Oscillator);
BigNumber HertzPerCount;
HertzPerCount = Oscillator / ThirtyTwoBits;
Serial.println(HertzPerCount);
BigNumber CountPerHertz;
CountPerHertz = ThirtyTwoBits / Oscillator;
Serial.println(CountPerHertz);
BigNumber TestFreq = "60000";
Serial.println(TestFreq);
BigNumber DeltaPhi;
DeltaPhi = TestFreq * CountPerHertz;
Serial.println(DeltaPhi);
long DeltaPhiL;
DeltaPhiL = DeltaPhi;
printf("Long: %ld\n",DeltaPhiL);
Serial.println("0.1 Hz increment …");
Serial.println(TestFreq + Tenth);
DeltaPhi = (TestFreq + Tenth) * CountPerHertz;
Serial.println(DeltaPhi);
TestFreq = DeltaPhi * HertzPerCount;
Serial.println(TestFreq);
Serial.println("Rounding DeltaPhi up …");
DeltaPhi += Half;
Serial.println(DeltaPhi);
TestFreq = DeltaPhi * HertzPerCount;
Serial.println(TestFreq);
pBigNumber = DeltaPhi.toString();
printf("String: %04x → %s\n",pBigNumber,pBigNumber);
free(pBigNumber);
DeltaPhiL = DeltaPhi;
printf("Unsigned: %ld\n",DeltaPhiL);
pBigNumber = "59999.9";
TestFreq = pBigNumber;
Serial.println(TestFreq);
DeltaPhi = TestFreq * CountPerHertz;
Serial.println(DeltaPhi);
Serial.println("Rounding DeltaPhi up …");
DeltaPhi = TestFreq * CountPerHertz + Half;
Serial.println(DeltaPhi);
DeltaPhiL = DeltaPhi;
int rc = snprintf(NumString,BUFFLEN,"%ld",DeltaPhiL);
if (rc > 0 && rc < BUFFLEN) {
printf("String length: %d\n",rc);
}
else {
printf("Whoops: %d for %ld\n",rc,DeltaPhiL);
strncpy(NumString,"123456789",sizeof(NumString));
NumString[BUFFLEN-1] = 0;
printf(" forced: %s\n",NumString);
}
printf("Back from string [%s]\n",NumString);
DeltaPhi = NumString;
Serial.println(DeltaPhi);
TestFreq = DeltaPhi * HertzPerCount;
Serial.println(TestFreq);
}
void loop () {
}
BigNumber exercise
Ed Nisley - KE4ZNU - April 2017
Fraction digits: 10
4294967296
125000000
0.0291038304
34.3597383680
60000
2061584.3020800000
Long: 2061584
0.1 Hz increment …
60000.1000000000
2061587.7380538368
60000.0998830384
Rounding DeltaPhi up …
2061588.2380538368
60000.1144349536
String: 045e → 2061588.2380538368
Unsigned: 2061588
59999.9
2061580.8661061632
Rounding DeltaPhi up …
2061581.3661061632
String length: 7
Back from string [2061581]
2061581
59999.9037798624
@ednisley

This comment has been minimized.

Copy link
Owner Author

ednisley commented May 1, 2017

More details on my blog at http://wp.me/poZKh-6Iw

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.