MathematicalValue.h 2.6 KB

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