.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/apyfloat.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_apyfloat.py: APyFloat ======== APyTypes provide a class for configurable floating-point numbers: :class:`APyFloat`. The numbers are created from the sign-bit, the exponent and the mantissa (without hidden one) and the corresponding wordlengths. However, there are also convenience methods to do this either from a float or from a bit-pattern. .. GENERATED FROM PYTHON SOURCE LINES 11-17 .. code-block:: Python from apytypes import APyFloat a = APyFloat(True, 3, 5, 4, 5) a .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=1, exp=3, man=5, exp_bits=4, man_bits=5) .. GENERATED FROM PYTHON SOURCE LINES 18-21 .. code-block:: Python b = APyFloat.from_float(2.25, 3, 2) b .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=4, man=0, exp_bits=3, man_bits=2) .. GENERATED FROM PYTHON SOURCE LINES 22-25 .. code-block:: Python c = APyFloat.from_bits(int("10110101"), 3, 4) c .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=1, exp=1, man=5, exp_bits=3, man_bits=4) .. GENERATED FROM PYTHON SOURCE LINES 26-28 Standard arithmetic operations are supported. The resulting number of exponent and mantissa bits are the maximum of the involved terms. .. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python a + b .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=7, man=30, exp_bits=4, man_bits=5) .. GENERATED FROM PYTHON SOURCE LINES 31-33 .. code-block:: Python a * c .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=1, man=17, exp_bits=4, man_bits=5) .. GENERATED FROM PYTHON SOURCE LINES 34-35 Standard string formatting is supported .. GENERATED FROM PYTHON SOURCE LINES 35-37 .. code-block:: Python str(a) .. rst-class:: sphx-glr-script-out .. code-block:: none '-0.0722656' .. GENERATED FROM PYTHON SOURCE LINES 38-39 Comparisons with integers and floats are supported .. GENERATED FROM PYTHON SOURCE LINES 39-41 .. code-block:: Python (b >= 7, a < -1.2) .. rst-class:: sphx-glr-script-out .. code-block:: none (False, False) .. GENERATED FROM PYTHON SOURCE LINES 42-44 Arithmetic with Python integers and floats is supported, although *not recommended*, by automatically casting the number to the same format of the APyType-object .. GENERATED FROM PYTHON SOURCE LINES 44-46 .. code-block:: Python b + 1.125 .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=4, man=2, exp_bits=3, man_bits=2) .. GENERATED FROM PYTHON SOURCE LINES 47-48 Which is equivalent to .. GENERATED FROM PYTHON SOURCE LINES 48-50 .. code-block:: Python b + APyFloat.from_float(1.125, exp_bits=b.exp_bits, man_bits=b.man_bits, bias=b.bias) .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=4, man=2, exp_bits=3, man_bits=2) .. GENERATED FROM PYTHON SOURCE LINES 51-53 However, while it is convenient to let APyTypes convert numbers automatically, it is not recommended as it can yield unexpected results. Consider the expression .. GENERATED FROM PYTHON SOURCE LINES 53-55 .. code-block:: Python 2 + 2 + APyFloat.from_float(8, exp_bits=4, man_bits=1) .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=10, man=1, exp_bits=4, man_bits=1) .. GENERATED FROM PYTHON SOURCE LINES 56-57 which would yield a different result from .. GENERATED FROM PYTHON SOURCE LINES 57-59 .. code-block:: Python APyFloat.from_float(8, exp_bits=4, man_bits=1) + 2 + 2 .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=10, man=0, exp_bits=4, man_bits=1) .. GENERATED FROM PYTHON SOURCE LINES 60-61 To change the word length of a APyFloat, the method :func:`~APyFloat.cast` can be used .. GENERATED FROM PYTHON SOURCE LINES 61-62 .. code-block:: Python (a + b).cast(4, 3) .. rst-class:: sphx-glr-script-out .. code-block:: none APyFloat(sign=0, exp=8, man=0, exp_bits=4, man_bits=3) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.007 seconds) .. _sphx_glr_download_examples_apyfloat.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: apyfloat.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: apyfloat.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: apyfloat.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_