rational.h 5.26 KB
Newer Older
Adrien Béraud's avatar
Adrien Béraud committed
1
/*
Guillaume Roguez's avatar
Guillaume Roguez committed
2
 *  Copyright (C) 2015-2018 Savoir-faire Linux Inc.
Guillaume Roguez's avatar
Guillaume Roguez committed
3
 *
Adrien Béraud's avatar
Adrien Béraud committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
Guillaume Roguez's avatar
Guillaume Roguez committed
18
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
Adrien Béraud's avatar
Adrien Béraud committed
19 20 21 22
 */

#pragma once

Adrien Béraud's avatar
Adrien Béraud committed
23
#include <utility> // std::swap
24
#include <cstdlib> // std::abs
Adrien Béraud's avatar
Adrien Béraud committed
25
#include <iostream>
26

27 28 29 30
extern "C" {
#include <libavutil/rational.h> // specify conversions for AVRational
}

Adrien Béraud's avatar
Adrien Béraud committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
namespace ring {

/**
 * Naive implementation of the boost::rational interface, described here:
 * http://www.boost.org/doc/libs/1_57_0/libs/rational/rational.html
 * No form of normalisation or overflow prevention is implemented.
 */
template<typename I>
class rational {
public:
    // Constructors
    rational() {};          // Zero
    rational(I n) : num_(n) {};       // Equal to n/1
    rational(I n, I d) : num_(n), den_(d) {};  // General case (n/d)

46 47 48 49
    // Define conversions to and from AVRational (equivalent)
    rational(AVRational r) : num_(r.num), den_(r.den) {};
    operator AVRational() { return AVRational{num_, den_}; }

Adrien Béraud's avatar
Adrien Béraud committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    // Normal copy constructors and assignment operators

    // Assignment from I
    rational& operator=(I n) { num_ = n; den_ = 1; return *this; }

    // Assign in place
    rational& assign(I n, I d) { num_ = n; den_ = d; return *this; }

    // Representation
    I numerator() const { return num_; };
    I denominator() const { return den_; };
    template<typename R=double> R real() const { return num_/(R)den_; }

    // In addition to the following operators, all of the "obvious" derived
    // operators are available - see operators.hpp

    // Arithmetic operators
    rational operator+ (const rational& r) {
        return {num_*r.den_ + r.num_*den_, den_*r.den_};
    }
    rational operator- (const rational& r) {
        return {num_*r.den_ - r.num_*den_, den_*r.den_};
    }
    rational operator* (const rational& r) {
        return {num_*r.num_, den_*r.den_};
    }
    rational operator/ (const rational& r) {
        return {num_*r.den_, den_*r.num_};
    }

    rational& operator+= (const rational& r) {
        std::swap(*this, *this + r);
        return *this;
    }
    rational& operator-= (const rational& r) {
        std::swap(*this, *this - r);
        return *this;
    }
    rational& operator*= (const rational& r) {
        num_ *= r.num_;
        den_ *= r.den_;
        return *this;
    }
    rational& operator/= (const rational& r) {
        num_ *= r.den_;
        den_ *= r.num_;
        return *this;
    }

    // Arithmetic with integers
    rational& operator+= (I i) { num_ += i * den_; return *this; }
    rational& operator-= (I i) { num_ -= i * den_; return *this; }
    rational& operator*= (I i) { num_ *= i; return *this; }
    rational& operator/= (I i) { den_ *= i; return *this; };

    // Increment and decrement
    const rational& operator++() { num_ += den_; return *this; }
    const rational& operator--() { num_ -= den_; return *this; }

    // Operator not
    bool operator!() const { return !num_; };

    // Boolean conversion
113
    explicit operator bool() const { return num_; }
Adrien Béraud's avatar
Adrien Béraud committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

    // Comparison operators
    bool operator< (const rational& r) const {
        bool inv = (den_ > 0) != (r.den_ > 0);
        return inv != (num_ * r.den_ < den_ * r.num_);
    }
    bool operator== (const rational& r) const { return num_ * r.den_ == den_ * r.num_; }

    // Comparison with integers
    bool operator< (I i) const { return num_ < i * den_; }
    bool operator> (I i) const { return num_ > i * den_; }
    bool operator== (I i) const { return num_ == i * den_; }
private:
    I num_ {0};
    I den_ {1};
};

// Unary operators
template <typename I> rational<I> operator+ (const rational<I>& r) { return r; }
template <typename I> rational<I> operator- (const rational<I>& r) { return {-r.numerator(), r.denominator()}; };

// Reversed order operators for - and / between (types convertible to) I and rational
template <typename I, typename II> inline rational<I> operator- (II i, const rational<I>& r);
137 138 139
template <typename I, typename II> inline rational<I> operator/ (II i, const rational<I>& r) {
    return { i * r.denominator(), r.numerator() };
}
Adrien Béraud's avatar
Adrien Béraud committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

// Absolute value
template <typename I> rational<I> abs (const rational<I>& r) {
    return { std::abs(r.numerator()), std::abs(r.denominator()) };
}

// Input and output
template <typename I> std::istream& operator>> (std::istream& is, rational<I>& r) {
    char sep;
    is >> r.num_ >> sep >> r.den_;
    return is;
}

template <typename I> std::ostream& operator<< (std::ostream& os, const rational<I>& r) {
    os << r.numerator() << '/' << r.denominator();
    return os;
}

// Type conversion
template <typename T, typename I> T rational_cast (const rational<I>& r);

}