ARTS  2.4.0(git:4fb77825)
rational.cc
Go to the documentation of this file.
1 /* Copyright (C) 2012
2 Richard Larsson <ric.larsson@gmail.com>
3 
4 This program is free software; you can redistribute it and/or modify it
5 under the terms of the GNU General Public License as published by the
6 Free Software Foundation; either version 2, or (at your option) any
7 later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 USA. */
18 
27 #include "rational.h"
28 
29 #include <ostream>
30 #include <stdexcept>
31 #include "mystring.h"
32 
33 std::ostream& operator<<(std::ostream& os, const Rational& a) {
34  Rational r = reduce_by_gcd(a);
35  r.fixSign();
36 
37  if (r.Denom() == 1)
38  os << r.Nom();
39  else
40  os << r.Nom() << "/" << r.Denom();
41  return os;
42 }
43 
44 std::istream& operator>>(std::istream& is, Rational& a) {
45  String s;
46  Index nom;
47  Index denom;
48  char* endptr;
49 
50  is >> s;
51 
52  try {
53  ArrayOfString as;
54 
55  s.split(as, "/");
56 
57  if (as.nelem() == 1) {
58  nom = strtol(s.c_str(), &endptr, 10);
59  if (endptr != s.c_str() + s.nelem())
60  throw std::runtime_error("Error parsing rational number");
61  a = Rational(nom, 1);
62  } else if (as.nelem() == 2) {
63  nom = strtol(as[0].c_str(), &endptr, 10);
64  if (endptr != as[0].c_str() + as[0].nelem())
65  throw std::runtime_error("Error parsing rational number nominator");
66  denom = strtol(as[1].c_str(), &endptr, 10);
67  if (endptr != as[1].c_str() + as[1].nelem())
68  throw std::runtime_error("Error parsing rational number denominator");
69  a = Rational(nom, denom);
70  } else
71  throw std::runtime_error("Error parsing rational number");
72  } catch (const std::runtime_error& e) {
73  std::ostringstream os;
74  os << "Error parsing rational number: " << s << std::endl;
75  os << e.what();
76  throw std::runtime_error(os.str());
77  }
78 
79  return is;
80 }
81 
82 
84 {
85  auto len = s.length();
86 
87  if (len) {
88  auto dot_pos = s.find(".");
89  auto slash_pos = s.find("/");
90  if (len > dot_pos) {
91  *this = numeric2rational(std::stod(s), len - dot_pos - 1);
92  } else if (len > slash_pos) {
93  *this = Rational(std::stoi(s.substr(0, slash_pos)),
94  std::stoi(s.substr(slash_pos + 1, len)));
95  } else {
96  *this = Rational(std::stoi(s));
97  }
98  } else {
99  *this = RATIONAL_UNDEFINED;
100  }
101 }
102 
103 
105 {
106  Rational a = reduce_by_gcd(*this);
107  mnom = a.Nom();
108  mdenom = a.Denom();
109  fixSign();
110 }
RATIONAL_UNDEFINED
#define RATIONAL_UNDEFINED
Definition: rational.h:361
numeric2rational
constexpr Rational numeric2rational(Numeric x, size_t maxdec=4)
Rational from Numeric.
Definition: rational.h:330
rational.h
Contains the rational class definition.
Rational::fixSign
constexpr Rational & fixSign()
Makes the sign of mdenom positive.
Definition: rational.h:296
reduce_by_gcd
constexpr Rational reduce_by_gcd(const Rational a)
Returns the rational reduced by the greates.
Definition: rational.h:315
Rational::Nom
constexpr Index Nom() const
Nominator.
Definition: rational.h:85
Array< String >
Absorption::nelem
Index nelem(const Lines &l)
Number of lines.
Definition: absorptionlines.h:1820
Rational::simplify_in_place
void simplify_in_place()
Simplify by reducing the values locally.
Definition: rational.cc:104
my_basic_string< char >
ARTS::Group::Rational
Rational Rational
Definition: autoarts.h:96
Rational::mdenom
Index mdenom
Definition: rational.h:307
operator>>
std::istream & operator>>(std::istream &is, Rational &a)
Input operator.
Definition: rational.cc:44
Rational::mnom
Index mnom
Definition: rational.h:306
Rational::Denom
constexpr Index Denom() const
Denominator.
Definition: rational.h:88
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
operator<<
std::ostream & operator<<(std::ostream &os, const Rational &a)
Output operator.
Definition: rational.cc:33
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:195
Rational::Rational
constexpr Rational(const Index nom=0, const Index denom=1)
Initialization call.
Definition: rational.h:61
Rational
Implements rational numbers to work with other ARTS types.
Definition: rational.h:54
mystring.h
This file contains the definition of String, the ARTS string class.