fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <math.h>
  4.  
  5. int Sign(double N){
  6. return N >= 0 ? 1 : -1;
  7. }
  8.  
  9. double MakeHoge(double A,double B){
  10. int F = Sign(A);
  11. double AA = std::abs(A);
  12.  
  13. double M = fmod(AA, B);
  14. double N = (AA - M) / B;
  15.  
  16. if (B / 2 <= M)N++;
  17.  
  18. return F*N*B;
  19.  
  20.  
  21. }
  22.  
  23.  
  24. int main(){
  25. double A = 0;
  26. double B = 0;
  27.  
  28. A = 123, B = 12;
  29. std::cout <<"A="<<A<<','<<"B="<<B<<" -> "<< MakeHoge(A, B) << std::endl;
  30. A = 126, B = 12;
  31. std::cout <<"A="<<A<<','<<"B="<<B<<" -> "<< MakeHoge(A, B) << std::endl;
  32. A = -123, B = 12;
  33. std::cout <<"A="<<A<<','<<"B="<<B<<" -> "<< MakeHoge(A, B) << std::endl;
  34. A = -126, B = 12;
  35. std::cout <<"A="<<A<<','<<"B="<<B<<" -> "<< MakeHoge(A, B) << std::endl;
  36. A = 1.234, B = 0.01;
  37. std::cout <<"A="<<A<<','<<"B="<<B<<" -> "<< MakeHoge(A, B) << std::endl;
  38. A = 1.235, B = 0.01;
  39. std::cout <<"A="<<A<<','<<"B="<<B<<" -> "<< MakeHoge(A, B) << std::endl;
  40.  
  41. return 0;
  42. }
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
A=123,B=12 -> 120
A=126,B=12 -> 132
A=-123,B=12 -> -120
A=-126,B=12 -> -132
A=1.234,B=0.01 -> 1.23
A=1.235,B=0.01 -> 1.24