Multiplication of three numbers in c give a wrong results?

前端 未结 2 1434
小蘑菇
小蘑菇 2020-12-22 14:02

I can\'t belive what\'s happen in my program

double den = 180*3600*10000 ;

in debugging a got this value -2109934592.0000000

any he

2条回答
  •  不思量自难忘°
    2020-12-22 14:44

    With the full code in the question we can now see it's an integer overflow.

    10000 * 180 * 3600 = 6,480,000,000.
    

    This is greater than 2,147,483,648 which is the max value of a 32-bit signed int. The results of the multiplication overflows to -2,109,934,592 and is then converted to double.

    To get the right result make one of the numbers a double before you do the multiplication:

    10000.0 * 180 * 3600
    

提交回复
热议问题