My floating point number has extra digits when I print it

后端 未结 2 700
深忆病人
深忆病人 2020-11-30 15:50

I define a floating point number as float transparency = 0.85f; And in the next line, I pass it to a function -- fcn_name(transparency) -- but it t

2条回答
  •  春和景丽
    2020-11-30 16:10

    The only way I can think of solving this problem is to pass characteristic and mantissa to the function separately and let IT work on setting the values appropriately.

    Also if you want more precision,

    http://www.drdobbs.com/cpp/fixed-point-arithmetic-types-for-c/184401992 is the article I know. Though this works for C++ only. (Searching for an equivalent C implementation).

    I tried this on VS2010,

    #include 
    void printfloat(float f)
    {
        printf("%f",f);
    }
    
    int main(int argc, char *argv[])
    {
        float f = 0.24f;
        printfloat(f);
        return 0;
    }
    
    OUTPUT: 0.240000
    

提交回复
热议问题