Double.cppm 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. export module ls.std.boxing.double;
  2. /*
  3. * Author: Patrick-Christopher Mattulat
  4. * Company: Lynar Studios
  5. * E-Mail: webmaster@lynarstudios.com
  6. * Created: 2020-08-14
  7. * Changed: 2025-12-25
  8. *
  9. * */
  10. #include <cmath>
  11. #include <ls-std/core/Class.hpp>
  12. #include <ls-std/core/interface/IBoxing.hpp>
  13. #include <ls-std/core/exception/IllegalArgumentException.hpp>
  14. #include <ls-std/os/dynamic-goal.hpp>
  15. /*
  16. * @doc: class(name: 'Double', package: 'boxing')
  17. * @doc: boxing.Double.description('This class represents the primitive datatype double and provides functionalities for arithmetic operations, accuracy and string representation.')
  18. * */
  19. using ls::std::core::Class;
  20. using ls::std::core::IllegalArgumentException;
  21. using ls::std::core::interface_type::IBoxing;
  22. using std::fabs;
  23. using std::stod;
  24. using std::string;
  25. using std::to_string;
  26. export namespace ls::std::boxing
  27. {
  28. class LS_STD_DYNAMIC_GOAL Double : public Class, public IBoxing
  29. {
  30. public:
  31. Double() : Class(_fetchClassName())
  32. {
  33. this->_assignEpsilon(0.00000001);
  34. }
  35. explicit Double(const double _value) : Class(_fetchClassName()), value(_value)
  36. {
  37. this->_assignEpsilon(0.00000001);
  38. }
  39. ~Double() noexcept override = default;
  40. Double &operator=(const double _value)
  41. {
  42. this->value = _value;
  43. return *this;
  44. }
  45. double operator-() const
  46. {
  47. return -this->value;
  48. }
  49. double operator+(const Double &_double) const
  50. {
  51. return this->value + _double.getValue();
  52. }
  53. double operator+(const double _value) const
  54. {
  55. return this->value + _value;
  56. }
  57. double operator*(const Double &_double) const
  58. {
  59. return this->value * _double.getValue();
  60. }
  61. double operator*(const double _value) const
  62. {
  63. return this->value * _value;
  64. }
  65. double operator-(const Double &_double) const
  66. {
  67. return this->value - _double.getValue();
  68. }
  69. double operator-(const double _value) const
  70. {
  71. return this->value - _value;
  72. }
  73. double operator/(const Double &_double) const
  74. {
  75. return this->value / _double.getValue();
  76. }
  77. double operator/(const double _value) const
  78. {
  79. return this->value / _value;
  80. }
  81. Double &operator+=(const Double &_double)
  82. {
  83. this->value += _double.getValue();
  84. return *this;
  85. }
  86. Double &operator+=(const double _value)
  87. {
  88. this->value += _value;
  89. return *this;
  90. }
  91. Double &operator-=(const Double &_double)
  92. {
  93. this->value -= _double.getValue();
  94. return *this;
  95. }
  96. Double &operator-=(const double _value)
  97. {
  98. this->value -= _value;
  99. return *this;
  100. }
  101. Double &operator*=(const Double &_double)
  102. {
  103. this->value *= _double.getValue();
  104. return *this;
  105. }
  106. Double &operator*=(const double _value)
  107. {
  108. this->value *= _value;
  109. return *this;
  110. }
  111. Double &operator/=(const Double &_double)
  112. {
  113. this->value /= _double.getValue();
  114. return *this;
  115. }
  116. Double &operator/=(const double _value)
  117. {
  118. this->value /= _value;
  119. return *this;
  120. }
  121. bool operator==(const Double &_double) const
  122. {
  123. return fabs(this->value - _double.getValue()) < this->epsilon;
  124. }
  125. bool operator==(const double _value) const
  126. {
  127. return fabs(this->value - _value) < this->epsilon;
  128. }
  129. bool operator!=(const Double &_double) const
  130. {
  131. return fabs(this->value - _double.getValue()) >= this->epsilon;
  132. }
  133. bool operator!=(const double _value) const
  134. {
  135. return fabs(this->value - _value) >= this->epsilon;
  136. }
  137. bool operator>(const Double &_double) const
  138. {
  139. return this->value > _double.getValue();
  140. }
  141. bool operator>(const double _value) const
  142. {
  143. return this->value > _value;
  144. }
  145. bool operator>=(const Double &_double) const
  146. {
  147. return this->value >= _double.getValue();
  148. }
  149. bool operator>=(const double _value) const
  150. {
  151. return this->value >= _value;
  152. }
  153. bool operator<(const Double &_double) const
  154. {
  155. return this->value < _double.getValue();
  156. }
  157. bool operator<(const double _value) const
  158. {
  159. return this->value < _value;
  160. }
  161. bool operator<=(const Double &_double) const
  162. {
  163. return this->value <= _double.getValue();
  164. }
  165. bool operator<=(const double _value) const
  166. {
  167. return this->value <= _value;
  168. }
  169. void operator++()
  170. {
  171. this->value += 1.0;
  172. }
  173. void operator--()
  174. {
  175. this->value -= 1.0;
  176. }
  177. void parse(const string &_parseText) override
  178. {
  179. this->value = stod(_parseText);
  180. }
  181. [[nodiscard]] string toString() override
  182. {
  183. return to_string(this->value);
  184. }
  185. [[nodiscard]] double getEpsilon() const
  186. {
  187. return this->epsilon;
  188. }
  189. [[nodiscard]] double getValue() const
  190. {
  191. return this->value;
  192. }
  193. void setEpsilon(const double _epsilon)
  194. {
  195. this->_assignEpsilon(_epsilon);
  196. }
  197. private:
  198. double epsilon{};
  199. double value{};
  200. void _assignEpsilon(const double _epsilon)
  201. {
  202. if (_epsilon <= 0.0)
  203. {
  204. throw IllegalArgumentException{"_epsilon is less than or equal zero"};
  205. }
  206. this->epsilon = _epsilon;
  207. }
  208. [[nodiscard]] static string _fetchClassName()
  209. {
  210. static const string className = "Double";
  211. return className;
  212. }
  213. };
  214. }