## Converting Floating-Point Numbers to Binary Strings in C

4 stars based on 34 reviews

You could print a floating-point number in binary by parsing and interpreting its IEEE representationor you could do it more elegantly by casting it as a base conversion problem — a binary to binary conversion ; specifically, a conversion from a binary number to a binary string.

Because we use a divisor of 10, this process simply isolates the digits of the number. If we string them together, we get the original number back: We can illustrate a similar process for fractional values, for c print double in binary trading 0. Because we use a multiplier of 10, this process isolates the digits of the number. The same two-part algorithm works for binary to binary conversion, if instead you divide and multiply by 2 and use binary arithmetic.

On paper, this is not too exciting. But in a computer, it allows us to convert binary numbers to binary strings. A floating-point binary value is a number, whereas a printed binary value is a string. We can use the binary to binary conversion algorithm to isolate the digits of the number and convert them to ASCII numerals in a string.

The function fp2bin converts a number from IEEE double c print double in binary trading to an equivalent character string made up of 0s and 1s. The algorithms are the same because in each case, the base of the number being converted is the c print double in binary trading as the base of the arithmetic used to convert.

Both are rounded to the nearest 53 significant bits, as shown in the values printed above trailing 0s are not printed. There are two aspects of the approximation in particular you can look at:.

The value of the binary number beyond its 53rd significant bit — its 56th bit overall — is greater than 2 ; therefore, it is rounded up the rounding results in a carry to the 52nd significant bit. This shows that the IEEE double approximation of 0. The resulting double will have the same value, only with extra trailing zeros — which fp2bin will not print.

My code depends on the fmod function, and one reader reports that the MINIX version of fmod produces incorrect results. Thanks for the feedback. Do you mean you want to print floats? I needed the single precision because I am generating values to be used in a hardware implementation of a LUT. Your functions helped a lot. Binary to Binary Conversion You could print a floating-point number in binary by parsing and interpreting its IEEE representationor you could do it more elegantly by casting it as a base conversion problem — a binary to binary conversion ; specifically, a conversion from a binary number to a binary string.

Get articles by e-mail. Could you please include IEEE numbers as well? If I want c print double in binary trading results to consider only 32 bits? There is a easy way to do it? The results are c print double in binary trading this way 0. Mauricio, Do you mean you want to print floats? What exactly is the error you get?