Double.cpp 4.4 KB

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