<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - INRIA
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 * Copyright (C) 2019 - Samuel GOUGEON
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml"
          xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook"
          xmlns:scilab="http://www.scilab.org" xml:id="log2" xml:lang="en">
    <refnamediv>
        <refname>log2</refname>
        <refpurpose>
            Base-2 logarithm. Base-2 exponent and mantissa of some real numbers
        </refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>
            y = log2(x)
            [m, e] = log2(u)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>x, y</term>
                <listitem>
                    <para>
                        arrays of real or complex numbers, such that <literal>2.^y = x</literal>.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>u, m, e</term>
                <listitem>
                    <para>arrays of real numbers such that <literal>u = m .* 2.^e</literal>,
                        with <literal>sign(m)=sign(u)</literal> and <literal>0.5 ≤ abs(m) &lt; 1</literal>.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            <literal>y = log2(x)</literal> is the "element-wise" base 2 logarithm, with
            <literal>y(i,j) = log2(x(i,j))</literal>.
            <note>
                <literal>log2(-%inf) == %inf + log2(-1)</literal>
            </note>
        </para>
        <para>
            <literal>[m, e] = log2(u)</literal> computes in an element-wise way the base-2 mantissa
            <varname>m</varname> and exponent <varname>e</varname> of every elements of
            <varname>u</varname>. Hence, for any index <literal>k</literal> we have
            <literal>u(k) = m(k) .* 2.^e(k)</literal> with <literal>0.5 ≤ |m(k)| &lt; 1</literal>.
            <note>
                <itemizedlist>
                    <listitem>
                        <literal>[m, e] = log2([0 %nan -%inf %inf])</literal> returns
                        <literal>m=[0  NaN -Inf Inf]</literal> and <literal>e=[0 0 0 0]</literal>.
                    </listitem>
                    <listitem>
                        This syntax corresponds to the ANSI C function frexp().
                    </listitem>
                </itemizedlist>
            </note>
        </para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <para>
            Base-2 logarithm:
        </para>
        <programlisting role="example"><![CDATA[
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
 ]]></programlisting>
    <screen><![CDATA[
--> 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
]]></screen>
        <para/>
        <para>
            Base-2 mantissa + exponent decomposition:
        </para>
        <programlisting role="example"><![CDATA[
[m, e] = log2([-3, -1.5, -1, 0, 4, 1e20])
m .* 2.^e
 ]]></programlisting>
    <screen><![CDATA[
--> [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
]]></screen>
    <para/>
        <programlisting role="example"><![CDATA[
np = number_properties;
x = [%eps np("tiniest") np("tiny") np("huge")]
[m, e] = log2(x)
 ]]></programlisting>
    <screen><![CDATA[
--> 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.
]]></screen>
    <para/>
        <programlisting role="example"><![CDATA[
[m, e] = log2([0 %nan -%inf %inf])
m .* 2.^e
 ]]></programlisting>
    <screen><![CDATA[
-->[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
]]></screen>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="log">log</link>
            </member>
            <member>
                <link linkend="log10">log10</link>
            </member>
            <member>
                <link linkend="nextpow2">nextpow2</link>
            </member>
            <member>
                <link linkend="bitstring">bitstring</link>
            </member>
            <member>
                <link linkend="hat">hat</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
