log(b, x)


Compute the base b logarithm of x. Throws DomainError for negative Real arguments.


  1. Compute the natural logarithm of a positive number:

    julia> x = 10.0;
    julia> log(x)

    This example calculates the natural logarithm of the positive number x.

  2. 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.

  3. 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 a DomainError. To obtain a complex result, use a complex argument instead.

  4. Using Base.Math.JuliaLibm for improved accuracy:
    julia> using Base.Math.JuliaLibm
    julia> log(x)

    The log function in the Base.Math.JuliaLibm module provides an experimental variant that is typically faster and more accurate. To use it, first import the module, and then call log 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.

*Required Field

Checking you are not a robot: