Float.cpp 3.9 KB

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