Double.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <cmath>
  10. #include <ls_std/boxing/Double.hpp>
  11. ls_std::Double::Double()
  12. : ls_std::Class("Double"),
  13. epsilon(0.00000001)
  14. {}
  15. ls_std::Double::Double(double _value)
  16. : ls_std::Class("Double"),
  17. epsilon(0.00000001),
  18. value(_value)
  19. {}
  20. ls_std::Double::operator double() const
  21. {
  22. return this->value;
  23. }
  24. ls_std::Double &ls_std::Double::operator=(double _value)
  25. {
  26. this->value = _value;
  27. return *this;
  28. }
  29. double ls_std::Double::operator-() const
  30. {
  31. return -this->value;
  32. }
  33. double ls_std::Double::operator+(const ls_std::Double &_double) const
  34. {
  35. return this->value + _double;
  36. }
  37. double ls_std::Double::operator+(double _value) const
  38. {
  39. return this->value + _value;
  40. }
  41. double ls_std::Double::operator*(const ls_std::Double &_double) const
  42. {
  43. return this->value * _double;
  44. }
  45. double ls_std::Double::operator*(double _value) const
  46. {
  47. return this->value * _value;
  48. }
  49. double ls_std::Double::operator-(const ls_std::Double &_double) const
  50. {
  51. return this->value - _double;
  52. }
  53. double ls_std::Double::operator-(double _value) const
  54. {
  55. return this->value - _value;
  56. }
  57. double ls_std::Double::operator/(const ls_std::Double &_double) const
  58. {
  59. return this->value / _double;
  60. }
  61. double ls_std::Double::operator/(double _value) const
  62. {
  63. return this->value / _value;
  64. }
  65. ls_std::Double &ls_std::Double::operator+=(const ls_std::Double &_double)
  66. {
  67. this->value += _double;
  68. return *this;
  69. }
  70. ls_std::Double &ls_std::Double::operator+=(double _value)
  71. {
  72. this->value += _value;
  73. return *this;
  74. }
  75. ls_std::Double &ls_std::Double::operator-=(const ls_std::Double &_double)
  76. {
  77. this->value -= _double;
  78. return *this;
  79. }
  80. ls_std::Double &ls_std::Double::operator-=(double _value)
  81. {
  82. this->value -= _value;
  83. return *this;
  84. }
  85. ls_std::Double &ls_std::Double::operator*=(const ls_std::Double &_double)
  86. {
  87. this->value *= _double;
  88. return *this;
  89. }
  90. ls_std::Double &ls_std::Double::operator*=(double _value)
  91. {
  92. this->value *= _value;
  93. return *this;
  94. }
  95. ls_std::Double &ls_std::Double::operator/=(const ls_std::Double &_double)
  96. {
  97. this->value /= _double;
  98. return *this;
  99. }
  100. ls_std::Double &ls_std::Double::operator/=(double _value)
  101. {
  102. this->value /= _value;
  103. return *this;
  104. }
  105. bool ls_std::Double::operator==(const ls_std::Double &_double) const
  106. {
  107. return std::fabs(this->value - _double) < this->epsilon;
  108. }
  109. bool ls_std::Double::operator==(double _value) const
  110. {
  111. return std::fabs(this->value - _value) < this->epsilon;
  112. }
  113. bool ls_std::Double::operator!=(const ls_std::Double &_double) const
  114. {
  115. return std::fabs(this->value - _double) >= this->epsilon;
  116. }
  117. bool ls_std::Double::operator!=(double _value) const
  118. {
  119. return std::fabs(this->value - _value) >= this->epsilon;
  120. }
  121. bool ls_std::Double::operator>(const ls_std::Double &_double) const
  122. {
  123. return this->value > _double;
  124. }
  125. bool ls_std::Double::operator>(double _value) const
  126. {
  127. return this->value > _value;
  128. }
  129. bool ls_std::Double::operator>=(const ls_std::Double &_double) const
  130. {
  131. return this->value >= _double;
  132. }
  133. bool ls_std::Double::operator>=(double _value) const
  134. {
  135. return this->value >= _value;
  136. }
  137. bool ls_std::Double::operator<(const ls_std::Double &_double) const
  138. {
  139. return this->value < _double;
  140. }
  141. bool ls_std::Double::operator<(double _value) const
  142. {
  143. return this->value < _value;
  144. }
  145. bool ls_std::Double::operator<=(const ls_std::Double &_double) const
  146. {
  147. return this->value <= _double;
  148. }
  149. bool ls_std::Double::operator<=(double _value) const
  150. {
  151. return this->value <= _value;
  152. }
  153. void ls_std::Double::operator++()
  154. {
  155. this->value += 1.0f;
  156. }
  157. void ls_std::Double::operator--()
  158. {
  159. this->value -= 1.0f;
  160. }
  161. ls_std::byte_field ls_std::Double::load()
  162. {
  163. ls_std::byte_field data{};
  164. if (this->storable != nullptr && this->serializable != nullptr)
  165. {
  166. data = this->storable->load();
  167. this->serializable->unmarshal(data);
  168. }
  169. return data;
  170. }
  171. ls_std::byte_field ls_std::Double::marshal()
  172. {
  173. ls_std::byte_field data{};
  174. if (this->serializable != nullptr)
  175. {
  176. data = this->serializable->marshal();
  177. }
  178. return data;
  179. }
  180. void ls_std::Double::parse(std::string _parseText)
  181. {
  182. this->value = std::stod(_parseText);
  183. }
  184. void ls_std::Double::save(const ls_std::byte_field &_data)
  185. {
  186. if (this->serializable != nullptr)
  187. {
  188. if (_data.empty())
  189. {
  190. this->storable->save(this->serializable->marshal());
  191. }
  192. else
  193. {
  194. this->storable->save(_data);
  195. }
  196. }
  197. }
  198. std::string ls_std::Double::toString()
  199. {
  200. return std::to_string(this->value);
  201. }
  202. void ls_std::Double::unmarshal(const ls_std::byte_field &_data)
  203. {
  204. if (this->serializable != nullptr)
  205. {
  206. this->serializable->unmarshal(_data);
  207. }
  208. }
  209. double ls_std::Double::getEpsilon()
  210. {
  211. return this->epsilon;
  212. }
  213. double ls_std::Double::getValue()
  214. {
  215. return this->value;
  216. }
  217. void ls_std::Double::setEpsilon(double _epsilon)
  218. {
  219. this->epsilon = _epsilon;
  220. }
  221. void ls_std::Double::setSerializable(std::shared_ptr<ISerializable> _serializable)
  222. {
  223. this->serializable = std::move(_serializable);
  224. }
  225. void ls_std::Double::setStorable(std::shared_ptr<IStorable> _storable)
  226. {
  227. this->storable = std::move(_storable);
  228. }