ARTS 2.5.4 (git: 31ce4f0e)
rational.cc
Go to the documentation of this file.
1/* Copyright (C) 2012
2Richard Larsson <ric.larsson@gmail.com>
3
4This program is free software; you can redistribute it and/or modify it
5under the terms of the GNU General Public License as published by the
6Free Software Foundation; either version 2, or (at your option) any
7later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17USA. */
18
27#include "rational.h"
28
29#include "debug.h"
30#include "mystring.h"
31#include <ostream>
32#include <stdexcept>
33
34std::ostream& operator<<(std::ostream& os, const Rational& a) {
36 r.fixSign();
37
38 if (r.denom == 1)
39 os << r.numer;
40 else
41 os << r.numer << "/" << r.denom;
42 return os;
43}
44
45std::istream& operator>>(std::istream& is, Rational& a) {
46 String s;
47
48 is >> s;
49 a = Rational(s);
50
51 ARTS_USER_ERROR_IF(a.isUndefined(), "Cannot read ", s, " as a rational")
52
53 return is;
54}
55
56
58 auto len = s.length();
59
60 if (len) {
61 auto dot_pos = s.find(".");
62 auto slash_pos = s.find("/");
63 if (len > dot_pos) {
64 *this = numeric2rational(std::stod(s), len - dot_pos - 1);
65 } else if (len > slash_pos) {
66 const String a{s.substr(0, slash_pos)};
67 const String b{s.substr(slash_pos + 1, len)};
68 try {
69 *this = Rational(std::stoi(a), std::stoi(b));
70 } catch (...) {
71 ARTS_USER_ERROR("Cannot interpret either '", a, "' or '", b, "' as an integer (or both)");
72 }
73 } else {
74 try {
75 *this = Rational(std::stoi(s));
76 } catch (...) {
77 ARTS_USER_ERROR("Cannot interpret '", s, "' as an integer");
78 }
79 }
80 } else {
81 *this = RATIONAL_UNDEFINED;
82 }
83}
84
85
87 Rational a = reduce_by_gcd(*this);
88 numer = a.numer;
89 denom = a.denom;
90 fixSign();
91}
Helper macros for debugging.
#define ARTS_USER_ERROR(...)
Definition: debug.h:150
#define ARTS_USER_ERROR_IF(condition,...)
Definition: debug.h:134
This file contains the definition of String, the ARTS string class.
std::ostream & operator<<(std::ostream &os, const Rational &a)
Definition: rational.cc:34
std::istream & operator>>(std::istream &is, Rational &a)
Definition: rational.cc:45
Contains the rational class definition.
#define RATIONAL_UNDEFINED
Definition: rational.h:357
constexpr Rational numeric2rational(Numeric x, size_t maxdec=4) noexcept
Rational from Numeric.
Definition: rational.h:329
constexpr Rational reduce_by_gcd(Rational a) noexcept
Returns the rational reduced by the greates.
Definition: rational.h:315
Implements rational numbers to work with other ARTS types.
Definition: rational.h:43
constexpr Rational(const Index n=0, const Index d=1) noexcept
Initialization call.
Definition: rational.h:52
void simplify_in_place() noexcept
Simplify by reducing the values locally.
Definition: rational.cc:86
Index numer
Definition: rational.h:44
Index denom
Definition: rational.h:45
constexpr Rational & fixSign() noexcept
Makes the sign of denom positive.
Definition: rational.h:297
#define a
#define b