how to convert decimal to floating point number

This is a discussion on how to convert decimal to floating point number within the C Programming forums, part of the General Programming Boards category; anyone care to help me here ??...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    how to convert floating point number into it's sign bit, mantissa, and exponent

    anyone care to help me here ??
    Last edited by -EquinoX-; 03-03-2008 at 09:26 AM.

  2. #2
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Yes of course, i assume you have a decimal number as a string, like "123.4567" and you need a variable of type double to hold the result.
    The function you need is atof, and is in the <stdlib.h>.
    For reasons unknown to me i would prefer to use strtod.
    Last edited by xuftugulus; 03-02-2008 at 06:41 PM.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    is there any way to convert a string to an int in C? how about if I have a decimal number as a double?? how would I convert that to a floating point??

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    Be more specific, show some of what you need to convert.
    Check this out.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    okay here's an example.. say that I have a decimal number of 10 and I want to extract it to +1.0100000000000000000000e+3, something like that

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    You mean an assignment?
    Code:
    int a = 10;
    double d = (double)a;
    or a conversion from text?
    Code:
    char s[] = "10";
    double d = atof(s);
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    yes I mean an assignment, not from a text

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    +1.0100000000000000000000e+3 is 1010
    so how you can "extract" 10 to this value?

    to assign 10 you use
    double a = 10;
    to print it in the scientific notation - use &#37;g format of printf - read about formats of printf and what formating they provide
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vart View Post
    +1.0100000000000000000000e+3 is 1010
    so how you can "extract" 10 to this value?

    to assign 10 you use
    double a = 10;
    to print it in the scientific notation - use %g format of printf - read about formats of printf and what formating they provide
    Or,
    Code:
    double a = 10.0;
    The original number posted has 22 decimal places, which is about 5 more than you can expect to get out of a "double". Some compilers and processors have larger floating point numbers "long double", implememented with for example the "80-bit Extended precision" of x86, that can give more digits - in the x86 case, you can expect about 20 digits precision with this.

    If you need more precise values, you need to use a special math library, such as GMP.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    and how would I convert the decimal to binary?? is there a specific command in C that would allow me to do this?? or do I have to do this manually?

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    58
    You have to do it manually, but it's not too hard. The only problem I can think of is deciding how to store the binary value. A string makes the most sense.
    Code:
    #include <stdio.h>
    
    // Return a value with the bit specified
    // by i set and all other bits unset
    #define bitmask(i) (1U << i)
    
    int main()
    {
        // The number of bits in an int
        const int intSize = 31;
    
        // The decimal number to convert
        int value;
        int i;
    
        printf("Please enter a decimal number to be converted: ");
        scanf("&#37;d", &value);
    
        // Print the bits of the decimal value
        for (i = intSize; i >= 0; --i)
        {
            // Assume bitmask(i) is unset
            char bit = '0';
    
            // Pick the character to print
            if (value & bitmask(i))
            {
                bit = '1';
            }
    
            putchar(bit);
        }
    
        putchar('\n');
    
        return 0;
    }
    Last edited by Banana Man; 03-03-2008 at 07:40 AM.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    Itoa can convert a number into base 2, if you will, but it's not a standard function.
    I don't know of any other function to do it, though.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    the code you supplied generates an error.. it's on the define part.. I know know what's wrong with that

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,791
    The code compiles fine.
    What compiler do you use?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Registered User
    Join Date
    Jan 2008
    Posts
    569
    I used this to compile gcc -ansi -Wall binary.c -o binary

    and it says error: expected identifier or "(" before '/' token

Page 1 of 7 1234567 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to read a digit of a floating point number?????
    By spicy_centipede in forum C Programming
    Replies: 15
    Last Post: 07-14-2007, 11:43 AM
  2. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 11:56 AM
  3. checking for floating point number
    By ssharish2005 in forum C Programming
    Replies: 6
    Last Post: 10-18-2005, 08:14 PM
  4. fixed point / floating point
    By confuted in forum Game Programming
    Replies: 4
    Last Post: 08-13-2002, 01:25 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21