Float.cpp 4.1 KB

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