Although C's floating-point types float
and double
typically use binary representations internally (almost invariably using formats conforming to IEEE-754), there are no standard routines for printing floating-point numbers in binary, or to convert binary strings to floating-point.
Basically there is almost no need for human-readable binary floating-point strings; floating-point numbers are almost always converted to and from decimal strings. (This can cause certain problems, though, since binary and decimal fractions are incommensurate, and almost always end up being imprecise approximations for each other — but that's a different story.)
The closest you can come to a binary floating-point string involves the %a
format, although it's hexadecimal, not binary. But %a
with printf
will convert a floating-point number to a hexadecimal string, and %a
with scanf
will convert that sort of hexadecimal string back to a floating-point number.
It is probably easier to explain all this by example. Suppose I have the decimal number 12345.53125, and suppose I store it in a float
variable:
float f = 12345.53125;
I can print it back out using %e
or %f
:
printf("%e\n", f);
printf("%f\n", f);
This prints
1.234553e+04
12345.531250
If I print it out using %a
I get the rather odd-looking
0x1.81cc4p+13
Internally (in IEEE-754 single-precision floating-point) the number 12345.53125 is represented by a bit pattern which in hexadecimal is 0x4640e620
, consisting of a 23-bit raw significand 0x40e620
and an 8-bit raw exponent 140. Shifting, applying the implicit "1" bit, and removing the exponent bias of 127, these work out to 0x1.81cc4
with an exponent of 13, which nicely matches %a
's output.
In actual binary this number is
0b11000000111001.10001
or if you want an exponential notation it might be
0b1.100000011100110001e13
But, as I said, there's no built-in C function that will generate binary representations like that for you automatically.
If you did something like
double fraBinary = 1010.011001;
printf("%.9e\n", fraBinary);
and saw
1.010011001e+03
then you were not working with binary numbers at all: those numbers 1010.011001
and 1.010011001e+03
you saw and typed were pure decimal.
When you're working with ordinary floating-point numbers (using scientific notation or not), you don't usually need to worry too much about the binary representation (other than to know that it's there), and you don't usually have to work with significands ("mantissas") and exponents separately. If you don't already, you should know that the notation exemplified by
1.234553e+04
is almost universal. You can enter constants in your program that way:
double d = 1.234553e+04;
You can scan numbers from the user that way. You can take strings like "1.234553e+04"
and convert them to float
or double
by calling the library functions atof()
or strtod()
. And as you know you can print them out (in decimal) using printf
and %f
or %e
.
So what is it exactly that you want to do with your significands and/or exponents, in binary?