MathematicalValue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibJS/Runtime/AbstractOperations.h>
  7. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/Intl/MathematicalValue.h>
  9. #include <math.h>
  10. namespace JS::Intl {
  11. bool MathematicalValue::is_number() const
  12. {
  13. return m_value.has<double>();
  14. }
  15. double MathematicalValue::as_number() const
  16. {
  17. VERIFY(is_number());
  18. return m_value.get<double>();
  19. }
  20. bool MathematicalValue::is_bigint() const
  21. {
  22. return m_value.has<Crypto::SignedBigInteger>();
  23. }
  24. Crypto::SignedBigInteger const& MathematicalValue::as_bigint() const
  25. {
  26. VERIFY(is_bigint());
  27. return m_value.get<Crypto::SignedBigInteger>();
  28. }
  29. bool MathematicalValue::is_mathematical_value() const
  30. {
  31. return is_number() || is_bigint();
  32. }
  33. bool MathematicalValue::is_positive_infinity() const
  34. {
  35. if (is_mathematical_value())
  36. return false;
  37. return m_value.get<Symbol>() == Symbol::PositiveInfinity;
  38. }
  39. bool MathematicalValue::is_negative_infinity() const
  40. {
  41. if (is_mathematical_value())
  42. return false;
  43. return m_value.get<Symbol>() == Symbol::NegativeInfinity;
  44. }
  45. bool MathematicalValue::is_negative_zero() const
  46. {
  47. if (is_mathematical_value())
  48. return false;
  49. return m_value.get<Symbol>() == Symbol::NegativeZero;
  50. }
  51. bool MathematicalValue::is_nan() const
  52. {
  53. if (is_mathematical_value())
  54. return false;
  55. return m_value.get<Symbol>() == Symbol::NotANumber;
  56. }
  57. void MathematicalValue::negate()
  58. {
  59. m_value.visit(
  60. [](double& value) {
  61. VERIFY(value != 0.0);
  62. value *= -1.0;
  63. },
  64. [](Crypto::SignedBigInteger& value) { value.negate(); },
  65. [](auto) { VERIFY_NOT_REACHED(); });
  66. }
  67. MathematicalValue MathematicalValue::plus(Checked<i32> addition) const
  68. {
  69. return m_value.visit(
  70. [&](double value) {
  71. return MathematicalValue { value + addition.value() };
  72. },
  73. [&](Crypto::SignedBigInteger const& value) {
  74. return MathematicalValue { value.plus(Crypto::SignedBigInteger { addition.value() }) };
  75. },
  76. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  77. }
  78. MathematicalValue MathematicalValue::plus(MathematicalValue const& addition) const
  79. {
  80. return m_value.visit(
  81. [&](double value) {
  82. return MathematicalValue { value + addition.as_number() };
  83. },
  84. [&](Crypto::SignedBigInteger const& value) {
  85. return MathematicalValue { value.plus(addition.as_bigint()) };
  86. },
  87. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  88. }
  89. MathematicalValue MathematicalValue::minus(Checked<i32> subtraction) const
  90. {
  91. return m_value.visit(
  92. [&](double value) {
  93. return MathematicalValue { value - subtraction.value() };
  94. },
  95. [&](Crypto::SignedBigInteger const& value) {
  96. return MathematicalValue { value.minus(Crypto::SignedBigInteger { subtraction.value() }) };
  97. },
  98. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  99. }
  100. MathematicalValue MathematicalValue::minus(MathematicalValue const& subtraction) const
  101. {
  102. return m_value.visit(
  103. [&](double value) {
  104. return MathematicalValue { value - subtraction.as_number() };
  105. },
  106. [&](Crypto::SignedBigInteger const& value) {
  107. return MathematicalValue { value.minus(subtraction.as_bigint()) };
  108. },
  109. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  110. }
  111. MathematicalValue MathematicalValue::multiplied_by(Checked<i32> multiplier) const
  112. {
  113. return m_value.visit(
  114. [&](double value) {
  115. return MathematicalValue { value * multiplier.value() };
  116. },
  117. [&](Crypto::SignedBigInteger const& value) {
  118. return MathematicalValue { value.multiplied_by(Crypto::SignedBigInteger { multiplier.value() }) };
  119. },
  120. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  121. }
  122. MathematicalValue MathematicalValue::multiplied_by(MathematicalValue const& multiplier) const
  123. {
  124. return m_value.visit(
  125. [&](double value) {
  126. return MathematicalValue { value * multiplier.as_number() };
  127. },
  128. [&](Crypto::SignedBigInteger const& value) {
  129. return MathematicalValue { value.multiplied_by(multiplier.as_bigint()) };
  130. },
  131. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  132. }
  133. MathematicalValue MathematicalValue::divided_by(Checked<i32> divisor) const
  134. {
  135. return m_value.visit(
  136. [&](double value) {
  137. return MathematicalValue { value / divisor.value() };
  138. },
  139. [&](Crypto::SignedBigInteger const& value) {
  140. return MathematicalValue { value.divided_by(Crypto::SignedBigInteger { divisor.value() }).quotient };
  141. },
  142. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  143. }
  144. MathematicalValue MathematicalValue::divided_by(MathematicalValue const& divisor) const
  145. {
  146. return m_value.visit(
  147. [&](double value) {
  148. return MathematicalValue { value / divisor.as_number() };
  149. },
  150. [&](Crypto::SignedBigInteger const& value) {
  151. return MathematicalValue { value.divided_by(divisor.as_bigint()).quotient };
  152. },
  153. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  154. }
  155. static Crypto::SignedBigInteger bigint_power(Checked<i32> exponent)
  156. {
  157. VERIFY(exponent >= 0);
  158. static auto base = Crypto::SignedBigInteger { 10 };
  159. auto result = Crypto::SignedBigInteger { 1 };
  160. for (i32 i = 0; i < exponent; ++i)
  161. result = result.multiplied_by(base);
  162. return result;
  163. }
  164. MathematicalValue MathematicalValue::multiplied_by_power(Checked<i32> exponent) const
  165. {
  166. return m_value.visit(
  167. [&](double value) {
  168. return MathematicalValue { value * pow(10, exponent.value()) };
  169. },
  170. [&](Crypto::SignedBigInteger const& value) {
  171. if (exponent < 0)
  172. return MathematicalValue { value.divided_by(bigint_power(-exponent.value())).quotient };
  173. return MathematicalValue { value.multiplied_by(bigint_power(exponent)) };
  174. },
  175. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  176. }
  177. MathematicalValue MathematicalValue::divided_by_power(Checked<i32> exponent) const
  178. {
  179. return m_value.visit(
  180. [&](double value) {
  181. if (exponent < 0)
  182. return MathematicalValue { value * pow(10, -exponent.value()) };
  183. return MathematicalValue { value / pow(10, exponent.value()) };
  184. },
  185. [&](Crypto::SignedBigInteger const& value) {
  186. if (exponent < 0)
  187. return MathematicalValue { value.multiplied_by(bigint_power(-exponent.value())) };
  188. return MathematicalValue { value.divided_by(bigint_power(exponent)).quotient };
  189. },
  190. [](auto) -> MathematicalValue { VERIFY_NOT_REACHED(); });
  191. }
  192. bool MathematicalValue::modulo_is_zero(Checked<i32> mod) const
  193. {
  194. return m_value.visit(
  195. [&](double value) {
  196. auto result = MathematicalValue { modulo(value, mod.value()) };
  197. return result.is_equal_to(MathematicalValue { 0.0 });
  198. },
  199. [&](Crypto::SignedBigInteger const& value) {
  200. return modulo(value, Crypto::SignedBigInteger { mod.value() }).is_zero();
  201. },
  202. [](auto) -> bool { VERIFY_NOT_REACHED(); });
  203. }
  204. int MathematicalValue::logarithmic_floor() const
  205. {
  206. return m_value.visit(
  207. [](double value) {
  208. return static_cast<int>(floor(log10(value)));
  209. },
  210. [](Crypto::SignedBigInteger const& value) {
  211. // FIXME: Can we do this without string conversion?
  212. return static_cast<int>(value.to_base(10).length() - 1);
  213. },
  214. [](auto) -> int { VERIFY_NOT_REACHED(); });
  215. }
  216. bool MathematicalValue::is_equal_to(MathematicalValue const& other) const
  217. {
  218. return m_value.visit(
  219. [&](double value) {
  220. static constexpr double epsilon = 5e-14;
  221. return fabs(value - other.as_number()) < epsilon;
  222. },
  223. [&](Crypto::SignedBigInteger const& value) {
  224. return value == other.as_bigint();
  225. },
  226. [](auto) -> bool { VERIFY_NOT_REACHED(); });
  227. }
  228. bool MathematicalValue::is_less_than(MathematicalValue const& other) const
  229. {
  230. return m_value.visit(
  231. [&](double value) {
  232. if (is_equal_to(other))
  233. return false;
  234. return value < other.as_number();
  235. },
  236. [&](Crypto::SignedBigInteger const& value) {
  237. return value < other.as_bigint();
  238. },
  239. [](auto) -> bool { VERIFY_NOT_REACHED(); });
  240. }
  241. bool MathematicalValue::is_negative() const
  242. {
  243. return m_value.visit(
  244. [](double value) { return value < 0.0; },
  245. [](Crypto::SignedBigInteger const& value) { return value.is_negative(); },
  246. [](Symbol symbol) { return symbol == Symbol::NegativeInfinity; });
  247. }
  248. bool MathematicalValue::is_positive() const
  249. {
  250. return m_value.visit(
  251. [](double value) { return value > 0.0; },
  252. [](Crypto::SignedBigInteger const& value) { return !value.is_zero() && !value.is_negative(); },
  253. [](Symbol symbol) { return symbol == Symbol::PositiveInfinity; });
  254. }
  255. bool MathematicalValue::is_zero() const
  256. {
  257. return m_value.visit(
  258. [&](double value) { return value == 0.0; },
  259. [](Crypto::SignedBigInteger const& value) { return value.is_zero(); },
  260. [](auto) { return false; });
  261. }
  262. DeprecatedString MathematicalValue::to_deprecated_string() const
  263. {
  264. return m_value.visit(
  265. [](double value) { return number_to_string(value, NumberToStringMode::WithoutExponent); },
  266. [](Crypto::SignedBigInteger const& value) { return value.to_base(10); },
  267. [](auto) -> DeprecatedString { VERIFY_NOT_REACHED(); });
  268. }
  269. Value MathematicalValue::to_value(VM& vm) const
  270. {
  271. return m_value.visit(
  272. [](double value) {
  273. return Value(value);
  274. },
  275. [&](Crypto::SignedBigInteger const& value) {
  276. return Value(BigInt::create(vm, value));
  277. },
  278. [](auto symbol) {
  279. switch (symbol) {
  280. case Symbol::PositiveInfinity:
  281. return js_infinity();
  282. case Symbol::NegativeInfinity:
  283. return js_negative_infinity();
  284. case Symbol::NegativeZero:
  285. return Value(-0.0);
  286. case Symbol::NotANumber:
  287. return js_nan();
  288. }
  289. VERIFY_NOT_REACHED();
  290. });
  291. }
  292. MathematicalValue::ValueType MathematicalValue::value_from_number(double number)
  293. {
  294. Value value(number);
  295. if (value.is_positive_infinity())
  296. return Symbol::PositiveInfinity;
  297. if (value.is_negative_infinity())
  298. return Symbol::NegativeInfinity;
  299. if (value.is_negative_zero())
  300. return Symbol::NegativeZero;
  301. if (value.is_nan())
  302. return Symbol::NotANumber;
  303. return number;
  304. }
  305. }