Float.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2023-02-22
  7. *
  8. * */
  9. #include <cmath>
  10. #include <ls-std/boxing/Float.hpp>
  11. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  12. ls::std::boxing::Float::Float() : ls::std::core::Class("Float"), epsilon(0.00001f)
  13. {}
  14. ls::std::boxing::Float::Float(float _value) : ls::std::core::Class("Float"), epsilon(0.00001f), value(_value)
  15. {}
  16. ls::std::boxing::Float::~Float() noexcept = default;
  17. ls::std::boxing::Float &ls::std::boxing::Float::operator=(float _value)
  18. {
  19. this->value = _value;
  20. return *this;
  21. }
  22. float ls::std::boxing::Float::operator-() const
  23. {
  24. return -this->value;
  25. }
  26. float ls::std::boxing::Float::operator+(const ls::std::boxing::Float &_float) const
  27. {
  28. return this->value + _float.getValue();
  29. }
  30. float ls::std::boxing::Float::operator+(float _value) const
  31. {
  32. return this->value + _value;
  33. }
  34. float ls::std::boxing::Float::operator*(const ls::std::boxing::Float &_float) const
  35. {
  36. return this->value * _float.getValue();
  37. }
  38. float ls::std::boxing::Float::operator*(float _value) const
  39. {
  40. return this->value * _value;
  41. }
  42. float ls::std::boxing::Float::operator-(const ls::std::boxing::Float &_float) const
  43. {
  44. return this->value - _float.getValue();
  45. }
  46. float ls::std::boxing::Float::operator-(float _value) const
  47. {
  48. return this->value - _value;
  49. }
  50. float ls::std::boxing::Float::operator/(const ls::std::boxing::Float &_float) const
  51. {
  52. return this->value / _float.getValue();
  53. }
  54. float ls::std::boxing::Float::operator/(float _value) const
  55. {
  56. return this->value / _value;
  57. }
  58. ls::std::boxing::Float &ls::std::boxing::Float::operator+=(const ls::std::boxing::Float &_float)
  59. {
  60. this->value += _float.getValue();
  61. return *this;
  62. }
  63. ls::std::boxing::Float &ls::std::boxing::Float::operator+=(float _value)
  64. {
  65. this->value += _value;
  66. return *this;
  67. }
  68. ls::std::boxing::Float &ls::std::boxing::Float::operator-=(const ls::std::boxing::Float &_float)
  69. {
  70. this->value -= _float.getValue();
  71. return *this;
  72. }
  73. ls::std::boxing::Float &ls::std::boxing::Float::operator-=(float _value)
  74. {
  75. this->value -= _value;
  76. return *this;
  77. }
  78. ls::std::boxing::Float &ls::std::boxing::Float::operator*=(const ls::std::boxing::Float &_float)
  79. {
  80. this->value *= _float.getValue();
  81. return *this;
  82. }
  83. ls::std::boxing::Float &ls::std::boxing::Float::operator*=(float _value)
  84. {
  85. this->value *= _value;
  86. return *this;
  87. }
  88. ls::std::boxing::Float &ls::std::boxing::Float::operator/=(const ls::std::boxing::Float &_float)
  89. {
  90. this->value /= _float.getValue();
  91. return *this;
  92. }
  93. ls::std::boxing::Float &ls::std::boxing::Float::operator/=(float _value)
  94. {
  95. this->value /= _value;
  96. return *this;
  97. }
  98. bool ls::std::boxing::Float::operator==(const ls::std::boxing::Float &_float) const
  99. {
  100. return ::std::fabs(this->value - _float.getValue()) < this->epsilon;
  101. }
  102. bool ls::std::boxing::Float::operator==(float _value) const
  103. {
  104. return ::std::fabs(this->value - _value) < this->epsilon;
  105. }
  106. bool ls::std::boxing::Float::operator!=(const ls::std::boxing::Float &_float) const
  107. {
  108. return ::std::fabs(this->value - _float.getValue()) >= this->epsilon;
  109. }
  110. bool ls::std::boxing::Float::operator!=(float _value) const
  111. {
  112. return ::std::fabs(this->value - _value) >= this->epsilon;
  113. }
  114. bool ls::std::boxing::Float::operator>(const ls::std::boxing::Float &_float) const
  115. {
  116. return this->value > _float.getValue();
  117. }
  118. bool ls::std::boxing::Float::operator>(float _value) const
  119. {
  120. return this->value > _value;
  121. }
  122. bool ls::std::boxing::Float::operator>=(const ls::std::boxing::Float &_float) const
  123. {
  124. return this->value >= _float.getValue();
  125. }
  126. bool ls::std::boxing::Float::operator>=(float _value) const
  127. {
  128. return this->value >= _value;
  129. }
  130. bool ls::std::boxing::Float::operator<(const ls::std::boxing::Float &_float) const
  131. {
  132. return this->value < _float.getValue();
  133. }
  134. bool ls::std::boxing::Float::operator<(float _value) const
  135. {
  136. return this->value < _value;
  137. }
  138. bool ls::std::boxing::Float::operator<=(const ls::std::boxing::Float &_float) const
  139. {
  140. return this->value <= _float.getValue();
  141. }
  142. bool ls::std::boxing::Float::operator<=(float _value) const
  143. {
  144. return this->value <= _value;
  145. }
  146. void ls::std::boxing::Float::operator++()
  147. {
  148. this->value += 1.0f;
  149. }
  150. void ls::std::boxing::Float::operator--()
  151. {
  152. this->value -= 1.0f;
  153. }
  154. void ls::std::boxing::Float::parse(::std::string _parseText)
  155. {
  156. this->value = ::std::stof(_parseText);
  157. }
  158. ::std::string ls::std::boxing::Float::toString()
  159. {
  160. return ::std::to_string(this->value);
  161. }
  162. float ls::std::boxing::Float::getEpsilon() const
  163. {
  164. return this->epsilon;
  165. }
  166. float ls::std::boxing::Float::getValue() const
  167. {
  168. return this->value;
  169. }
  170. void ls::std::boxing::Float::setEpsilon(float _epsilon)
  171. {
  172. this->_assignEpsilon(_epsilon);
  173. }
  174. void ls::std::boxing::Float::_assignEpsilon(float _epsilon)
  175. {
  176. if (_epsilon <= 0.0)
  177. {
  178. throw ls::std::core::IllegalArgumentException{"epsilon is less than or equal zero"};
  179. }
  180. this->epsilon = _epsilon;
  181. }