fork download
  1. // 固定小数をクラスにしてみた。
  2. #include <iostream>
  3. #include <cmath>
  4. #include <cassert>
  5. #include <limits>
  6.  
  7. #ifdef NDEBUG
  8. #define check(exp)
  9. #else
  10. #define check(exp) if (!(exp)) { \
  11.   std::cerr << __FILE__ << "(" << __LINE__ << "): " << \
  12.   #exp << std::endl; \
  13.   }
  14. #endif
  15.  
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // fixed<t_shift>
  18. ///////////////////////////////////////////////////////////////////////////////
  19.  
  20. template <unsigned int t_shift>
  21. struct fixed {
  22. int m_value;
  23.  
  24. fixed() {
  25. }
  26. fixed(const fixed<t_shift>& f) {
  27. m_value = f.m_value;
  28. }
  29. fixed(int i) {
  30. m_value = i << t_shift;
  31. }
  32. fixed(float f) {
  33. m_value = (int)(f * (1 << t_shift));
  34. }
  35. ~fixed() { }
  36.  
  37. bool operator!() const {
  38. return m_value == 0;
  39. }
  40.  
  41. int get_mask() const {
  42. return ((1 << t_shift) - 1);
  43. }
  44.  
  45. int get_int_part() const {
  46. return m_value >> t_shift;
  47. }
  48. void set_int_part(int i) {
  49. m_value = (i << t_shift) | (m_value & get_mask());
  50. }
  51.  
  52. int get_decimal_part() const {
  53. return m_value & get_mask();
  54. }
  55. void set_decimal_part(int part) const {
  56. const int mask = get_mask();
  57. m_value = (m_value & ~mask) | (part & mask);
  58. }
  59.  
  60. float get_float() const {
  61. return (float)m_value / (float)(1 << t_shift);
  62. }
  63. void set_float(float f) {
  64. m_value = (int)(f * (1 << t_shift));
  65. }
  66.  
  67. bool operator==(const fixed<t_shift>& f) const {
  68. return m_value == f.m_value;
  69. }
  70. bool operator!=(const fixed<t_shift>& f) const {
  71. return m_value != f.m_value;
  72. }
  73. bool operator<=(const fixed<t_shift>& f) const {
  74. return m_value <= f.m_value;
  75. }
  76. bool operator>=(const fixed<t_shift>& f) const {
  77. return m_value >= f.m_value;
  78. }
  79. bool operator<(const fixed<t_shift>& f) const {
  80. return m_value < f.m_value;
  81. }
  82. bool operator>(const fixed<t_shift>& f) const {
  83. return m_value > f.m_value;
  84. }
  85.  
  86. bool operator==(int i) const {
  87. return m_value == (i << t_shift);
  88. }
  89. bool operator!=(int i) const {
  90. return m_value != (i << t_shift);
  91. }
  92. bool operator<=(int i) const {
  93. return m_value <= (i << t_shift);
  94. }
  95. bool operator>=(int i) const {
  96. return m_value >= (i << t_shift);
  97. }
  98. bool operator<(int i) const {
  99. return m_value < (i << t_shift);
  100. }
  101. bool operator>(int i) const {
  102. return m_value > (i << t_shift);
  103. }
  104.  
  105. bool operator==(float f) const {
  106. return m_value == (int)(f * (1 << t_shift));
  107. }
  108. bool operator!=(float f) const {
  109. return m_value != (int)(f * (1 << t_shift));
  110. }
  111. bool operator<=(float f) const {
  112. return m_value <= (int)(f * (1 << t_shift));
  113. }
  114. bool operator>=(float f) const {
  115. return m_value >= (int)(f * (1 << t_shift));
  116. }
  117. bool operator<(float f) const {
  118. return m_value < (int)(f * (1 << t_shift));
  119. }
  120. bool operator>(float f) const {
  121. return m_value > (int)(f * (1 << t_shift));
  122. }
  123.  
  124. fixed<t_shift>& operator=(const fixed<t_shift>& f) {
  125. m_value = f.m_value;
  126. return *this;
  127. }
  128. fixed<t_shift>& operator=(int i) {
  129. m_value = i << t_shift;
  130. return *this;
  131. }
  132. fixed<t_shift>& operator=(float f) {
  133. m_value = (int)(f * (1 << t_shift));
  134. return *this;
  135. }
  136.  
  137. fixed<t_shift>& operator+=(const fixed<t_shift>& f) {
  138. m_value += f.m_value;
  139. return *this;
  140. }
  141. fixed<t_shift>& operator-=(const fixed<t_shift>& f) {
  142. m_value -= f.m_value;
  143. return *this;
  144. }
  145. fixed<t_shift>& operator*=(const fixed<t_shift>& f) {
  146. m_value *= f.m_value;
  147. m_value >>= t_shift;
  148. return *this;
  149. }
  150. fixed<t_shift>& operator/=(const fixed<t_shift>& f) {
  151. assert(f.m_value != 0);
  152. m_value <<= t_shift;
  153. m_value /= f.m_value;
  154. return *this;
  155. }
  156.  
  157. fixed<t_shift>& operator+=(int i) {
  158. m_value += i << t_shift;
  159. return *this;
  160. }
  161. fixed<t_shift>& operator-=(int i) {
  162. m_value -= i << t_shift;
  163. return *this;
  164. }
  165. fixed<t_shift>& operator*=(int i) {
  166. m_value *= i;
  167. return *this;
  168. }
  169. fixed<t_shift>& operator/=(int i) {
  170. assert(i != 0);
  171. m_value /= i;
  172. return *this;
  173. }
  174. fixed<t_shift>& operator<<=(int i) {
  175. m_value <<= i;
  176. return *this;
  177. }
  178. fixed<t_shift>& operator>>=(int i) {
  179. m_value >>= i;
  180. return *this;
  181. }
  182.  
  183. fixed<t_shift>& operator+=(float f) {
  184. m_value += (int)(f * (1 << t_shift));
  185. return *this;
  186. }
  187. fixed<t_shift>& operator-=(float f) {
  188. m_value -= (int)(f * (1 << t_shift));
  189. return *this;
  190. }
  191. fixed<t_shift>& operator*=(float f) {
  192. m_value = (int)(m_value * f);
  193. return *this;
  194. }
  195. fixed<t_shift>& operator/=(float f) {
  196. assert(f != 0);
  197. m_value = (int)(m_value / f);
  198. return *this;
  199. }
  200.  
  201. friend inline fixed<t_shift> operator+(const fixed<t_shift>& f1, int i) {
  202. fixed<t_shift> f(f1);
  203. f += i;
  204. return f;
  205. }
  206. friend inline fixed<t_shift> operator-(const fixed<t_shift>& f1, int i) {
  207. fixed<t_shift> f(f1);
  208. f -= i;
  209. return f;
  210. }
  211. friend inline fixed<t_shift> operator*(const fixed<t_shift>& f1, int i) {
  212. fixed<t_shift> f(f1);
  213. f *= i;
  214. return f;
  215. }
  216. friend inline fixed<t_shift> operator/(const fixed<t_shift>& f1, int i) {
  217. fixed<t_shift> f(f1);
  218. f /= i;
  219. return f;
  220. }
  221. friend inline fixed<t_shift> operator+(
  222. const fixed<t_shift>& f1, const fixed<t_shift>& f2)
  223. {
  224. fixed<t_shift> f(f1);
  225. f += f2;
  226. return f;
  227. }
  228. friend inline fixed<t_shift> operator-(
  229. const fixed<t_shift>& f1, const fixed<t_shift>& f2)
  230. {
  231. fixed<t_shift> f(f1);
  232. f -= f2;
  233. return f;
  234. }
  235. friend inline fixed<t_shift> operator*(
  236. const fixed<t_shift>& f1, const fixed<t_shift>& f2)
  237. {
  238. fixed<t_shift> f(f1);
  239. f *= f2;
  240. return f;
  241. }
  242. friend inline fixed<t_shift> operator/(
  243. const fixed<t_shift>& f1, const fixed<t_shift>& f2)
  244. {
  245. fixed<t_shift> f(f1);
  246. f /= f2;
  247. return f;
  248. }
  249. friend inline fixed<t_shift> operator<<(const fixed<t_shift>& f1, int i) {
  250. fixed<t_shift> f(f1);
  251. f <<= i;
  252. return f;
  253. }
  254. friend inline fixed<t_shift> operator>>(const fixed<t_shift>& f1, int i) {
  255. fixed<t_shift> f(f1);
  256. f >>= i;
  257. return f;
  258. }
  259. };
  260.  
  261. template <unsigned int t_shift>
  262. std::ostream&
  263. operator<<(std::ostream& o, const fixed<t_shift>& f) {
  264. return (o << f.get_float());
  265. }
  266.  
  267. ///////////////////////////////////////////////////////////////////////////////
  268.  
  269. #ifdef __BORLANDC__
  270. #pragma option -w-8027
  271. #endif
  272.  
  273. int main(void)
  274. {
  275. fixed<8> f1(1.2f), f2(3.4f), f;
  276.  
  277. int i = 3;
  278. f = f1 + i;
  279. std::cout << "1.2 + 3 = " << f << std::endl;
  280. f = f1 - i;
  281. std::cout << "1.2 - 3 = " << f << std::endl;
  282. f = f1 * i;
  283. std::cout << "1.2 * 3 = " << f << std::endl;
  284. f = f1 / i;
  285. std::cout << "1.2 / 3 = " << f << std::endl;
  286.  
  287. f = f1 + f2;
  288. std::cout << "1.2 + 3.4 = " << f << std::endl;
  289. f = f1 - f2;
  290. std::cout << "1.2 - 3.4 = " << f << std::endl;
  291. f = f1 * f2;
  292. std::cout << "1.2 * 3.4 = " << f << std::endl;
  293. f = f1 / f2;
  294. std::cout << "1.2 / 3.4 = " << f << std::endl;
  295.  
  296. f = 1.2f;
  297. f += 2;
  298. std::cout << "1.2 + 2 = " << f << std::endl;
  299. f = 1.2f;
  300. f -= 2;
  301. std::cout << "1.2 - 2 = " << f << std::endl;
  302. f = 1.2f;
  303. f *= 2;
  304. std::cout << "1.2 * 2 = " << f << std::endl;
  305. f = 1.2f;
  306. f /= 2;
  307. std::cout << "1.2 / 2 = " << f << std::endl;
  308.  
  309. return 0;
  310. }
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
1.2 + 3 = 4.19922
1.2 - 3 = -1.80078
1.2 * 3 = 3.59766
1.2 / 3 = 0.398438
1.2 + 3.4 = 4.59766
1.2 - 3.4 = -2.19922
1.2 * 3.4 = 4.07422
1.2 / 3.4 = 0.351562
1.2 + 2 = 3.19922
1.2 - 2 = -0.800781
1.2 * 2 = 2.39844
1.2 / 2 = 0.597656