Skip to content

Commit cde925f

Browse files
committed
Adapted exp() for complex numbers so that it correctly treats the corner cases +/-inf and NaN.
1 parent beeed0b commit cde925f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,28 @@ impl<T: Float> Complex<T> {
193193
/// Computes `e^(self)`, where `e` is the base of the natural logarithm.
194194
#[inline]
195195
pub fn exp(self) -> Self {
196-
// formula: e^(a + bi) = e^a (cos(b) + i*sin(b))
197-
// = from_polar(e^a, b)
198-
Self::from_polar(self.re.exp(), self.im)
196+
// formula: e^(a + bi) = e^a (cos(b) + i*sin(b)) = from_polar(e^a, b)
197+
198+
// Treat the corner cases +∞, -∞, and NaN
199+
let mut im = self.im;
200+
if self.re.is_infinite() {
201+
if self.re < T::zero() {
202+
if !self.im.is_finite() {
203+
im = T::one();
204+
}
205+
} else {
206+
if self.im == T::zero() || !self.im.is_finite() {
207+
if self.im.is_infinite() {
208+
im = T::nan();
209+
}
210+
return Self::new(self.re, im);
211+
}
212+
}
213+
} else if self.re.is_nan() && self.im == T::zero() {
214+
return self;
215+
}
216+
217+
Self::from_polar(self.re.exp(), im)
199218
}
200219

201220
/// Computes the principal value of natural logarithm of `self`.

0 commit comments

Comments
 (0)