ARTS  2.2.66
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 
26 #include "rational.h"
27 
28 #include <stdexcept>
29 #include "mystring.h"
30 
31 
33 {
34  if (mdenom != 1)
35  {
36  std::ostringstream os;
37  os << "Cannot convert Rational " << *this << " to Index.";
38  throw std::runtime_error(os.str());
39  }
40  return mnom;
41 }
42 
43 
45 {
46  Index ii = mdenom;
47 
48  while( ii != 0 )
49  {
50  if( mnom % ii == 0 && mdenom % ii == 0 )
51  {
52  mnom /= ii;
53  mdenom /= ii;
54 
55  if( mnom == 1 || mdenom == 1 || mnom == 0 || mnom == -1 )
56  break;
57  }
58  if( mdenom < ii )
59  ii = mdenom;
60  else
61  ii--;
62  }
63 }
64 
65 std::ostream& operator<<(std::ostream& os, const Rational& a)
66 {
67  os << a.Nom() << "/" << a.Denom();
68  return os;
69 }
70 
71 std::istream& operator>>(std::istream& is, Rational& a)
72 {
73  String s;
74  Index nom;
75  Index denom;
76  char* endptr;
77 
78  is >> s;
79 
80  ArrayOfString as;
81 
82  s.split(as, "/");
83 
84  if (as.nelem() == 1)
85  {
86  nom=strtol(s.c_str(), &endptr, 10);
87  if (endptr != s.c_str()+s.nelem())
88  throw std::runtime_error("Error parsing quantum number");
89  a = Rational(nom, 1);
90  }
91  else if (as.nelem() == 2)
92  {
93  nom=strtol(as[0].c_str(), &endptr, 10);
94  if (endptr != as[0].c_str()+as[0].nelem())
95  throw std::runtime_error("Error parsing quantum number nominator");
96  denom=strtol(as[1].c_str(), &endptr, 10);
97  if (endptr != as[1].c_str()+as[1].nelem())
98  throw std::runtime_error("Error parsing quantum number denominator");
99  a = Rational(nom, denom);
100  }
101  else
102  throw std::runtime_error("Error parsing quantum number");
103 
104  return is;
105 }
106 
108 {
109  return a<Rational(0)?-a:a;
110 }
rational.h
Contains the rational class definition.
Rational::Denom
Index Denom() const
Definition: rational.h:46
abs
Rational abs(const Rational &a)
Definition: rational.cc:107
Array
This can be used to make arrays out of anything.
Definition: array.h:107
Rational::toIndex
Index toIndex() const
Definition: rational.cc:32
my_basic_string
The implementation for String, the ARTS string class.
Definition: mystring.h:64
Rational::mdenom
Index mdenom
Definition: rational.h:72
operator>>
std::istream & operator>>(std::istream &is, Rational &a)
Definition: rational.cc:71
Rational::mnom
Index mnom
Definition: rational.h:71
Rational::Simplify
void Simplify()
Definition: rational.cc:44
Index
INDEX Index
The type to use for all integer numbers and indices.
Definition: matpack.h:35
operator<<
std::ostream & operator<<(std::ostream &os, const Rational &a)
Definition: rational.cc:65
Rational::Nom
Index Nom() const
Definition: rational.h:45
Array::nelem
Index nelem() const
Number of elements.
Definition: array.h:176
Rational
Definition: rational.h:35
mystring.h
This file contains the definition of String, the ARTS string class.