Comparing currency values as floats does not work

后端 未结 4 1248
忘了有多久
忘了有多久 2020-12-12 04:54

If I type 0.01 or 0.02 the the program just exits immediately. What do I do?

#include 

char line[100];
float amount;

int main()
{
printf(\"E         


        
4条回答
  •  庸人自扰
    2020-12-12 05:22

    What is the most effective way for float and double comparison?

    You should use something like this

    bool isEqual(double a, double b)
    {
        return fabs(a - b) < EPSILON;
    }
    

    or this

    #define ISEQUAL(a,b) ( fabs((a) - (b)) < EPSILON)
    

    and define EPSILON

    #define EPSILON (0.000001)
    

提交回复
热议问题