log(b, x)
log(b,x)
Compute the base b
logarithm of x
. Throws DomainError
for negative Real
arguments.
Examples
-
Compute the natural logarithm of a positive number:
julia> x = 10.0; julia> log(x) 2.302585092994046
This example calculates the natural logarithm of the positive number
x
. -
Handle complex logarithm:
julia> z = 1 + 2im; julia> log(z) 0.8047189562170503 + 1.1071487177940904im
The
log
function can also handle complex numbers. It returns the complex logarithm of the given complex argument. -
Throw an error for negative real arguments:
julia> log(-5.0) ERROR: DomainError with -5.0: log will only return a complex result if called with a complex argument. Try log(Complex(x)).
When called with a negative real argument,
log
throws aDomainError
. To obtain a complex result, use a complex argument instead. - Using
Base.Math.JuliaLibm
for improved accuracy:julia> using Base.Math.JuliaLibm julia> log(x) 2.302585092994046
The
log
function in theBase.Math.JuliaLibm
module provides an experimental variant that is typically faster and more accurate. To use it, first import the module, and then calllog
function as usual.
Common mistake example:
julia> log("hello")
ERROR: MethodError: no method matching log(::String)
In this example, the log
function is called with an invalid argument type (String
). Make sure to provide a valid argument of type Real
or a complex number when using log
.
See Also
abs2, beta, binomial, ceil, cell, cross, ctranspose, ctranspose!, cummin, cumprod, cumprod!, cumsum, cumsum!, cumsum_kbn, div, divrem, eigfact, eigfact!, eigmin, eps, erf, erfc, erfcinv, erfcx, erfi, erfinv, exp, exp10, exp2, expm1, exponent, factor, factorial, factorize, floor, gcd, invmod, log, log10, log1p, log2, logspace, max, min, mod, mod1, modf, next, nextpow, nextprod, num, primes, primesmask, prod, realmin, sqrt, sum!, sumabs, sumabs!, sumabs2, sumabs2!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.