当前位置:网站首页>Double to int precision loss

Double to int precision loss

2022-07-06 11:25:00 %xiao Q

Let's first look at the phenomenon of precision loss :

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    
   double a = 74.46;
   int b = a * 100;

   cout << "a: " << a << " b: " << b <<endl;
   return 0;
}

result : Insert picture description here

solve : Use rounding method

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    
   double a = 74.46;
   int b = round(a * 100);

   cout << "a: " << a << " b: " << b << endl;
   return 0;
}

result :
 Insert picture description here

原网站

版权声明
本文为[%xiao Q]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060912470028.html