gcc中的无穷大值

时间:2017-06-01 18:56:00

标签: c gcc nan

当GNU编译器在无限值上打印inf时,我可以将其更改为不同的输出吗?

#include<stdio.h>
{
   float runs = 40.0;
   float wickets = 0.0;

   printf("Average = %.2f(/*This is the place where I want to print NA 
   instead of inf*/)", runs / wickets);

   return 0;
}

1 个答案:

答案 0 :(得分:4)

您无法更改无限值的打印方式。但是你可以检查它并采取相应的行动。

double avg = runs / wickets;
if (isinf(avg)) {
    printf("Average = NA\n");
} else {
    printf("Average = %.2f\n", avg);
}