fork download
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. std::pair<double, double> div1(int num, double denom){
  5. double whole = std::floor(num / denom);
  6. double remain = std::fmod(num, denom);
  7. return {whole, remain};
  8. }
  9. std::pair<double, double> div2(int num, double denom){
  10. double floatdiv = num / denom;
  11. double whole;
  12. double remain = std::modf(floatdiv, &whole);
  13. return {whole, remain * denom};
  14. }
  15. std::pair<double, double> div3(int num, double denom){
  16. double whole = std::round(num / denom);
  17. double remain = std::remainder(num, denom);
  18. if(remain < 0){
  19. whole--;
  20. remain += denom;
  21. }
  22. return {whole, remain};
  23. }
  24. std::pair<double, double> div4( int num, double denom){
  25. int quo;
  26. auto rem = std::remquo(num, denom, &quo );
  27. return {quo, rem};
  28. }
  29. std::pair<double, double> div5(int num, double denom){
  30. double whole = std::floor(num / denom);
  31. int n = trunc(num / denom) ;
  32. double remain = num - n * denom ;
  33. return {whole, remain};
  34. }
  35. std::pair<double, double> div6(int num, double denom)
  36. {
  37. double whole = std::floor(num / denom);
  38. double remain = std::fma(whole, -denom, num);
  39. if (remain < 0)
  40. {
  41. --whole;
  42. remain = std::fma(whole, -denom, num);
  43. }
  44. return {whole, remain};
  45. }
  46. int main() {
  47. double denom = 100.0 / 6;
  48. int divtype = 0;
  49. for(auto div: {div1, div2, div3, div4, div5, div6}){
  50. std::cerr << "== Using div" << ++divtype << " for this run ==\n";
  51. for(int i = 49; i <= 51; ++i){
  52. auto res = div(i, denom);
  53. std::cerr << i << ": " << res.first << ", " << res.second << " = " << res.first * denom + res.second << "\n";
  54. }
  55. auto oldprec = std::cerr.precision(64);
  56. auto res = div(50, denom);
  57. std::cerr << 50 << ": " << res.first << ", " << res.second << " = " << res.first << " * " << denom << " + " << res.second << " = " << std::floor(res.first) * denom + res.second << "\n";
  58. std::cerr.precision(oldprec);
  59. std::cerr << "\n";
  60. }
  61. return 0;
  62. }
  63.  
Success #stdin #stdout #stderr 0s 15240KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
== Using div1 for this run ==
49: 2, 15.6667 = 49
50: 3, 16.6667 = 66.6667
51: 3, 1 = 51
50: 3, 16.66666666666666429819088079966604709625244140625 = 3 * 16.666666666666667850904559600166976451873779296875 + 16.66666666666666429819088079966604709625244140625 = 66.666666666666657192763523198664188385009765625

== Using div2 for this run ==
49: 2, 15.6667 = 49
50: 3, 0 = 50
51: 3, 1 = 51
50: 3, 0 = 3 * 16.666666666666667850904559600166976451873779296875 + 0 = 50

== Using div3 for this run ==
49: 2, 15.6667 = 49
50: 2, 16.6667 = 50
51: 3, 1 = 51
50: 2, 16.66666666666666429819088079966604709625244140625 = 2 * 16.666666666666667850904559600166976451873779296875 + 16.66666666666666429819088079966604709625244140625 = 50

== Using div4 for this run ==
49: 3, -1 = 49
50: 3, -3.55271e-15 = 50
51: 3, 1 = 51
50: 3, -3.552713678800500929355621337890625e-15 = 3 * 16.666666666666667850904559600166976451873779296875 + -3.552713678800500929355621337890625e-15 = 50

== Using div5 for this run ==
49: 2, 15.6667 = 49
50: 3, 0 = 50
51: 3, 1 = 51
50: 3, 0 = 3 * 16.666666666666667850904559600166976451873779296875 + 0 = 50

== Using div6 for this run ==
49: 2, 15.6667 = 49
50: 2, 16.6667 = 50
51: 3, 1 = 51
50: 2, 16.66666666666666429819088079966604709625244140625 = 2 * 16.666666666666667850904559600166976451873779296875 + 16.66666666666666429819088079966604709625244140625 = 50