Double.cpp 4.1 KB

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