'error ovf with double math arduino teensy
I'm trying to count the number of digits in a double. Maybe I am going about this wrong, but I don't see what's wrong.When I have three decimals it counts correctly but at first it only displays two, so there is a difference between whats actually happening and what is being displayed. At 4 decimals I hit some large loop which ovf the numbers??? Maybe if there is a better way of counting digits, but idk how to avoid errors with double math? I just need the num digits, how doesn't matter.
My code basically just has this:
void setup() {
Serial.begin(115200);
Serial.println("setup MASTER");
Wire.begin();
}
void loop() {
Serial.println("");
Serial.println("&&&&&&&&&&&&&&&master&&&&&&&&&&&&&&&&&&&&&");
Serial.println("");
Serial.println(doubleDigitCount(290));
delay(1000);
Serial.println("/////////////////////");
Serial.println(doubleDigitCount(290.0));
delay(1000);
Serial.println("/////////////////////");
Serial.println(doubleDigitCount(290.1));
delay(1000);
Serial.println("/////////////////////");
Serial.println(doubleDigitCount(290.22));
delay(1000);
Serial.println("/////////////////////");
Serial.println(doubleDigitCount(290.333));
delay(1000);
Serial.println("/////////////////////");
Serial.println(doubleDigitCount(290.4444));
delay(1000);
Serial.println("/////////////////////");
} and then this is the function that counts digits:
int doubleDigitCount(double data){
int count = 0;
double whole = floor(data) + 0.0;
double remainder = data - whole + 0.0;
Serial.print("1data: "); Serial.println(data + 0.0);
Serial.print("1whole: "); Serial.println(whole);
Serial.print("1remainder: "); Serial.println(remainder + 0.0);
while(remainder != (double(0) + 0.0)){
if(remainder <= .01){//the difference should always have a number in 10s decimal, and if its .011 then its larger. Too bad if the data is exactly .01 is duration = 10.01, but
break;
}
data *= 10;
whole = floor(data) + 0.0;
Serial.print("L data: ");Serial.println(data);
Serial.print("L whole: "); Serial.println(whole);
remainder = data-whole + 0.0;
Serial.print("diff"); Serial.println(remainder);
count++;
}
return count;
}
When I run it, I get the following print outs(it's long, but ill highlight specific sections):
what can I do about this? Or is there another way to count digits in a double?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
