Float.cpp 4.2 KB

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