I spent the day tracking down a divide by zero error. (I know I am complaining.) I am using a teensy 3.1 on Arduino 1.6.5 and Teensyduino 1.25-Beta2 on Ubuntu.
So this is a combination bug report and enhancement request for Serial.print().
What finally clued me in is that when my error occurred and Serial.print printed out a zero for the number, the cosf(num) was also zero. Finally I started searching and found Serial.print in line 726 of my revisions.txt file on version 1.6.5. This applies to Arduino 1.0.2. It references the following links:
https://code.google.com/p/arduino/issues/detail?id=946
https://code.google.com/p/arduino/issues/detail?id=961
https://code.google.com/p/arduino/issues/detail?id=967
This indicates that NaN should be printed out. To check things out I wrote some code to see how things work.
Which gives the result
Hopefully the Teensy Serial.print function will be able to print out overflow, underflow, exponential notation would be nice, but at least NaN and Inf (though I did not get an inf with this test).
So this is a combination bug report and enhancement request for Serial.print().
What finally clued me in is that when my error occurred and Serial.print printed out a zero for the number, the cosf(num) was also zero. Finally I started searching and found Serial.print in line 726 of my revisions.txt file on version 1.6.5. This applies to Arduino 1.0.2. It references the following links:
https://code.google.com/p/arduino/issues/detail?id=946
https://code.google.com/p/arduino/issues/detail?id=961
https://code.google.com/p/arduino/issues/detail?id=967
This indicates that NaN should be printed out. To check things out I wrote some code to see how things work.
Code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
delay(2000);
float i;
for (i = 0; i <= 40; i++) {
Serial.print(i / 10);
Serial.print("\t");
Serial.print(cosf(i / 10.0));
Serial.println();
}
Serial.println();
Serial.println();
float a = 0.0 / 0.0;
float b = 1 / 0 / 0.0;
float c = 1e-35;
float d = 1e35;
float e = 1e-350;
float f = 1e350;
Serial.print(a);
Serial.print("\t");
Serial.print(cosf(a));
Serial.print("\t");
Serial.print(isinf(a) ? "inf" : "fin");
Serial.print("\t");
Serial.println(isnan(a) ? "nan" : "num");
Serial.print(b);
Serial.print("\t");
Serial.print(cosf(b));
Serial.print("\t");
Serial.print(isinf(b) ? "inf" : "fin");
Serial.print("\t");
Serial.println(isnan(b) ? "nan" : "num");
Serial.print(c);
Serial.print("\t");
Serial.print(cosf(c));
Serial.print("\t");
Serial.print(isinf(c) ? "inf" : "fin");
Serial.print("\t");
Serial.println(isnan(c) ? "nan" : "num");
Serial.print(d);
Serial.print("\t");
Serial.print(cosf(d));
Serial.print("\t");
Serial.print(isinf(d) ? "inf" : "fin");
Serial.print("\t");
Serial.println(isnan(d) ? "nan" : "num");
Serial.print(e);
Serial.print("\t");
Serial.print(cosf(e));
Serial.print("\t");
Serial.print(isinf(e) ? "inf" : "fin");
Serial.print("\t");
Serial.println(isnan(e) ? "nan" : "num");
Serial.print(f);
Serial.print("\t");
Serial.print(cosf(f));
Serial.print("\t");
Serial.print(isinf(f) ? "inf" : "fin");
Serial.print("\t");
Serial.println(isnan(f) ? "nan" : "num");
}
void loop() {
// put your main code here, to run repeatedly:
}
Which gives the result
Code:
0.00 1.00
0.10 1.00
0.20 0.98
0.30 0.96
0.40 0.92
0.00 0.00 fin nan
0.00 0.00 fin nan
0.00 1.00 fin num
4294967295.// -0.04 fin num
0.00 1.00 fin num
4294967295.// 0.00 inf num
Hopefully the Teensy Serial.print function will be able to print out overflow, underflow, exponential notation would be nice, but at least NaN and Inf (though I did not get an inf with this test).
Last edited: