Float.cpp 5.0 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/Float.hpp>
  11. ls_std::Float::Float()
  12. : ls_std::Class("Float"),
  13. epsilon(0.00001f)
  14. {}
  15. ls_std::Float::Float(float _value)
  16. : ls_std::Class("Float"),
  17. epsilon(0.00001f),
  18. value(_value)
  19. {}
  20. ls_std::Float::operator float() const
  21. {
  22. return this->value;
  23. }
  24. ls_std::Float &ls_std::Float::operator=(float _value)
  25. {
  26. this->value = _value;
  27. return *this;
  28. }
  29. float ls_std::Float::operator-() const
  30. {
  31. return -this->value;
  32. }
  33. float ls_std::Float::operator+(const ls_std::Float &_float) const
  34. {
  35. return this->value + _float;
  36. }
  37. float ls_std::Float::operator+(float _value) const
  38. {
  39. return this->value + _value;
  40. }
  41. float ls_std::Float::operator*(const ls_std::Float &_float) const
  42. {
  43. return this->value * _float;
  44. }
  45. float ls_std::Float::operator*(float _value) const
  46. {
  47. return this->value * _value;
  48. }
  49. float ls_std::Float::operator-(const ls_std::Float &_float) const
  50. {
  51. return this->value - _float;
  52. }
  53. float ls_std::Float::operator-(float _value) const
  54. {
  55. return this->value - _value;
  56. }
  57. float ls_std::Float::operator/(const ls_std::Float &_float) const
  58. {
  59. return this->value / _float;
  60. }
  61. float ls_std::Float::operator/(float _value) const
  62. {
  63. return this->value / _value;
  64. }
  65. ls_std::Float &ls_std::Float::operator+=(const ls_std::Float &_float)
  66. {
  67. this->value += _float;
  68. return *this;
  69. }
  70. ls_std::Float &ls_std::Float::operator+=(float _value)
  71. {
  72. this->value += _value;
  73. return *this;
  74. }
  75. ls_std::Float &ls_std::Float::operator-=(const ls_std::Float &_float)
  76. {
  77. this->value -= _float;
  78. return *this;
  79. }
  80. ls_std::Float &ls_std::Float::operator-=(float _value)
  81. {
  82. this->value -= _value;
  83. return *this;
  84. }
  85. ls_std::Float &ls_std::Float::operator*=(const ls_std::Float &_float)
  86. {
  87. this->value *= _float;
  88. return *this;
  89. }
  90. ls_std::Float &ls_std::Float::operator*=(float _value)
  91. {
  92. this->value *= _value;
  93. return *this;
  94. }
  95. ls_std::Float &ls_std::Float::operator/=(const ls_std::Float &_float)
  96. {
  97. this->value /= _float;
  98. return *this;
  99. }
  100. ls_std::Float &ls_std::Float::operator/=(float _value)
  101. {
  102. this->value /= _value;
  103. return *this;
  104. }
  105. bool ls_std::Float::operator==(const ls_std::Float &_float) const
  106. {
  107. return std::fabs(this->value - _float) < this->epsilon;
  108. }
  109. bool ls_std::Float::operator==(float _value) const
  110. {
  111. return std::fabs(this->value - _value) < this->epsilon;
  112. }
  113. bool ls_std::Float::operator!=(const ls_std::Float &_float) const
  114. {
  115. return std::fabs(this->value - _float) >= this->epsilon;
  116. }
  117. bool ls_std::Float::operator!=(float _value) const
  118. {
  119. return std::fabs(this->value - _value) >= this->epsilon;
  120. }
  121. bool ls_std::Float::operator>(const ls_std::Float &_float) const
  122. {
  123. return this->value > _float;
  124. }
  125. bool ls_std::Float::operator>(float _value) const
  126. {
  127. return this->value > _value;
  128. }
  129. bool ls_std::Float::operator>=(const ls_std::Float &_float) const
  130. {
  131. return this->value >= _float;
  132. }
  133. bool ls_std::Float::operator>=(float _value) const
  134. {
  135. return this->value >= _value;
  136. }
  137. bool ls_std::Float::operator<(const ls_std::Float &_float) const
  138. {
  139. return this->value < _float;
  140. }
  141. bool ls_std::Float::operator<(float _value) const
  142. {
  143. return this->value < _value;
  144. }
  145. bool ls_std::Float::operator<=(const ls_std::Float &_float) const
  146. {
  147. return this->value <= _float;
  148. }
  149. bool ls_std::Float::operator<=(float _value) const
  150. {
  151. return this->value <= _value;
  152. }
  153. void ls_std::Float::operator++()
  154. {
  155. this->value += 1.0f;
  156. }
  157. void ls_std::Float::operator--()
  158. {
  159. this->value -= 1.0f;
  160. }
  161. ls_std::byte_field ls_std::Float::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::Float::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::Float::parse(std::string _parseText)
  181. {
  182. this->value = std::stof(_parseText);
  183. }
  184. void ls_std::Float::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::Float::toString()
  199. {
  200. return std::to_string(this->value);
  201. }
  202. void ls_std::Float::unmarshal(const ls_std::byte_field &_data)
  203. {
  204. if (this->serializable != nullptr)
  205. {
  206. this->serializable->unmarshal(_data);
  207. }
  208. }
  209. float ls_std::Float::getEpsilon()
  210. {
  211. return this->epsilon;
  212. }
  213. float ls_std::Float::getValue()
  214. {
  215. return this->value;
  216. }
  217. void ls_std::Float::setEpsilon(float _epsilon)
  218. {
  219. this->epsilon = _epsilon;
  220. }
  221. void ls_std::Float::setSerializable(std::shared_ptr<ISerializable> _serializable)
  222. {
  223. this->serializable = std::move(_serializable);
  224. }
  225. void ls_std::Float::setStorable(std::shared_ptr<IStorable> _storable)
  226. {
  227. this->storable = std::move(_storable);
  228. }