MathematicalValue.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/Value.h>
  12. namespace JS::Intl {
  13. // https://tc39.es/proposal-intl-numberformat-v3/out/numberformat/proposed.html#intl-mathematical-value
  14. class MathematicalValue {
  15. public:
  16. enum class Symbol {
  17. PositiveInfinity,
  18. NegativeInfinity,
  19. NegativeZero,
  20. NotANumber,
  21. };
  22. MathematicalValue() = default;
  23. explicit MathematicalValue(double value)
  24. : m_value(value_from_number(value))
  25. {
  26. }
  27. explicit MathematicalValue(Crypto::SignedBigInteger value)
  28. : m_value(move(value))
  29. {
  30. }
  31. explicit MathematicalValue(Symbol symbol)
  32. : m_value(symbol)
  33. {
  34. }
  35. MathematicalValue(Value value)
  36. : m_value(value.is_number()
  37. ? value_from_number(value.as_double())
  38. : ValueType(value.as_bigint().big_integer()))
  39. {
  40. }
  41. bool is_number() const;
  42. double as_number() const;
  43. bool is_bigint() const;
  44. Crypto::SignedBigInteger const& as_bigint() const;
  45. bool is_mathematical_value() const;
  46. bool is_positive_infinity() const;
  47. bool is_negative_infinity() const;
  48. bool is_negative_zero() const;
  49. bool is_nan() const;
  50. void negate();
  51. MathematicalValue plus(Checked<i32> addition) const;
  52. MathematicalValue plus(MathematicalValue const& addition) const;
  53. MathematicalValue minus(Checked<i32> subtraction) const;
  54. MathematicalValue minus(MathematicalValue const& subtraction) const;
  55. MathematicalValue multiplied_by(Checked<i32> multiplier) const;
  56. MathematicalValue multiplied_by(MathematicalValue const& multiplier) const;
  57. MathematicalValue divided_by(Checked<i32> divisor) const;
  58. MathematicalValue divided_by(MathematicalValue const& divisor) const;
  59. MathematicalValue multiplied_by_power(Checked<i32> exponent) const;
  60. MathematicalValue divided_by_power(Checked<i32> exponent) const;
  61. bool modulo_is_zero(Checked<i32> mod) const;
  62. int logarithmic_floor() const;
  63. bool is_equal_to(MathematicalValue const&) const;
  64. bool is_less_than(MathematicalValue const&) const;
  65. bool is_negative() const;
  66. bool is_positive() const;
  67. bool is_zero() const;
  68. ThrowCompletionOr<String> to_string(VM&) const;
  69. Value to_value(VM&) const;
  70. private:
  71. using ValueType = Variant<double, Crypto::SignedBigInteger, Symbol>;
  72. static ValueType value_from_number(double number);
  73. ValueType m_value { 0.0 };
  74. };
  75. }