Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2023.1.0 - English


log2

Base-2 logarithm. Base-2 exponent and mantissa of some real numbers

Syntax

y = log2(x)
[m, e] = log2(u)

Arguments

x, y

arrays of real or complex numbers, such that 2.^y = x.

u, m, e

arrays of real numbers such that u = m .* 2.^e, with sign(m)=sign(u) and 0.5 ≤ abs(m) < 1.

Description

y = log2(x) is the "element-wise" base 2 logarithm, with y(i,j) = log2(x(i,j)).

log2(-%inf) == %inf + log2(-1)

[m, e] = log2(u) computes in an element-wise way the base-2 mantissa m and exponent e of every elements of u. Hence, for any index k we have u(k) = m(k) .* 2.^e(k) with 0.5 ≤ |m(k)| < 1.

  • [m, e] = log2([0 %nan -%inf %inf]) returns m=[0 NaN -Inf Inf] and e=[0 0 0 0].
  • This syntax corresponds to the ANSI C function frexp().

Examples

Base-2 logarithm:

L = log2([-1, 1, 2, %i, -%i])
clean(2.^L)
L = log2([0 %nan %inf])
2 .^ L
L = log2([0 %nan %inf -%inf]) // -%inf<0 turns the result to complex
2 .^ L                        // -Inf is not retrieved
--> L = log2([-1, 1, 2, %i, -%i])
 L  =
   4.5323601i   0.   1.   2.2661801i  -2.2661801i

--> clean(2.^L)
 ans  =
  -1.   1.   2.   i   -i

--> L = log2([0 %nan %inf])
 L  =
  -Inf   Nan   Inf

--> 2 .^ L
 ans  =
   0.   Nan   Inf

--> L = log2([0 %nan %inf -%inf]) // -%inf<0 turns the result to complex
 L  =
  -Inf   Nan + Nani   Inf   Inf + 4.5323601i

--> 2 .^ L                        // -Inf is not retrieved
 ans  =
   0.   Nan + Nani   Inf   Nan + Nani

Base-2 mantissa + exponent decomposition:

[m, e] = log2([-3, -1.5, -1, 0, 4, 1e20])
m .* 2.^e
--> [m, e] = log2([-3, -1.5, -1, 0, 4, 1e20])
 e  =
   2.     1.     1.    0.   3.   67.
 m  =
  -0.75  -0.75  -0.5   0.   0.5   0.6776264

--> m .* 2.^e
 ans  =
  -3.  -1.5  -1.   0.   4.   1.000D+20

np = number_properties;
x = [%eps np("tiniest") np("tiny") np("huge")]
[m, e] = log2(x)
--> x = [%eps np("tiniest") np("tiny") np("huge")]
 x  =
    2.220D-16    4.94D-324    2.22D-308    1.79D+308

--> [m, e] = log2(x)
 e  =
  - 51.  - 1073.  - 1021.    1024.
 m  =
    0.5    0.5    0.5    1.

[m, e] = log2([0 %nan -%inf %inf])
m .* 2.^e
-->[m, e] = log2([0 %nan -%inf %inf])
 e  =
    0.    0.    0.    0.
 m  =
    0.    Nan  - Inf    Inf

-->m .* 2.^e
 ans  =
    0.    Nan  - Inf    Inf

See also

  • log — natural logarithm
  • log10 — base 10 logarithm
  • nextpow2 — next higher power of 2.
  • bitstring — A string giving the literal bit representation of a number
  • hat — (^) exponentiation
Report an issue
<< log1p Log - exp - power logm >>

Copyright (c) 2022-2023 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Mon May 22 12:37:05 CEST 2023