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


im running issues floating point arithmetic not being accurate. i'm trying calculate score based on weighted formula every input variable weighs as 20 times next significant one. inputs real numbers, ended using double store result. code below has problem of losing difference between e1 , e2.

this code performance sensitive, need find efficient answer problem. thought of multiplying inputs hundred , using int (since precise enough think), doubt best solution, hence question.

#include <iostream>  int main() {     double score1, score2;     float  = 2.75 ;     float b  = 5.25 ;     float c  = 5.25 ;     float d  = 2.75 ;     float e1 = 3    ;     float e2 = 6    ;      score1 = 20 * b - 1 * + 0.05 * d  /* - 0.0025 * c*/ + 0.0001 * e1 ;     score2 = 20 * b - 1 * + 0.05 * d  /* - 0.0025 * c*/ + 0.0001 * e2 ;      std::cout << score1 << std::endl;     std::cout << score2 << std::endl;      std::cin.get();     return 0; }  //ouputs: //102.388 //102.388 

  1. you not outputting entire value, use cout << setprecision(number_of_digits) << score1 << endl;
  2. how many valid digits need in score computation?

Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -