how to convert numbers in decimal to binary? 
Author Message
 how to convert numbers in decimal to binary?

Quote:

> E.g I want convert 0.375 to binary

That's not an easy problem, actually.  I think that GNU's libgmp
might have a solution--I've used it to convert numbers to base 36
for a special file format (no, not one that I designed).

Here's the documentation for the function in question, so that
you can figure out whether it's what you want before you actually
download the library.

 - Function: size_t mpf_out_str (FILE *STREAM, int BASE, size_t
          N_DIGITS, mpf_t OP)
     Output OP on stdio stream STREAM, as a string of digits in base
     BASE.  The base may vary from 2 to 36.  Print at most N_DIGITS
     significant digits, or if N_DIGITS is 0, the maximum number of
     digits accurately representable by OP.

     In addition to the significant digits, a leading `0.' and a
     trailing exponent, in the form `eNNN', are printed.  If BASE is

     delimiter.

     Return the number of bytes written, or if an error occurred,
     return 0.

You can obtain GNU libgmp from
        ftp://ftp.gnu.org/pub/gnu/gmp
in case you're interested.
--
"You call this a *C* question? What the hell are you smoking?" --Kaz



Tue, 16 Jul 2002 03:00:00 GMT  
 how to convert numbers in decimal to binary?
E.g I want convert 0.375 to binary
Thanks


Wed, 17 Jul 2002 03:00:00 GMT  
 how to convert numbers in decimal to binary?
Ben is right. This is not as easy a problem as converting an integer to
binary (which I recently posted some untested code to do) where you do
repeated divisions and modulo remainders to put numbers into a reversed
string ...

However, and I won't make an off the cuff effort to do this, but you can do
something similar (with some complexity that doesn't happen in the integer
case) for decimal fractions -- fairly obvious since the internal
representation is binary.

Take the fractional part only

Multiply by two: if the result is greater than 1, the first fractional bit
is 1, otherwise it is zero

Take the fractional part of the resulting number

Multiply by two: if the result is greater than 1, the next fractional bit is
1, otherwise it is zero

Seems like a pattern is emerging here

So, what's the problem ... think about the exact representation of some
decimal fraction like 0.1 in binary ... doesn't seem to exist, so you have
to make some intelligent choices about where you will cut everything off.

Perhaps a legitimate question to ask here (as well as in a separate thread)
is what support the C99 standard may have for binary literals. Introductory
material in the standard says that one of the changes is to support "binary
floating-point literals" and the printf and scanf modifiers. I am not quite
sure how to interpret this.

Regards,
Sam



Wed, 17 Jul 2002 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. **How to convert decimal number into binary number?????

2. Help:convert binary to decimal numbers

3. Converting numbers between decimal /binary

4. Convert from binary to decimal, hex to decimal etc

5. Need Help!! convert roman number to decimal number

6. Help Needed Converting Binary Number To Hex Number

7. Binary to Decimal number conversion ?

8. decimal to binary number C routines

9. How to convert binary to decimal?

10. converting decimal to binary

11. convert 66-bit number to decimal

12. convert decimal to roman number

 

 
Powered by phpBB® Forum Software