MathematicalValue.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. auto value_as_string = MUST(value.to_base(10));
  213. return static_cast<int>(value_as_string.bytes_as_string_view().length() - 1);
  214. },
  215. [](auto) -> int { VERIFY_NOT_REACHED(); });
  216. }
  217. bool MathematicalValue::is_equal_to(MathematicalValue const& other) const
  218. {
  219. return m_value.visit(
  220. [&](double value) {
  221. static constexpr double epsilon = 5e-14;
  222. return fabs(value - other.as_number()) < epsilon;
  223. },
  224. [&](Crypto::SignedBigInteger const& value) {
  225. return value == other.as_bigint();
  226. },
  227. [](auto) -> bool { VERIFY_NOT_REACHED(); });
  228. }
  229. bool MathematicalValue::is_less_than(MathematicalValue const& other) const
  230. {
  231. return m_value.visit(
  232. [&](double value) {
  233. if (is_equal_to(other))
  234. return false;
  235. return value < other.as_number();
  236. },
  237. [&](Crypto::SignedBigInteger const& value) {
  238. return value < other.as_bigint();
  239. },
  240. [](auto) -> bool { VERIFY_NOT_REACHED(); });
  241. }
  242. bool MathematicalValue::is_negative() const
  243. {
  244. return m_value.visit(
  245. [](double value) { return value < 0.0; },
  246. [](Crypto::SignedBigInteger const& value) { return value.is_negative(); },
  247. [](Symbol symbol) { return symbol == Symbol::NegativeInfinity; });
  248. }
  249. bool MathematicalValue::is_positive() const
  250. {
  251. return m_value.visit(
  252. [](double value) { return value > 0.0; },
  253. [](Crypto::SignedBigInteger const& value) { return !value.is_zero() && !value.is_negative(); },
  254. [](Symbol symbol) { return symbol == Symbol::PositiveInfinity; });
  255. }
  256. bool MathematicalValue::is_zero() const
  257. {
  258. return m_value.visit(
  259. [&](double value) { return value == 0.0; },
  260. [](Crypto::SignedBigInteger const& value) { return value.is_zero(); },
  261. [](auto) { return false; });
  262. }
  263. String MathematicalValue::to_string() const
  264. {
  265. return m_value.visit(
  266. [&](double value) {
  267. return number_to_string(value, NumberToStringMode::WithoutExponent);
  268. },
  269. [&](Crypto::SignedBigInteger const& value) {
  270. return MUST(value.to_base(10));
  271. },
  272. [&](auto) -> String { VERIFY_NOT_REACHED(); });
  273. }
  274. Value MathematicalValue::to_value(VM& vm) const
  275. {
  276. return m_value.visit(
  277. [](double value) {
  278. return Value(value);
  279. },
  280. [&](Crypto::SignedBigInteger const& value) {
  281. return Value(BigInt::create(vm, value));
  282. },
  283. [](auto symbol) {
  284. switch (symbol) {
  285. case Symbol::PositiveInfinity:
  286. return js_infinity();
  287. case Symbol::NegativeInfinity:
  288. return js_negative_infinity();
  289. case Symbol::NegativeZero:
  290. return Value(-0.0);
  291. case Symbol::NotANumber:
  292. return js_nan();
  293. }
  294. VERIFY_NOT_REACHED();
  295. });
  296. }
  297. MathematicalValue::ValueType MathematicalValue::value_from_number(double number)
  298. {
  299. Value value(number);
  300. if (value.is_positive_infinity())
  301. return Symbol::PositiveInfinity;
  302. if (value.is_negative_infinity())
  303. return Symbol::NegativeInfinity;
  304. if (value.is_negative_zero())
  305. return Symbol::NegativeZero;
  306. if (value.is_nan())
  307. return Symbol::NotANumber;
  308. return number;
  309. }
  310. }