0

Is there some clever and reliable way to print series of bits as an IEEE-754 without actually using a float type?

I have found a way to print fractions, which allows me to represent the float as a a fraction. However, I then came to realize that the exponent may range from -127 to 128 (after adjusting with bias), which may result in the multiplication mantissa * 2^128. The fraction method relies on representing the numerator as an integer, and I would require a really large integer to do this multiplication. I mean, I could use "custom" type to represent this large value (i.e. https://gmplib.org/), but I would prefer if to avoid this. If we multiplied by 10^x, I could simply adjust the decimal point and add some zeros, but sadly that's not the case either.

I have not been able to find anything that mentions any solution for this. Probably due to the fact that googling stuff like "print from

Why am I actually trying to do this?
I'm only doing this to get a better understanding of how floats (IEEE-754 in particular) work, and I find that it always help to do some practical example. So I thought "Hey, why not try to code it?". This has no practical application (that I know of)!

maltebp
  • 11
  • 1
  • 1
  • You can `memcpy` bytes between a `float` and a `uint32_t` to play with the representation. You can also use the `%a` format specifier on a `float`. – dbush Sep 29 '21 at 19:07
  • Thanks for the tip - might do that to toy around with it a bit more. But it's not exactly relevant to the question :-) – maltebp Sep 29 '21 at 19:10
  • `without actually using a float type?` GCC compiler has _software_ implementation of IEEE-745 types. – KamilCuk Sep 29 '21 at 19:30
  • The answer at [this question](https://stackoverflow.com/questions/69207580/bitwise-splitting-the-mantissa-of-a-ieee-754-double-how-to-access-bit-structure/69207903#69207903) might give you some tips. – Steve Summit Sep 29 '21 at 19:51
  • Re “Is there some clever and reliable way”: There are clever and reliable ways that are hard to understand and hard to implement (Ryu, Grisu3, others), and there are simple and reliable ways that are easy to understand and are inefficient (e.g., elementary school arithmetic). You can just convert the significand to a decimal numeral in an array of `char`, then multiply or divide repeatedly by two as directed by the exponent, carrying from digit to digit as taught in elementary school. – Eric Postpischil Sep 29 '21 at 20:54
  • Right, @EricPostpischil, I had an idea that there was a simple trick I had overlooked, that I didn't even consider that super simple solution. Efficiency is not a problem - it's just for me to toy around with. Might look into the other, more complicated solutions, at some point. – maltebp Oct 03 '21 at 06:18

1 Answers1

1

So, almost immediately after posting this, I finally succeded in finding the resources I've been looking for.

https://www.ryanjuckett.com/printing-floating-point-numbers/ talks about it, and references other relevant sources.

maltebp
  • 11
  • 1
  • 1
  • weeell, if you want it that way, basically copy from this point on: https://github.com/ulfjack/ryu/blob/master/ryu/d2s.c#L456 , as the first call in the function converts double to integers. – KamilCuk Sep 29 '21 at 19:32
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 29 '21 at 22:24