Hi all,
Recently I've been working on a project that handles numbers composed of 16 32-bit (uint32_t) words. One of the requirements of the project is to be able to compare two such numbers to each other. To check if one number (a) is greater than or equal to another number (L), I wrote up the following quick and dirty test function:
bool greaterThanOrEqualTo(uint32_t* a) {
unsigned short i = 0;
while((a[i] >= L[i]) && (i < 16)) {
i += 1;
}
Serial.println(i);
if(i == 16) {
Serial.println("true");
return true;
}
Serial.println("false");
return false;
}
It should be noted that the variable L is hard-coded elsewhere. At the end of this post is a copy of the entire .ino test file, as well as the serial print-out, and a corrected, working version of the above function for your reference.
Here's the rub: when this function is run with a = L, the serial print-out of i is 16. But, if(i == 16) fails, i.e. greaterThanOrEqualTo returns false when it should return true.
I don't know why this function does not work, but maybe one of you do, and I would love to hear why if so. I have a sneaking suspicion it might have something to do either with memory and / or with working within the Arduino environment directly. I suspect these for the following reasons:
-
If
(a[i] >= L[i])and(i < 16)are swapped within the while loop such that the while loop is:while((i < 16) && (a[i] >= L[i])), the function, interestingly, returnstrue. This suggests that ifa[16]andL[16]are read (which is undefined becauseaandLare arrays of only size 16), somehow, this disrupts the later inequalityif(i == 16). -
I've had this code converted to C and executed outside of the Arduino IDE, and it ran as intended.
For your reference I am working on an Arduino Nano 33 IoT, and am using version 1.8.13 of the IDE. I might update it to the most recent version and see if that solves my issue, but I don't believe the issue lies there.
Here is a complete copy of the .ino file that I am working from:
#include "Arduino.h"
#include "HardwareSerial.h"
#include <stdint.h>
const uint32_t L[16] = {0x00001000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000014de, 0x0000f9de, 0x0000a2f7, 0x00009cd6, 0x00005812, 0x0000631a, 0x00005cf5, 0x0000d3ed};
bool greaterThanOrEqualTo(uint32_t* a) { // WARNING: BROKEN!
unsigned short i = 0;
while((a[i] >= L[i]) && (i < 16)) {
i += 1;
}
Serial.println(i);
if(i == 16) {
Serial.println("true");
return true;
}
Serial.println("false");
return false;
}
void setup() {
Serial.begin(9600);
while(!Serial) {
delay(250);
}
uint32_t a[16] = {0x00001000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x00000000, 0x000014de, 0x0000f9de, 0x0000a2f7, 0x00009cd6, 0x00005812, 0x0000631a, 0x00005cf5, 0x0000d3ed};
if(greaterThanOrEqualTo(a)) {
Serial.println("true");
} else {
Serial.println("false");
}
}
void loop() {}
When the above .ino file is executed, the following serial print-out is given:
16
false
false
Here is a corrected, working version of the function:
bool greaterThanOrEqualTo(uint32_t* a) {
unsigned short i = 0;
while((i < 16) && (a[i] >= L[i])) {
i += 1;
}
Serial.println(i);
if(i == 16) {
Serial.println("true");
return true;
}
Serial.println("false");
return false;
}
Thanks!