exact

An up-to-date version of this and other files can be found in my CommandLineTools project on GitHub.

Shows the exact value of a decimal number when stored as single (32 bit), double (64 bit) or extended (80 bit) IEEE-754 floating point variables.

Introduction

In my article on floating point types in Delphi, I explain that such types only have a limited number of bits and cannot represent every value given to them exactly. The result is the best approximation of the exact value that can be represented.

Often, in discussions on Stack Overflow and for answers explaining certain aspects of floating point math, it would be handy to know the exact value the floating point variable represents. Using my BigDecimals unit, it was very easy to create this simple program.

It displays the exact values of a floating point number as single (32 bit), double (64 bit) and extended (80 bit) precision IEEE-754 floating point. Because I often need that too, it also displays the hex representation of the data types.

Program

The program reads a decimal number (as string) from the command line. It then converts this to a BigDecimal. The BigDecimal represents the value in the string exactly. Then this exact value is converted to the nearest possible representation as single, double and extended precision floating point number. These floating point numbers are then converted back to BigDecimals, giving the exact numbers stored in the floating point values.

If there is no command line parameter, a simple help text is shown.

Example

C:\> exact 1.2345e-6
The value:    1.2345e-6 (0.0000012345)

32 bit float: 0.0000012345000186542165465652942657470703125
Hex:          35A5B119

64 bit float: 0.000001234499999999999996546707660416419827242862083949148654937744140625
Hex:          3EB4B6231ABFD271

80 bit float: 0.00000123449999999999999995882768719765898608742293873774542589671909809112548828125
Hex:          3FEBA5B118D5FE938821

Compiling

This program uses BigDecimals, and needs Delphi XE2 or newer to compile.

To compile the program, you need the downloaded zip (click the Download button above) as well as my BigDecimals unit, which is part of my BigNumbers github repository. You can either clone it, or download it as a zip file. Just put the Source directory of the repository in your search path, and it should compile.

If you want values for Single, Double and Extended, compile the program for Win32. If you compile for Win64, you will only get the values for Single and Double. The program should also compile for (32 bit) macOS. I did not try to compile it for the mobile compilers.

Conclusion

I hope this code is useful to you. If you use some of it, please credit me. If you modify or improve the unit, please send me the modifications.

I may improve or enhance the unit myself (e.g. to make it work in macOS too), and I will try to post changes here. But this is not a promise. Please don’t request features.

I do not accept any responsibility about the usability or in case of any damage caused by the use of my programs or files. Use them at your own risk. If they don’t work properly, you have the sources.

Rudy Velthuis

Standard Disclaimer for External Links

These links are being provided as a convenience and for informational purposes only; they do not constitute an endorsement or an approval of any of the products, services or opinions of the corporation or organization or individual. I bear no responsibility for the accuracy, legality or content of the external site or for that of subsequent links. Contact the external site for answers to questions regarding its content.

Disclaimer and Copyright

The coding examples presented here are for illustration purposes only. The author takes no responsibility for end-user use. All content herein is copyrighted by Rudy Velthuis, and may not be reproduced in any form without the author's permission. Source code written by Rudy Velthuis presented as download is subject to the license in the files.

Back to top