Ratio.cpp 689 B

1234567891011121314151617181920212223242526272829303132333435
  1. /*
  2. * Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Ratio.h"
  7. #include <math.h>
  8. namespace Web::CSS {
  9. Ratio::Ratio(float first, float second)
  10. : m_first_value(first)
  11. , m_second_value(second)
  12. {
  13. }
  14. // https://www.w3.org/TR/css-values-4/#degenerate-ratio
  15. bool Ratio::is_degenerate() const
  16. {
  17. return !isfinite(m_first_value) || m_first_value == 0
  18. || !isfinite(m_second_value) || m_second_value == 0;
  19. }
  20. String Ratio::to_string() const
  21. {
  22. return String::formatted("{} / {}", m_first_value, m_second_value);
  23. }
  24. auto Ratio::operator<=>(const Ratio& other) const
  25. {
  26. return value() - other.value();
  27. }
  28. }