17
17
namespace Fortran ::evaluate::value {
18
18
19
19
template <typename R>
20
- ValueWithRealFlags<Complex<R>> Complex<R>::Add(const Complex &that) const {
20
+ ValueWithRealFlags<Complex<R>> Complex<R>::Add(
21
+ const Complex &that, Rounding rounding) const {
21
22
RealFlags flags;
22
- Part reSum{re_.Add (that.re_ ).AccumulateFlags (flags)};
23
- Part imSum{im_.Add (that.im_ ).AccumulateFlags (flags)};
23
+ Part reSum{re_.Add (that.re_ , rounding ).AccumulateFlags (flags)};
24
+ Part imSum{im_.Add (that.im_ , rounding ).AccumulateFlags (flags)};
24
25
return {Complex{reSum, imSum}, flags};
25
26
}
26
27
27
28
template <typename R>
28
- ValueWithRealFlags<Complex<R>> Complex<R>::Subtract(const Complex &that) const {
29
+ ValueWithRealFlags<Complex<R>> Complex<R>::Subtract(
30
+ const Complex &that, Rounding rounding) const {
29
31
RealFlags flags;
30
- Part reDiff{re_.Subtract (that.re_ ).AccumulateFlags (flags)};
31
- Part imDiff{im_.Subtract (that.im_ ).AccumulateFlags (flags)};
32
+ Part reDiff{re_.Subtract (that.re_ , rounding ).AccumulateFlags (flags)};
33
+ Part imDiff{im_.Subtract (that.im_ , rounding ).AccumulateFlags (flags)};
32
34
return {Complex{reDiff, imDiff}, flags};
33
35
}
34
36
35
37
template <typename R>
36
- ValueWithRealFlags<Complex<R>> Complex<R>::Multiply(const Complex &that) const {
38
+ ValueWithRealFlags<Complex<R>> Complex<R>::Multiply(
39
+ const Complex &that, Rounding rounding) const {
37
40
// (a + ib)*(c + id) -> ac - bd + i(ad + bc)
38
41
RealFlags flags;
39
- Part ac{re_.Multiply (that.re_ ).AccumulateFlags (flags)};
40
- Part bd{im_.Multiply (that.im_ ).AccumulateFlags (flags)};
41
- Part ad{re_.Multiply (that.im_ ).AccumulateFlags (flags)};
42
- Part bc{im_.Multiply (that.re_ ).AccumulateFlags (flags)};
43
- Part acbd{ac.Subtract (bd).AccumulateFlags (flags)};
44
- Part adbc{ad.Add (bc).AccumulateFlags (flags)};
42
+ Part ac{re_.Multiply (that.re_ , rounding ).AccumulateFlags (flags)};
43
+ Part bd{im_.Multiply (that.im_ , rounding ).AccumulateFlags (flags)};
44
+ Part ad{re_.Multiply (that.im_ , rounding ).AccumulateFlags (flags)};
45
+ Part bc{im_.Multiply (that.re_ , rounding ).AccumulateFlags (flags)};
46
+ Part acbd{ac.Subtract (bd, rounding ).AccumulateFlags (flags)};
47
+ Part adbc{ad.Add (bc, rounding ).AccumulateFlags (flags)};
45
48
return {Complex{acbd, adbc}, flags};
46
49
}
47
50
48
51
template <typename R>
49
- ValueWithRealFlags<Complex<R>> Complex<R>::Divide(const Complex &that) const {
52
+ ValueWithRealFlags<Complex<R>> Complex<R>::Divide(
53
+ const Complex &that, Rounding rounding) const {
50
54
// (a + ib)/(c + id) -> [(a+ib)*(c-id)] / [(c+id)*(c-id)]
51
55
// -> [ac+bd+i(bc-ad)] / (cc+dd)
52
56
// -> ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd))
@@ -55,30 +59,30 @@ ValueWithRealFlags<Complex<R>> Complex<R>::Divide(const Complex &that) const {
55
59
RealFlags flags;
56
60
bool cGEd{that.re_ .ABS ().Compare (that.im_ .ABS ()) != Relation::Less};
57
61
if (cGEd) {
58
- scale = that.im_ .Divide (that.re_ ).AccumulateFlags (flags);
62
+ scale = that.im_ .Divide (that.re_ , rounding ).AccumulateFlags (flags);
59
63
} else {
60
- scale = that.re_ .Divide (that.im_ ).AccumulateFlags (flags);
64
+ scale = that.re_ .Divide (that.im_ , rounding ).AccumulateFlags (flags);
61
65
}
62
66
Part den;
63
67
if (cGEd) {
64
- Part dS{scale.Multiply (that.im_ ).AccumulateFlags (flags)};
65
- den = dS.Add (that.re_ ).AccumulateFlags (flags);
68
+ Part dS{scale.Multiply (that.im_ , rounding ).AccumulateFlags (flags)};
69
+ den = dS.Add (that.re_ , rounding ).AccumulateFlags (flags);
66
70
} else {
67
- Part cS{scale.Multiply (that.re_ ).AccumulateFlags (flags)};
68
- den = cS.Add (that.im_ ).AccumulateFlags (flags);
71
+ Part cS{scale.Multiply (that.re_ , rounding ).AccumulateFlags (flags)};
72
+ den = cS.Add (that.im_ , rounding ).AccumulateFlags (flags);
69
73
}
70
- Part aS{scale.Multiply (re_).AccumulateFlags (flags)};
71
- Part bS{scale.Multiply (im_).AccumulateFlags (flags)};
74
+ Part aS{scale.Multiply (re_, rounding ).AccumulateFlags (flags)};
75
+ Part bS{scale.Multiply (im_, rounding ).AccumulateFlags (flags)};
72
76
Part re1, im1;
73
77
if (cGEd) {
74
- re1 = re_.Add (bS).AccumulateFlags (flags);
75
- im1 = im_.Subtract (aS).AccumulateFlags (flags);
78
+ re1 = re_.Add (bS, rounding ).AccumulateFlags (flags);
79
+ im1 = im_.Subtract (aS, rounding ).AccumulateFlags (flags);
76
80
} else {
77
- re1 = aS.Add (im_).AccumulateFlags (flags);
78
- im1 = bS.Subtract (re_).AccumulateFlags (flags);
81
+ re1 = aS.Add (im_, rounding ).AccumulateFlags (flags);
82
+ im1 = bS.Subtract (re_, rounding ).AccumulateFlags (flags);
79
83
}
80
- Part re{re1.Divide (den).AccumulateFlags (flags)};
81
- Part im{im1.Divide (den).AccumulateFlags (flags)};
84
+ Part re{re1.Divide (den, rounding ).AccumulateFlags (flags)};
85
+ Part im{im1.Divide (den, rounding ).AccumulateFlags (flags)};
82
86
return {Complex{re, im}, flags};
83
87
}
84
88
0 commit comments