Demonstrates that Nucleo represents divide by zero result as inf, not an error.

Dependencies:   mbed

Fork of ArrayLimits by Charles Tritt

Committer:
CSTritt
Date:
Tue Oct 24 17:31:14 2017 +0000
Revision:
3:f29617cd39a9
Parent:
2:68190b1e8bf3
Added int calculations. Cleaned up comments.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
CSTritt 1:a000e0121191 1
CSTritt 1:a000e0121191 2 /*
CSTritt 1:a000e0121191 3 File: main.cpp
CSTritt 2:68190b1e8bf3 4 Project: DivideByZero
CSTritt 0:2f9e67d4c561 5
CSTritt 3:f29617cd39a9 6 Demonstrates divide by zero on Nucleo boards. For floats, the result is inf,
CSTritt 3:f29617cd39a9 7 not an error. For ints, the result is 0, not an error. These results are for
CSTritt 3:f29617cd39a9 8 the default online mbed compiler settings.
CSTritt 2:68190b1e8bf3 9
CSTritt 3:f29617cd39a9 10 Created by Dr. C. S. Tritt; Last revised 10/24/17 (v. 1.1)
CSTritt 1:a000e0121191 11 */
CSTritt 1:a000e0121191 12 #include "mbed.h"
CSTritt 0:2f9e67d4c561 13
CSTritt 0:2f9e67d4c561 14 int main()
CSTritt 0:2f9e67d4c561 15 {
CSTritt 3:f29617cd39a9 16 // Try some float values.
CSTritt 3:f29617cd39a9 17 for (int i = 3; i >= -3; i--) { // Displays inf for 1/0.
CSTritt 2:68190b1e8bf3 18 float x = (float) i;
CSTritt 2:68190b1e8bf3 19 printf("x is %f and 1/x is %f.\n", x, 1/x);
CSTritt 1:a000e0121191 20 wait(0.5f);
CSTritt 0:2f9e67d4c561 21 }
CSTritt 1:a000e0121191 22
CSTritt 3:f29617cd39a9 23 // Try some other things with floats.
CSTritt 2:68190b1e8bf3 24 float x = 1.0f/0.0f; // Produces compile time warning.
CSTritt 3:f29617cd39a9 25 // The following displays three infs.
CSTritt 2:68190b1e8bf3 26 printf("\nx, x+1.0 & 2.0*x are: %f, %f, %f.\n", x, x+1.0f, 2.0f*x);
CSTritt 3:f29617cd39a9 27 printf("\ninf cast to int is %d.\n\n", (int)x);
CSTritt 1:a000e0121191 28
CSTritt 3:f29617cd39a9 29 // Try some int values.
CSTritt 3:f29617cd39a9 30 for (int i = 3; i >= -3; i--) { // Displays 0 for 1/0.
CSTritt 3:f29617cd39a9 31 printf("i is %d and 1/i is %d.\n", i, 1/i);
CSTritt 3:f29617cd39a9 32 wait(0.5f);
CSTritt 3:f29617cd39a9 33 }
CSTritt 1:a000e0121191 34 while (true) {}; // Just loop here forever after tests.
CSTritt 0:2f9e67d4c561 35 }