Handle infinity and NaN in printf
This commit is contained in:
committed by
Jeremy Soller
parent
8ba70792e9
commit
74555698fb
@@ -1,4 +1,5 @@
|
||||
#include <stdio.h>
|
||||
#include <math.h> // INFINITY, NAN constants
|
||||
|
||||
int main(void) {
|
||||
int sofar = 0;
|
||||
@@ -61,4 +62,16 @@ int main(void) {
|
||||
printf("%G\n", 0.0001);
|
||||
printf("%G\n", 0.00001);
|
||||
printf("%E\n", 0.00001);
|
||||
|
||||
double nonfinites[] = {INFINITY, -INFINITY, NAN, -NAN};
|
||||
char *float_formats[] = {"%e", "%E", "%f", "%F", "%g", "%G"};
|
||||
puts("\nNon-finite float madness:");
|
||||
for (size_t i = 0; i < sizeof(float_formats)/sizeof(char *); i++) {
|
||||
printf("%s:", float_formats[i]);
|
||||
for (size_t j = 0; j < sizeof(nonfinites)/sizeof(double); j++) {
|
||||
printf(" ");
|
||||
printf(float_formats[i], nonfinites[j]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user