.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/apyfixed.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_apyfixed.py: APyFixed ======== APyTypes provide a class for configurable floating-point numbers: :class:`APyFixed`. The numbers are represented as the underlying integer, the total number of bits and integer bits. However, there are also convenience methods to do this either from a float, a string, or a bit-pattern. One can also supply the number of fractional bits; two out of the three (total, integer, and fractional bits) must be supplied. .. GENERATED FROM PYTHON SOURCE LINES 12-18 .. code-block:: Python import apytypes as apy a = apy.APyFixed(7, 4, 2) a .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(7, bits=6, int_bits=4) .. GENERATED FROM PYTHON SOURCE LINES 19-20 Create a APyFixed from a floating-point number .. GENERATED FROM PYTHON SOURCE LINES 20-23 .. code-block:: Python b = apy.APyFixed.from_float(-2.25, int_bits=3, frac_bits=2) b .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(23, bits=5, int_bits=3) .. GENERATED FROM PYTHON SOURCE LINES 24-25 There is also a convenience method that does this .. GENERATED FROM PYTHON SOURCE LINES 25-28 .. code-block:: Python b = apy.fx(-2.25, int_bits=3, frac_bits=2) b .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(23, bits=5, int_bits=3) .. GENERATED FROM PYTHON SOURCE LINES 29-32 .. code-block:: Python c = apy.APyFixed.from_str("3.75", bits=6, frac_bits=3) c .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(30, bits=6, int_bits=3) .. GENERATED FROM PYTHON SOURCE LINES 33-36 Standard arithmetic operations are supported. The resulting number of bits depends on the operation. For addition and subtraction, the maximum number of integer bits and fractional bits are used. A single integer bit is added to make sure that the computation does not overflow. .. GENERATED FROM PYTHON SOURCE LINES 36-38 .. code-block:: Python a + b .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(126, bits=7, int_bits=5) .. GENERATED FROM PYTHON SOURCE LINES 39-41 For multiplication, the number of integer bits and fractional bits is the sum of the number of integer bits and fractional bits of the included operands, respectively. .. GENERATED FROM PYTHON SOURCE LINES 41-43 .. code-block:: Python a * c .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(210, bits=12, int_bits=7) .. GENERATED FROM PYTHON SOURCE LINES 44-45 Standard string formatting is supported .. GENERATED FROM PYTHON SOURCE LINES 45-47 .. code-block:: Python str(a) .. rst-class:: sphx-glr-script-out .. code-block:: none '1.75' .. GENERATED FROM PYTHON SOURCE LINES 48-49 Comparisons with integers and floats are supported .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: Python (b >= -2.25, a == 1.75) .. rst-class:: sphx-glr-script-out .. code-block:: none (True, True) .. GENERATED FROM PYTHON SOURCE LINES 52-54 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 54-56 .. code-block:: Python b + 3.5 .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(5, bits=6, int_bits=4) .. GENERATED FROM PYTHON SOURCE LINES 57-58 Which is equivalent to .. GENERATED FROM PYTHON SOURCE LINES 58-60 .. code-block:: Python b + apy.fx(3.5, int_bits=b.int_bits, frac_bits=b.frac_bits) .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(5, bits=6, int_bits=4) .. GENERATED FROM PYTHON SOURCE LINES 61-63 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 63-65 .. code-block:: Python 1.25 + 1.25 + apy.fx(2, int_bits=4, frac_bits=1) .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(9, bits=6, int_bits=5) .. GENERATED FROM PYTHON SOURCE LINES 66-67 which would yield a different result from .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: Python apy.fx(2, int_bits=4, frac_bits=1) + 1.25 + 1.25 .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(10, bits=7, int_bits=6) .. GENERATED FROM PYTHON SOURCE LINES 70-71 To change the word length of a APyFixed, the method :func:`~APyFixed.cast` can be used .. GENERATED FROM PYTHON SOURCE LINES 71-72 .. code-block:: Python (a + b).cast(4, 3) .. rst-class:: sphx-glr-script-out .. code-block:: none APyFixed(124, bits=7, int_bits=4) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.003 seconds) .. _sphx_glr_download_examples_apyfixed.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: apyfixed.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: apyfixed.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: apyfixed.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_