MathematicalValue.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Checked.h>
  8. #include <AK/String.h>
  9. #include <AK/Variant.h>
  10. #include <LibCrypto/BigInt/SignedBigInteger.h>
  11. #include <LibJS/Runtime/BigInt.h>
  12. #include <LibJS/Runtime/Value.h>
  13. namespace JS::Intl {
  14. // https://tc39.es/ecma402/#intl-mathematical-value
  15. class MathematicalValue {
  16. public:
  17. enum class Symbol {
  18. PositiveInfinity,
  19. NegativeInfinity,
  20. NegativeZero,
  21. NotANumber,
  22. };
  23. MathematicalValue() = default;
  24. explicit MathematicalValue(double value)
  25. : m_value(value_from_number(value))
  26. {
  27. }
  28. explicit MathematicalValue(Crypto::SignedBigInteger value)
  29. : m_value(move(value))
  30. {
  31. }
  32. explicit MathematicalValue(Symbol symbol)
  33. : m_value(symbol)
  34. {
  35. }
  36. MathematicalValue(Value value)
  37. : m_value(value.is_number()
  38. ? value_from_number(value.as_double())
  39. : ValueType(value.as_bigint().big_integer()))
  40. {
  41. }
  42. bool is_number() const;
  43. double as_number() const;
  44. bool is_bigint() const;
  45. Crypto::SignedBigInteger const& as_bigint() const;
  46. bool is_mathematical_value() const;
  47. bool is_positive_infinity() const;
  48. bool is_negative_infinity() const;
  49. bool is_negative_zero() const;
  50. bool is_nan() const;
  51. void negate();
  52. MathematicalValue plus(Checked<i32> addition) const;
  53. MathematicalValue plus(MathematicalValue const& addition) const;
  54. MathematicalValue minus(Checked<i32> subtraction) const;
  55. MathematicalValue minus(MathematicalValue const& subtraction) const;
  56. MathematicalValue multiplied_by(Checked<i32> multiplier) const;
  57. MathematicalValue multiplied_by(MathematicalValue const& multiplier) const;
  58. MathematicalValue divided_by(Checked<i32> divisor) const;
  59. MathematicalValue divided_by(MathematicalValue const& divisor) const;
  60. MathematicalValue multiplied_by_power(Checked<i32> exponent) const;
  61. MathematicalValue divided_by_power(Checked<i32> exponent) const;
  62. bool modulo_is_zero(Checked<i32> mod) const;
  63. int logarithmic_floor() const;
  64. bool is_equal_to(MathematicalValue const&) const;
  65. bool is_less_than(MathematicalValue const&) const;
  66. bool is_negative() const;
  67. bool is_positive() const;
  68. bool is_zero() const;
  69. String to_string() const;
  70. Value to_value(VM&) const;
  71. private:
  72. using ValueType = Variant<double, Crypto::SignedBigInteger, Symbol>;
  73. static ValueType value_from_number(double number);
  74. ValueType m_value { 0.0 };
  75. };
  76. }