2021年3月23日星期二

Fmod does not work as expected(c programming)

I am using gcc 9.3.0 version and standard settings. In the code in the example, I am getting 7 while waiting to get output 7.14. What could be the reason for this?(im using gcc main.c -lm -o main when compiling,what I'm trying to do is print as an integer if the double number is equal to integer, otherwise print it as a double,sorry for the adjustments, this is the last and proper version)

   #include <stdio.h>      #include<math.h>      double try(double a);              int main()      {          double b=7.14;          double x;          double temp;                   x=try(b);                    if(fmod(x,1)==0.0)//print 7 in this function                  temp=x;                  printf("%d",(int)temp);              }              else if(fmod(x,1)!=0.0 ){                  printf("%f",x);                                }                   return 0;      }  double try(double a){    if(fmod(a,1)!=0.0){              printf("%lf",a);//print 7.14 in this function          }          else if(fmod(a,1)==0.0 ){              printf("%d",(int)a);                        }       return a+0;  }  
https://stackoverflow.com/questions/66773641/fmod-does-not-work-as-expectedc-programming March 24, 2021 at 09:46AM

没有评论:

发表评论