How to get around rounding issues in floating point arithmetic in C++?

倖福魔咒の 提交于 2019-12-06 11:51:32
  1. you are not outputting the entire value, use cout << setprecision(number_of_digits) << score1 << endl;
  2. how many valid digits do you need in your score computation?

I thought of multiplying my inputs by a hundred and then using an int (since that would be precise enough I think), but I doubt that is the best solution

Given the values you've shown, I would say it is.

http://ideone.com/qqTB3 shows you that the difference is not lost, but actually as big as you'd expect (up to floating point accuracy, which is 15 decimal digits for double).

Lets see what is happening in this code:

score1 = 20 * b - 1 * a + 0.05 * d  /* - 0.0025 * c*/ + 0.0001 * E1 ;

// Multiplication division happens first:

float  tmp1 = static_cast<float>(20) * b;      // 20 cast to float.
float  tmp2 = static_cast<float>(1)  * a;      // 1  cast to float.
double tmp3 = 0.05   * static_cast<double>(d); // d converted to double as 0.05 is double
double tmp4 = 0.0001 * static_cast<double>(E1);// E1 cast to double as 0.0001 is double

// Addition and subtraction now happen
float  tmp5  = tmp1 - tmp2;
double tmp6  = static_cast<double>(tmp5) + tmp3; // tmp5 cast to double as tmp3 is a double.
double tmp7  = tmp6 + tmp4;
score1       = tmp7;

If we do this in our heads:

tmp1 = 105.0
tmp2 =   2.75
tmp3 =   0.1375
tmp4 =   0.0003
tmp5 = 107.75
tmp6 = 107.8875
tmp7 = 107.8878

The precision should hold for those values:
But when you print out the default precision for doubles is 3 decimal places.

std::cout << 107.8878;
> 107.888

So set the precision:

std::cout << std::setprecision(15) << 107.8878 << "\n";
> 107.8878
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!