User Tools

Site Tools


pdclib:printing_floating_point_numbers

This is an old revision of the document!


Printing Floating Point Numbers

To complete the ``printf()`` function of PDCLib – which so far lacks support for ``%e``, ``%f``, and ``%g`` – I needed to solve the problem of converting the binary, in-memory representation of ``double`` and ``long double`` to decimal.

Using the same algorithm as for integers (divide by ten, take quotient, recurse with remainder) is not an option. Not only would repeated floating point divisions be horrendously slow: Multiplying / dividing a (base 2) floating point by 10 repeatedly would also accumulate rounding errors. You would have slow, wrong results.

Dragon4

The seminal work in this area is a paper by Guy L. Steele Jr. and Jon L. White, How to Print Floating-Point Numbers Accurately. They had worked out an algorithm they had named *Dragon 4*, which solved the problem. Years later, Robert G. Burger and R. Kent Dybvig, Printing Floating-Point Numbers Quickly and Accurately improved the performance of *Dragon 4* significantly.

pdclib/printing_floating_point_numbers.1754846447.txt.gz · Last modified: by solar

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki