How to print IEEE754 number (without printf)?

For the purposes of this question, I am not able to use printf objects (I cannot say why, unfortunately, but let me assume that now I know what I'm doing).

For the IEEE754 single precision number, you have the following bits:

 SEEE EEEE EFFF FFFF FFFF FFFF FFFF FFFF 

where S is the sign, E is the exponent, and F is the proportion.

Printing a character is relatively easy for all cases, as it catches all special cases, such as NaN ( E == 0xff, F != 0 ), Inf ( E == 0xff, F == 0 ) and 0 ( E == 0, F == 0 , is considered special simply because in this case the exponential offset is not used).

I have two questions.

First, what is the best way to turn denormalized numbers (where E == 0, F != 0 ) into normalized numbers (where 1 <= E <= 0xfe )? I suspect that this will be necessary to simplify the answer to the following question (but I may be wrong, so feel free to educate me).

The second question is how to print normalized numbers. I want to be able to print them in two ways: exponential, such as -3.74195E3 and -3.74195E3 , like 3741.95 . Although, just looking at these two side by side, it should be quite easy to turn the first into the last, simply by moving the decimal point around. So let's just focus on exponential form.

I have a vague recollection of an algorithm that I used a long time ago to print PI, where you used one of the ever-decreasing formulas and kept the upper and lower limit of possibilities, displaying a digit when both limits are matched, and the offset calculation is 10 times (therefore, when the upper and lower limits were 3.2364 and 3.1234 , you could output 3 and adjust for this in the calculation).

But that was a long time ago since I did this, so I donโ€™t even know if this is the right approach. It seems, since the value of each bit is half that of the previous bit when moving along the fractional part ( 1/2 , 1/4 , 1/8 , etc.).

I would prefer that I do not have to wade through the printf source code if it is absolutely necessary, so if someone can help with this, I will be infinitely grateful.

+5
c ieee-754 printf
source share
4 answers

If you want to get accurate results for each conversion, you will have to use arithmetic of arbitrary precision, as is done in printf () implementations. If you want to get results that are close, possibly differing only in their least significant numbers, then a simple algorithm with double precision will be enough: for the integer part, divide by ten and add the remainders to form the decimal string (in reverse order); for the fractional part, multiply by ten and subtract the integer parts to form the decimal string.

I recently wrote an article about this method: http://www.exploringbinary.com/quick-and-dirty-floating-point-to-decimal-conversion/ . It does not print scientific notation, but it should be trivial to add. The algorithm prints subnormal numbers (the ones I printed came out for sure, but you will need to do more rigorous testing).

+3
source share

Denormalized numbers cannot be converted to normalized numbers of the same floating point type. The equivalent normalized number will be too small to be represented.

To print normalized numbers, one stupid way I can think of is to multiply by 10 times (well, for the fractional part).

+1
source share

The first thing you need to do is convert the exponent to decimal (since presumably this is what you want to get) using logarithms. You take a fraction of this result and multiply the mantissa by exp10 of that fraction, and then convert it to decimal characters. From there, you just need to insert the decimal point in the appropriate place, shifted by the decimal value.

0
source share

There is an article by G. Steele that describes in more detail an algorithm that seems to be based on the same principle as it was drawn. If memory is being served, there is a time when you are forced to use unlimited precision arithmetic. (I think itโ€™s accurate to print floating point numbers exactly, but the quote is currently scrolling, I canโ€™t confirm, and Googleโ€™s results are polluted with retrospective paper the same from 20 years later).

0
source share

All Articles