currently I'm working with
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
double RangeFinder::getRange(void) {
return fRand(0, 1700);
};
char range[12];
double r = rangeFinder.getRange();
snprintf(range, 11, "range: %d", r);
But it isn't giving me between 0,1700 its giving me between -99,999 ( I think ). I have a feeling the msb is interpretting the thousands place as negative.
What's the fix?
double fRand(double fMin, double fMax)
{
double f = (double)rand() / RAND_MAX;
return fMin + f * (fMax - fMin);
}
double RangeFinder::getRange(void) {
return fRand(0, 1700);
};
char range[22];
int r = rangeFinder.getRange();
snprintf(range, 22, "range: %i ", r);
LCDString(range);
the 2nd arg to snprintf was 11.. too short. there weren't white characters after the number to clear out previous values, and the double r needed to be cast int r.
A possible problem is that the "%d" is used for integers. In order to print float/doubles you should use "%f"(floats) or "%lf"(doubles or long float).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With