Demonstrates that Nucleo represents divide by zero result as inf, not an error.
Fork of ArrayLimits by
main.cpp@2:68190b1e8bf3, 2017-10-12 (annotated)
- Committer:
- CSTritt
- Date:
- Thu Oct 12 20:40:08 2017 +0000
- Revision:
- 2:68190b1e8bf3
- Parent:
- 1:a000e0121191
- Child:
- 3:f29617cd39a9
Initial version.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:68190b1e8bf3 | 6 | Demonstrates divide by zero on Nucleo boards. Result is inf, not an error. |
CSTritt | 2:68190b1e8bf3 | 7 | |
CSTritt | 2:68190b1e8bf3 | 8 | Created by Dr. C. S. Tritt; Last revised 10/12/17 (v. 1.0) |
CSTritt | 1:a000e0121191 | 9 | */ |
CSTritt | 1:a000e0121191 | 10 | #include "mbed.h" |
CSTritt | 0:2f9e67d4c561 | 11 | |
CSTritt | 0:2f9e67d4c561 | 12 | int main() |
CSTritt | 0:2f9e67d4c561 | 13 | { |
CSTritt | 2:68190b1e8bf3 | 14 | printf("Hello World !\n\n"); |
CSTritt | 1:a000e0121191 | 15 | |
CSTritt | 2:68190b1e8bf3 | 16 | for (int i = 5; i >= -5; i--) { // Displays inf for 1/0. |
CSTritt | 2:68190b1e8bf3 | 17 | float x = (float) i; |
CSTritt | 2:68190b1e8bf3 | 18 | printf("x is %f and 1/x is %f.\n", x, 1/x); |
CSTritt | 1:a000e0121191 | 19 | wait(0.5f); |
CSTritt | 0:2f9e67d4c561 | 20 | } |
CSTritt | 1:a000e0121191 | 21 | |
CSTritt | 2:68190b1e8bf3 | 22 | float x = 1.0f/0.0f; // Produces compile time warning. |
CSTritt | 2:68190b1e8bf3 | 23 | // Prints three infs. |
CSTritt | 2:68190b1e8bf3 | 24 | printf("\nx, x+1.0 & 2.0*x are: %f, %f, %f.\n", x, x+1.0f, 2.0f*x); |
CSTritt | 1:a000e0121191 | 25 | |
CSTritt | 1:a000e0121191 | 26 | while (true) {}; // Just loop here forever after tests. |
CSTritt | 0:2f9e67d4c561 | 27 | } |