Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
src
Math
Rational.cpp
Go to the documentation of this file.
1
/* This file is part of MoDELib, distributed under GNU GPL v2. */
2
#include <
Rational.h
>
3
4
namespace
model
5
{
6
7
auto
Rational::gcd(
const
size_t
&
a
,
const
size_t
&
b
) ->
int
8
{
9
return
b
> 0 ? gcd(
b
,
a
%
b
) :
a
;
10
}
11
12
auto
Rational::sgn(
const
long
int
&
a
) ->
int
13
{
14
return
a
< 0 ? -1 : 1;
15
}
16
17
Rational::Rational
() :
n
(0),
d
(1)
18
{
19
}
20
21
Rational::Rational
(
const
long
int
&n_in) :
n
(n_in),
d
(1)
22
{
23
}
24
25
Rational::Rational
(
const
long
int
&n_in,
const
long
int
&d_in)
26
:
n
(sgn(d_in) * n_in / gcd(abs(n_in), abs(d_in))),
d
(sgn(d_in) * d_in / gcd(abs(n_in), abs(d_in)))
27
{
28
}
29
30
auto
Rational::asDouble
() const ->
double
31
{
32
return
double(
n
) / double(
d
);
33
}
34
35
auto
Rational::operator==
(
const
long
int
&other)
const
->
bool
36
{
37
switch
(d)
38
{
39
case
0:
40
return
false
;
41
break
;
42
43
case
1:
44
return
n == other;
45
break
;
46
47
default
:
48
return
n == 0 && other == 0;
49
break
;
50
}
51
}
52
53
auto
Rational::operator!=
(
const
long
int
&other)
const
->
bool
54
{
55
return
!(*
this
== other);
56
}
57
58
auto
Rational::operator*
(
const
Rational
&r2)
const
->
Rational
59
{
60
return
Rational
(n * r2.n, d * r2.d);
61
}
62
63
auto
Rational::operator/
(
const
Rational
&r2)
const
->
Rational
64
{
65
return
Rational
(n * r2.d, d * r2.n);
66
}
67
68
auto
Rational::operator+
(
const
Rational
&r2)
const
->
Rational
69
{
70
return
Rational
(n * r2.d + r2.n * d, d * r2.d);
71
}
72
73
auto
Rational::operator-
(
const
Rational
&r2)
const
->
Rational
74
{
75
return
Rational
(n * r2.d - r2.n * d, d * r2.d);
76
}
77
78
auto
Rational::operator*
(
const
long
int
&
i
)
const
->
Rational
79
{
80
return
Rational
(n *
i
, d);
81
}
82
83
auto
Rational::operator/
(
const
long
int
&
i
)
const
->
Rational
84
{
85
return
Rational
(n, d *
i
);
86
}
87
88
auto
Rational::operator+
(
const
long
int
&
i
)
const
->
Rational
89
{
90
return
*
this
+
Rational
(
i
, 1);
91
}
92
93
auto
Rational::operator-
(
const
long
int
&
i
)
const
->
Rational
94
{
95
return
*
this
-
Rational
(
i
, 1);
96
}
97
98
std::ostream &
operator<<
(std::ostream &os,
const
Rational
&r)
99
{
100
os << r.n <<
"/"
<< r.d;
101
return
os;
102
}
103
}
// namespace model
Rational.h
model::Rational
Definition
Rational.h:18
model::Rational::d
long int d
Definition
Rational.h:28
model::Rational::operator==
bool operator==(const long int &other) const
model::Rational::Rational
Rational()
Definition
Rational.cpp:17
model::Rational::n
long int n
Definition
Rational.h:27
model::Rational::operator-
Rational operator-(const Rational &r2) const
model::Rational::operator*
Rational operator*(const Rational &r2) const
model::Rational::operator+
Rational operator+(const Rational &r2) const
model::Rational::asDouble
double asDouble() const
model::Rational::operator!=
bool operator!=(const long int &other) const
model::Rational::operator/
Rational operator/(const Rational &r2) const
a
a
Definition
int_R.m:14
b
b
Definition
int_R.m:16
model
Definition
DD_converter.dox:2
model::operator<<
T & operator<<(T &os, const PeriodicPlanePatchIO< dim > &ds)
Definition
PeriodicGlidePlane.h:289
i
int i
Definition
triangle.cpp:3300
Generated by
1.15.0