Demonstrates that Nucleo represents divide by zero result as inf, not an error.
Fork of ArrayLimits by
Revision 3:f29617cd39a9, committed 2017-10-24
- Comitter:
- CSTritt
- Date:
- Tue Oct 24 17:31:14 2017 +0000
- Parent:
- 2:68190b1e8bf3
- Commit message:
- Added int calculations. Cleaned up comments.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 68190b1e8bf3 -r f29617cd39a9 main.cpp --- a/main.cpp Thu Oct 12 20:40:08 2017 +0000 +++ b/main.cpp Tue Oct 24 17:31:14 2017 +0000 @@ -3,25 +3,33 @@ File: main.cpp Project: DivideByZero - Demonstrates divide by zero on Nucleo boards. Result is inf, not an error. + Demonstrates divide by zero on Nucleo boards. For floats, the result is inf, + not an error. For ints, the result is 0, not an error. These results are for + the default online mbed compiler settings. - Created by Dr. C. S. Tritt; Last revised 10/12/17 (v. 1.0) + Created by Dr. C. S. Tritt; Last revised 10/24/17 (v. 1.1) */ #include "mbed.h" int main() { - printf("Hello World !\n\n"); - - for (int i = 5; i >= -5; i--) { // Displays inf for 1/0. + // Try some float values. + for (int i = 3; i >= -3; i--) { // Displays inf for 1/0. float x = (float) i; printf("x is %f and 1/x is %f.\n", x, 1/x); wait(0.5f); } + // Try some other things with floats. float x = 1.0f/0.0f; // Produces compile time warning. - // Prints three infs. + // The following displays three infs. printf("\nx, x+1.0 & 2.0*x are: %f, %f, %f.\n", x, x+1.0f, 2.0f*x); + printf("\ninf cast to int is %d.\n\n", (int)x); + // Try some int values. + for (int i = 3; i >= -3; i--) { // Displays 0 for 1/0. + printf("i is %d and 1/i is %d.\n", i, 1/i); + wait(0.5f); + } while (true) {}; // Just loop here forever after tests. } \ No newline at end of file