ARTS  2.3.1285(git:92a29ea9-dirty)
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) {
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) {
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 }
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:39
#define RATIONAL_UNDEFINED
Definition: rational.h:361
Index nelem() const
Number of elements.
Definition: array.h:195
constexpr Index Nom() const
Nominator.
Definition: rational.h:85
constexpr Rational(const Index nom=0, const Index denom=1)
Initialization call.
Definition: rational.h:61
Index mnom
Definition: rational.h:306
constexpr Rational reduce_by_gcd(const Rational a)
Returns the rational reduced by the greates.
Definition: rational.h:315
Index mdenom
Definition: rational.h:307
constexpr Rational & fixSign()
Makes the sign of mdenom positive.
Definition: rational.h:296
std::istream & operator>>(std::istream &is, Rational &a)
Input operator.
Definition: rational.cc:44
Implements rational numbers to work with other ARTS types.
Definition: rational.h:54
constexpr Index Denom() const
Denominator.
Definition: rational.h:88
basic_ostringstream< char, string_char_traits< char >, alloc > ostringstream
Definition: sstream.h:204
This can be used to make arrays out of anything.
Definition: array.h:40
constexpr Rational numeric2rational(Numeric x, size_t maxdec=4)
Rational from Numeric.
Definition: rational.h:330
Index nelem(const Lines &l)
Number of lines.
Contains the rational class definition.
void simplify_in_place()
Simplify by reducing the values locally.
Definition: rational.cc:104
std::ostream & operator<<(std::ostream &os, const Rational &a)
Output operator.
Definition: rational.cc:33
This file contains the definition of String, the ARTS string class.