Handle infinity and NaN in printf

This commit is contained in:
Peter Limkilde Svendsen
2019-12-05 01:37:17 +00:00
committed by Jeremy Soller
parent 8ba70792e9
commit 74555698fb
3 changed files with 102 additions and 26 deletions
+13
View File
@@ -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");
}
}