# xarray.ufuncs.log2

### xarray.ufuncs.log2 *= <xarray.ufuncs._unary_ufunc object>*

xarray specific variant of `numpy.log2()`. Handles xarray objects by dispatching to the appropriate function for the underlying array type.

Documentation from numpy:

Base-2 logarithm of x.

* **Parameters:**
  * **x** ([array_like](https://numpy.org/doc/stable/glossary.html#term-array_like)) – Input values.
  * **out** ([`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray), [`None`](https://docs.python.org/3/builtins/constants.html#None), or [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple) of [`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray) and [`None`](https://docs.python.org/3/builtins/constants.html#None), *optional*) – A location into which the result is stored. If provided, it must have
    a shape that the inputs broadcast to. If not provided or None,
    a freshly-allocated array is returned. A tuple (possible only as a
    keyword argument) must have length equal to the number of outputs.
  * **where** ([array_like](https://numpy.org/doc/stable/glossary.html#term-array_like), *optional*) – This condition is broadcast over the input. At locations where the
    condition is True, the out array will be set to the ufunc result.
    Elsewhere, the out array will retain its original value.
    Note that if an uninitialized out array is created via the default
    `out=None`, locations within it where the condition is False will
    remain uninitialized.
  * **\*\*kwargs** – For other keyword-only arguments, see the
    [ufunc docs](https://numpy.org/doc/stable/reference/ufuncs.html#ufuncs-kwargs).
* **Returns:**
  **y** ([`ndarray`](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html#numpy.ndarray)) – Base-2 logarithm of x.
  This is a scalar if x is a scalar.

#### SEE ALSO
[`log`](xarray.ufuncs.log.html.md#xarray.ufuncs.log), [`log10`](xarray.ufuncs.log10.html.md#xarray.ufuncs.log10), [`log1p`](xarray.ufuncs.log1p.html.md#xarray.ufuncs.log1p), `emath.log2`

### Notes

Logarithm is a multivalued function: for each x there is an infinite
number of z such that 2\*\*z = x. The convention is to return the z
whose imaginary part lies in (-pi, pi].

For real-valued input data types, log2 always returns real output.
For each value that cannot be expressed as a real number or infinity,
it yields `nan` and sets the invalid floating point error flag.

For complex-valued input, log2 is a complex analytical function that
has a branch cut [-inf, 0] and is continuous from above on it. log2
handles the floating-point negative zero as an infinitesimal negative
number, conforming to the C99 standard.

In the cases where the input has a negative real part and a very small
negative complex part (approaching 0), the result is so close to -pi
that it evaluates to exactly -pi.

### Examples

<div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesShowIframe('15fa4cc8-02e0-4ed7-b0c8-3fc5424e7b04','cb43c7af-ad70-47bf-bc5c-14545ba4b294','2a4a7216-8b02-41f0-a3f7-f8a744ab6928','../lite/tree/../notebooks/index.html?path=6cb071b7_7013_461a_9868_6b3f6ae85b8b.ipynb','None')">Try it in your browser!</button></div>
```pycon
>>> import numpy as np
>>> x = np.array([0, 1, 2, 2**4])
>>> np.log2(x)
array([-inf,   0.,   1.,   4.])
```

```pycon
>>> xi = np.array([0+1.j, 1, 2+0.j, 4.j])
>>> np.log2(xi)
array([ 0.+2.26618007j,  0.+0.j        ,  1.+0.j        ,  2.+2.26618007j])
```

<div id="2a4a7216-8b02-41f0-a3f7-f8a744ab6928" class="try_examples_outer_iframe  hidden"><div class="try_examples_button_container"><button class="try_examples_button" onclick="window.tryExamplesHideIframe('15fa4cc8-02e0-4ed7-b0c8-3fc5424e7b04','2a4a7216-8b02-41f0-a3f7-f8a744ab6928')">Go Back</button><button class="try_examples_button" onclick="window.openInNewTab('15fa4cc8-02e0-4ed7-b0c8-3fc5424e7b04','2a4a7216-8b02-41f0-a3f7-f8a744ab6928')">Open In Tab</button></div><div id="cb43c7af-ad70-47bf-bc5c-14545ba4b294" class="jupyterlite_sphinx_iframe_container"></div></div><script>document.addEventListener("DOMContentLoaded", function() {window.loadTryExamplesConfig("../try_examples.json");});</script>
