.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/approximate_multiplication.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_approximate_multiplication.py: Approximate floating-point multiplication ========================================= This example compares an approximate floating-point multiplication to the mathematically correct result. The approximation is based on Mitchell's logarithm approximation and the reference is the unquantized result. (Example inspired from T. Lindberg and O. Gustafsson, "Exact Eight-Bit Floating-Point Multiplication Using Integer Arithmetic", in *Proc. Asilomar Conf. Signals Syst. Comput.*, 2025.) .. GENERATED FROM PYTHON SOURCE LINES 10-39 .. code-block:: Python import matplotlib.pyplot as plt from matplotlib import ticker import apytypes as apy EXP_BITS, MAN_BITS = (4, 3) input_values = apy.fullrange(1, 2, exp_bits=EXP_BITS, man_bits=MAN_BITS) xx, yy = apy.meshgrid(input_values, input_values) # Calculate the reference using a format wide enough to capture the exact result ref = xx.cast(EXP_BITS, 2 * MAN_BITS) * yy def approx_mul(x, y): return apy.APyFloat.from_bits( x.to_bits() + y.to_bits() - (x.bias << x.man_bits), EXP_BITS, MAN_BITS ) result = apy.zeros_like(ref, exp_bits=EXP_BITS, man_bits=MAN_BITS) for xi, x in enumerate(input_values): for yi, y in enumerate(input_values): result[xi, yi] = approx_mul(x, y) error = result - ref .. GENERATED FROM PYTHON SOURCE LINES 40-41 Plot the results .. GENERATED FROM PYTHON SOURCE LINES 41-55 .. code-block:: Python fig, ax = plt.subplots() ax.set_xlabel(r"$x$") ax.set_ylabel(r"$y$") ax.set_aspect("equal") ax.xaxis.set_major_locator(ticker.MultipleLocator(2 * 2**-MAN_BITS)) ax.yaxis.set_major_locator(ticker.MultipleLocator(2 * 2**-MAN_BITS)) pcol = ax.pcolormesh(xx, yy, error, ec="k", lw=0.1, cmap="Blues_r") cbar = fig.colorbar(pcol) cbar.ax.set_title("Error") plt.show() .. image-sg:: /examples/images/sphx_glr_approximate_multiplication_001.png :alt: Error :srcset: /examples/images/sphx_glr_approximate_multiplication_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.112 seconds) .. _sphx_glr_download_examples_approximate_multiplication.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: approximate_multiplication.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: approximate_multiplication.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: approximate_multiplication.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_