OE7WPA
October 11, 2019, 9:41am
1
Hello, i am quite new to arduino programming and i am using IDE 1.8.10 on a Windows 10 computer with an ArduinoDue (64 bits). The Proglem is: I cant add two unsigned long long variables!
My Program:
unsigned long long F_OUT = 100000000 ;
int Counter = 0 ;
void setup() {
Serial.begin(115200);
}
void loop() {
int i;
unsigned long long FrequencyCorrection = F_OUT;
if (Counter == 0 ) {
for (i=1; i<=32; i++) {
FrequencyCorrection = F_OUT / 1000000 * 8;
unsigned long long F_OUT_CORRECTED = F_OUT + FrequencyCorrection;
Serial.print((float)F_OUT); //Conversion to float, because Serial.print() cant print out unsigned long long...
Serial.print(" + ");
Serial.print((float)FrequencyCorrection);
Serial.print(" = ");
Serial.println((float)F_OUT_CORRECTED);
F_OUT += 100000000;
delay(3000);
}
Counter++;
}
}
The results, monitored over te serial Interface, things get a bit funny @ F_OUT=700000000.00: 700000000.00 + 5600.00 = 700005632.00
All Results, printed over the serial Interface:
100000000.00 + 800.00 = 100000800.00
200000000.00 + 1600.00 = 200001600.00
300000000.00 + 2400.00 = 300002400.00
400000000.00 + 3200.00 = 400003200.00
500000000.00 + 4000.00 = 500004000.00
600000000.00 + 4800.00 = 600004800.00
700000000.00 + 5600.00 = 700005632.00
800000000.00 + 6400.00 = 800006400.00
900000000.00 + 7200.00 = 900007168.00
1000000000.00 + 8000.00 = 1000008000.00
1100000000.00 + 8800.00 = 1100008832.00
1200000000.00 + 9600.00 = 1200009600.00
1300000000.00 + 10400.00 = 1300010368.00
1400000000.00 + 11200.00 = 1400011264.00
1500000000.00 + 12000.00 = 1500012032.00
1600000000.00 + 12800.00 = 1600012800.00
1700000000.00 + 13600.00 = 1700013568.00
1800000000.00 + 14400.00 = 1800014336.00
1900000000.00 + 15200.00 = 1900015232.00
2000000000.00 + 16000.00 = 2000016000.00
2100000000.00 + 16800.00 = 2100016768.00
2200000000.00 + 17600.00 = 2200017664.00
2300000000.00 + 18400.00 = 2300018432.00
2400000000.00 + 19200.00 = 2400019200.00
2500000000.00 + 20000.00 = 2500019968.00
2600000000.00 + 20800.00 = 2600020736.00
2700000000.00 + 21600.00 = 2700021504.00
2800000000.00 + 22400.00 = 2800022528.00
2900000000.00 + 23200.00 = 2900023296.00
3000000000.00 + 24000.00 = 3000024064.00
3100000000.00 + 24800.00 = 3100024832.00
3200000000.00 + 25600.00 = 3200025600.00
Does anybody now a solution to this Problem, iam quite confused, obviouslyi cant add two simple unsigned long long variables...
This is only a simple sketch i made show my problems
With best regards
Werner Pichl
You only get about 6 digits of precision in a float.
EDIT (because I just remembered) - On the due you can use double (8-byte floating point type).
lesept
October 11, 2019, 11:43am
3
Nick Gammon's Big Number library may provide an answer, to the cost or more memory usage and slower calculation.
OE7WPA
October 11, 2019, 11:47am
4
Thank you!
This seems to work for me!
I exchanged all variables to double!:
double F_OUT = 100000000 ;
int Counter = 0 ;
void setup() {
Serial.begin(115200);
}
void loop() {
int i;
double FrequencyCorrection = F_OUT;
if (Counter == 0 ) {
for (i=1; i<=32; i++) {
FrequencyCorrection = F_OUT / 1000000 * 8;
double F_OUT_CORRECTED = F_OUT + FrequencyCorrection;
Serial.print((double)F_OUT); //Conversion to float, because Serial.print() cant print out unsigned long long...
Serial.print(" + ");
Serial.print((double)FrequencyCorrection);
Serial.print(" = ");
Serial.println((double)F_OUT_CORRECTED);
F_OUT += 100000000;
delay(3000);
}
Counter++;
}
}
Output now as desired:
100000000.00 + 800.00 = 100000800.00
200000000.00 + 1600.00 = 200001600.00
300000000.00 + 2400.00 = 300002400.00
400000000.00 + 3200.00 = 400003200.00
500000000.00 + 4000.00 = 500004000.00
600000000.00 + 4800.00 = 600004800.00
700000000.00 + 5600.00 = 700005600.00
800000000.00 + 6400.00 = 800006400.00
900000000.00 + 7200.00 = 900007200.00
1000000000.00 + 8000.00 = 1000008000.00
1100000000.00 + 8800.00 = 1100008800.00
1200000000.00 + 9600.00 = 1200009600.00
1300000000.00 + 10400.00 = 1300010400.00
1400000000.00 + 11200.00 = 1400011200.00
1500000000.00 + 12000.00 = 1500012000.00
1600000000.00 + 12800.00 = 1600012800.00
1700000000.00 + 13600.00 = 1700013600.00
1800000000.00 + 14400.00 = 1800014400.00
1900000000.00 + 15200.00 = 1900015200.00
2000000000.00 + 16000.00 = 2000016000.00
2100000000.00 + 16800.00 = 2100016800.00
2200000000.00 + 17600.00 = 2200017600.00
2300000000.00 + 18400.00 = 2300018400.00
2400000000.00 + 19200.00 = 2400019200.00
2500000000.00 + 20000.00 = 2500020000.00
2600000000.00 + 20800.00 = 2600020800.00
2700000000.00 + 21600.00 = 2700021600.00
2800000000.00 + 22400.00 = 2800022400.00
2900000000.00 + 23200.00 = 2900023200.00
3000000000.00 + 24000.00 = 3000024000.00
3100000000.00 + 24800.00 = 3100024800.00
3200000000.00 + 25600.00 = 3200025600.00
OE7WPA:
Thank you!
This seems to work for me!
I exchanged all variables to double!:
Glad you're working. But I meant for you to just use double to get around the problem with Serial.print, like this:
unsigned long long F_OUT = 100000000 ;
int Counter = 0 ;
void setup() {
Serial.begin(115200);
}
void loop() {
int i;
unsigned long long FrequencyCorrection = F_OUT;
if (Counter == 0 ) {
for (i=1; i<=32; i++) {
FrequencyCorrection = F_OUT / 1000000 * 8;
unsigned long long F_OUT_CORRECTED = F_OUT + FrequencyCorrection;
Serial.print((float)F_OUT); //Conversion to double, because Serial.print() cant print out unsigned long long...
Serial.print(" + ");
Serial.print((double)FrequencyCorrection);
Serial.print(" = ");
Serial.println((double)F_OUT_CORRECTED);
F_OUT += 100000000;
delay(3000);
}
Counter++;
}
}
OE7WPA
October 11, 2019, 12:16pm
6
using double instead of unsigned long long solved the problem for me, now my program is working as it should...
Working on a programmable Synthesizer that outputs 20 to 3200 MHz with programmable amplitude and frequency. The PLL in this Project puts out Frequencies that are 6-8 ppm off the desired Frequencies, so i had to implement some sort of correction at certain frequencies!
Thanks again, it is now working like a charm!!
With best regards
Werner