<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( https://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="fr">
    <refnamediv>
        <refname>log2</refname>
        <refpurpose>logarithme en base 2. Exposant et mantisse en base 2 de nombres réels.</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Séquence d'appel</title>
        <synopsis>
            y = log2(x)
            [m, e] = log2(u)
        </synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Paramètres</title>
        <variablelist>
            <varlistentry>
                <term>x, y</term>
                <listitem>
                    <para>
                        tableaux de nombres réels ou complexes, tels que <literal>2.^y = x</literal>.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>u, m, e</term>
                <listitem>
                    <para>Tableaux de nombres réels tels que <literal>u = m .* 2.^e</literal>,
                        avec <literal>sign(m)=sign(u)</literal> et <literal>0.5 ≤ abs(m) &lt; 1</literal>.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>
            <literal>log2(x)</literal> calcule le logarithme en base 2 élément
            par élément : <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> calcule élément par élément la mantisse
            <varname>m</varname> et l'exposant <varname>e</varname> de tous les éléments de
            <varname>u</varname>. Ainsi, pour tout indice <literal>k</literal> nous avons
            <literal>u(k) = m(k) .* 2.^e(k)</literal> avec <literal>0.5 ≤ |m(k)| &lt; 1</literal>.
            <note>
                <itemizedlist>
                    <listitem>
                        <literal>[m, e] = log2([0 %nan -%inf %inf])</literal> donne
                        <literal>m=[0  NaN -Inf Inf]</literal> et <literal>e=[0 0 0 0]</literal>.
                    </listitem>
                    <listitem>
                        Cette syntaxe correspond à la fonction nommée frexp() en langage C ANSI.
                    </listitem>
                </itemizedlist>
            </note>
        </para>
    </refsection>
    <refsection>
        <title>Exemples</title>
        <para>
            Logarithme en base 2:
        </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 rend le résultat complexe
2 .^ L                        // -Inf n'est pas retrouvé
 ]]></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 rend le résultat complexe
 L  =
  -Inf   Nan + Nani   Inf   Inf + 4.5323601i

--> 2 .^ L                        // -Inf n'est pas retrouvé
 ans  =
   0.   Nan + Nani   Inf   Nan + Nani
]]></screen>
        <para/>
        <para>
            Décomposition en mantisses et exposants en base 2 :
        </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>Voir aussi</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>
            <member>
                <link linkend="logm">logm</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
