reallog
Natural logarithm that errors on complex results.
Category: Mathematics > Elementary Math
Syntax
Y = reallog(X)
Returns log(X) for nonnegative real X, and an error if any element is negative.
Description
log quietly returns a complex number when it meets a negative argument, which can hide a bug further upstream. reallog turns that situation into an error (Reallog produced complex result.), so a program that must stay in the reals fails early instead of carrying complex values forward.
When all the elements are nonnegative it gives exactly the same answer as log.
Input arguments
X- Real array whose elements are expected to be nonnegative.
Output arguments
Y- Real array of the same size as
X.
Example
Strict logarithm
reallog([1 exp(1) 10])
log(-1)
Output
ans = 1×3
0 1.0000 2.3026
ans = 0.0000 + 3.1416i