Skip to content

Commit a5faa8d

Browse files
committed
Add proj overload and fix const correctness
1 parent 8ee77ce commit a5faa8d

2 files changed

Lines changed: 46 additions & 11 deletions

File tree

include/boost/multiprecision/complex.hpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,34 @@ class complex
8989
return *this;
9090
}
9191

92-
BOOST_MP_CXX14_CONSTEXPR bool operator==(const complex& rhs) noexcept
92+
BOOST_MP_CXX14_CONSTEXPR bool operator==(const complex& rhs) const noexcept
9393
{
9494
return real_ == rhs.real_ && imag_ == rhs.imag_;
9595
}
9696

97-
BOOST_MP_CXX14_CONSTEXPR bool operator!=(const complex& rhs) noexcept
97+
BOOST_MP_CXX14_CONSTEXPR bool operator!=(const complex& rhs) const noexcept
9898
{
9999
return !(*this == rhs);
100100
}
101101

102-
BOOST_MP_CXX14_CONSTEXPR bool operator==(const T& rhs) noexcept
102+
BOOST_MP_CXX14_CONSTEXPR bool operator==(const T& rhs) const noexcept
103103
{
104104
return real_ == rhs && imag_ == T{0};
105105
}
106106

107-
BOOST_MP_CXX14_CONSTEXPR bool operator!=(const T& rhs) noexcept
107+
BOOST_MP_CXX14_CONSTEXPR bool operator!=(const T& rhs) const noexcept
108108
{
109109
return !(*this == rhs);
110110
}
111111

112-
// Writes in the form (real, imag)
113112
template <typename CharT, typename Traits>
114-
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os)
113+
friend std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT, Traits>& os, const complex& z)
115114
{
116115
std::basic_ostringstream<CharT, Traits> s;
117116
s.flags(os.flags());
118117
s.imbue(os.getloc());
119118
s.precision(os.precision());
120-
s << '(' << real_ << ',' << imag_ << ')';
119+
s << '(' << z.real_ << ',' << z.imag_ << ')';
121120

122121
return os << s.str();
123122
}
@@ -127,7 +126,7 @@ class complex
127126
// 2) (real)
128127
// 3) (real, imag)
129128
template <typename CharT, typename Traits>
130-
std::basic_istream<CharT, Traits>& operator>>(std::basic_ostream<CharT, Traits>& is)
129+
friend std::basic_istream<CharT, Traits>& operator>>(std::basic_istream<CharT, Traits>& is, complex& z)
131130
{
132131
CharT ch {};
133132
T real = T{0};
@@ -163,8 +162,8 @@ class complex
163162

164163
if (!is.fail())
165164
{
166-
real_ = real;
167-
imag_ = imag;
165+
z.real_ = real;
166+
z.imag_ = imag;
168167
}
169168

170169
return is;
@@ -215,6 +214,17 @@ inline BOOST_MP_CXX14_CONSTEXPR complex<boost::multiprecision::number<T, ET>> co
215214
return {z.real(), -z.imag()};
216215
}
217216

217+
template <typename T, expression_template_option ET>
218+
inline BOOST_MP_CXX14_CONSTEXPR complex<boost::multiprecision::number<T, ET>> proj(const complex<boost::multiprecision::number<T, ET>>& z) noexcept
219+
{
220+
if (isinf(z.real()) || isinf(z.imag()))
221+
{
222+
return {std::numeric_limits<boost::multiprecision::number<T, ET>>::infinity(), copysign(boost::multiprecision::number<T, ET>{0}, z.imag())};
223+
}
224+
225+
return z;
226+
}
227+
218228
} // Namespace multiprecision
219229
} // Namespace boost
220230

test/test_complex_class.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,27 @@ void test_conj()
195195
complex_scalar lhs {T{1}, T{1}};
196196
complex_scalar rhs {T{1}, T{-1}};
197197

198-
BOOST_TEST(conj(lhs) == rhs);
198+
BOOST_TEST_EQ(conj(lhs), rhs);
199+
}
200+
201+
template <typename T>
202+
void test_proj()
203+
{
204+
using std::complex;
205+
using std::polar;
206+
using std::proj;
207+
using complex_scalar = decltype(polar(T(), T()));
208+
209+
complex_scalar lhs {T{1}, T{1}};
210+
BOOST_TEST_EQ(lhs, proj(lhs));
211+
212+
lhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{1}};
213+
complex_scalar rhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{0}};
214+
BOOST_TEST_EQ(proj(lhs), rhs);
215+
216+
lhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{-1}};
217+
rhs = complex_scalar{T{std::numeric_limits<T>::infinity()}, T{-0}};
218+
BOOST_TEST_EQ(proj(lhs), rhs);
199219
}
200220

201221
int main()
@@ -255,5 +275,10 @@ int main()
255275
test_conj<cpp_bin_float_50>();
256276
test_conj<cpp_dec_float_50>();
257277

278+
test_proj<float>();
279+
test_proj<double>();
280+
test_proj<cpp_bin_float_50>();
281+
test_proj<cpp_dec_float_50>();
282+
258283
return boost::report_errors();
259284
}

0 commit comments

Comments
 (0)