c++ floating point precision loss: 3015/0.00025298219406977296

后端 未结 5 1972
陌清茗
陌清茗 2021-02-05 10:26

The problem.

Microsoft Visual C++ 2005 compiler, 32bit windows xp sp3, amd 64 x2 cpu.

Code:

double a = 3015.0; 
double b = 0.00025298219406977296         


        
5条回答
  •  北海茫月
    2021-02-05 11:10

    I'd guess you're printing out the number without specifying a precision. Try this:

    #include 
    #include 
    
    int main() { 
        double a = 3015.0; 
        double b = 0.00025298219406977296;
        double f = a/b;
    
        std::cout << std::fixed << std::setprecision(15) << f << std::endl;
        return 0;
    }
    

    This produces:

    11917834.814763514000000

    Which looks correct to me. I'm using VC++ 2008 instead of 2005, but I'd guess the difference is in your code, not the compiler.

提交回复
热议问题