aboutsummaryrefslogtreecommitdiff
path: root/util/simple_fraction.h
blob: 2df45e24ca4949a4fd63063f7451501374859a88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef UTIL_SIMPLE_FRACTION_H_
#define UTIL_SIMPLE_FRACTION_H_

#include <cmath>
#include <limits>
#include <string>

#include "absl/strings/string_view.h"
#include "platform/base/error.h"

namespace openscreen {

// SimpleFraction is used to represent simple (or "common") fractions, composed
// of a rational number written a/b where a and b are both integers.
// Some helpful notes on SimpleFraction assumptions/limitations:
// 1. SimpleFraction does not perform reductions. 2/4 != 1/2, and -1/-1 != 1/1.
// 2. denominator = 0 is considered undefined.
// 3. numerator = saturates range to int min or int max
// 4. A SimpleFraction is "positive" if and only if it is defined and at least
//    equal to zero. Since reductions are not performed, -1/-1 is negative.
class SimpleFraction {
 public:
  static ErrorOr<SimpleFraction> FromString(absl::string_view value);
  std::string ToString() const;

  constexpr SimpleFraction() = default;
  constexpr SimpleFraction(int numerator)  // NOLINT
      : numerator_(numerator) {}
  constexpr SimpleFraction(int numerator, int denominator)
      : numerator_(numerator), denominator_(denominator) {}

  constexpr SimpleFraction(const SimpleFraction&) = default;
  constexpr SimpleFraction(SimpleFraction&&) noexcept = default;
  constexpr SimpleFraction& operator=(const SimpleFraction&) = default;
  constexpr SimpleFraction& operator=(SimpleFraction&&) = default;
  ~SimpleFraction() = default;

  constexpr bool operator==(const SimpleFraction& other) const {
    return numerator_ == other.numerator_ && denominator_ == other.denominator_;
  }

  constexpr bool operator!=(const SimpleFraction& other) const {
    return !(*this == other);
  }

  constexpr bool is_defined() const { return denominator_ != 0; }

  constexpr bool is_positive() const {
    return (numerator_ >= 0) && (denominator_ > 0);
  }

  constexpr explicit operator double() const {
    if (denominator_ == 0) {
      return nan("");
    }
    return static_cast<double>(numerator_) / static_cast<double>(denominator_);
  }

  constexpr int numerator() const { return numerator_; }
  constexpr int denominator() const { return denominator_; }

 private:
  int numerator_ = 0;
  int denominator_ = 1;
};

}  // namespace openscreen

#endif  // UTIL_SIMPLE_FRACTION_H_