OpenLB 1.7
Loading...
Searching...
No Matches
fraction.h
Go to the documentation of this file.
1/* This file is part of the OpenLB library
2 *
3 * Copyright (C) 2019 Adrian Kummerlaender
4 * E-mail contact: info@openlb.net
5 * The most recent release of OpenLB can be downloaded at
6 * <http://www.openlb.net/>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public
19 * License along with this program; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22*/
23
24#ifndef UTILITIES_FRACTION_H
25#define UTILITIES_FRACTION_H
26
27#include <stdexcept>
28
29namespace olb {
30
31namespace utilities {
32
34class Fraction {
35private:
36 const int _numerator;
37 const int _denominator;
38
39public:
40 constexpr Fraction(int num, int denum):
41 _numerator(num), _denominator(denum)
42 {
43 if (_denominator == 0) {
44 throw std::invalid_argument("denominator must not be zero");
45 }
46 }
47
48 constexpr Fraction(int parts[2]):
49 Fraction(parts[0], parts[1]) { }
50
51 constexpr Fraction(int num):
52 Fraction(num, 1) { }
53
54 constexpr Fraction():
55 Fraction(0) { }
56
57 template <typename T>
58 constexpr T as() const
59 {
60 return T(_numerator) / T(_denominator);
61 }
62
63 template <typename T>
64 constexpr T inverseAs() const
65 {
66 return _numerator != 0 ? T(_denominator) / T(_numerator) : throw std::invalid_argument("inverse of zero is undefined");
67 }
68};
69
70}
71
72}
73
74#endif
Floating-point independent fraction type.
Definition fraction.h:34
constexpr Fraction(int parts[2])
Definition fraction.h:48
constexpr Fraction(int num)
Definition fraction.h:51
constexpr T as() const
Definition fraction.h:58
constexpr Fraction(int num, int denum)
Definition fraction.h:40
constexpr T inverseAs() const
Definition fraction.h:64
Top level namespace for all of OpenLB.