Results 1 to 5 of 5

Thread: Casting int to float in C

  1. #1
    Join Date
    Mar 2010
    Posts
    197

    Casting int to float in C

    Hello,
    I'm doing the exercise decimal to binary converter and I have a little problem with the use of type (float). Actually my function that transforms my decimal to binary number works well, but as I use the pow () function to calculate powers of 2, I am forced to use numbers of type (double), although the numbers after the point is that 0. So, can I cast int to float. any help on this is appreciated. Thanks in advance.

  2. #2
    Join Date
    Nov 2009
    Posts
    335

    Re: Casting int to float in C

    Hello,
    Yes give us the code. If you do not need to use numbers to decimal, does not declare a double. The cast is implicit if you do
    Here is the code
    Code:
    int number = pow(4, 3);
    You can even re-code your own function that calculates powers, it is very easy. This is not the full code, but this is the important line in the code.

  3. #3
    Join Date
    Nov 2009
    Posts
    518

    Re: Casting int to float in C

    Hello,
    Check out the following code
    Code:
    int hand()
    {
        int chc=0;
        double num=0, nombreConverti=0;
        chariot ky=0;
    
        printf("Welcome to the decimal-binary converter!\ N \ N");
        printf("1.Conversion decimal-binary\ N");
        printf("Binary-decimal 2.Conversion\ N \ N");
        printf("What is your chc?\ N");
        scanf("% D", &chc);
        printf("\ NWhat num do you convert?\ N");
        scanf("% Lf", &num);
    
        if (chc==1)
        {
            nombreConverti=convDecBin(num);
            printf("\ NThe conversion of% d in binary is% f\ N", (int)num, (float)nombreConverti);/ / Pb with the float!
        }
        else
        {
            / / ConvBinDec (num);
        }
    
        printf("\ N \ NPress any ky to terminate\ N");
        scanf("% C", &ky);
        return 0;
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    343

    Re: Casting int to float in C

    Hello,
    You can alternatively try this code, see if this helps you, though I am not sure that this is what you need
    Code:
    double convDecBin(double number)
    {
        double nm=0, pwr, x;
    
        while (number>0)
        {
            pwr=2, x=1;
            while(number-pwr>=0)
            {
                x++;
                pwr=pow(2,x);
            }
            x--;
            pwr=pow(2,x);
            nm+=pow(10,x);
            number-=pwr;
    
        }
    
        return nm;
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    356

    Re: Casting int to float in C

    Hello,
    Already I want to clarify that the float can contain a point number, as well as double. It is less clear. It is not you going with that solved the problem of zeros after the comma. Since he can not contain too many (eg 10001010111000.000000 want a double, but not float), there is an overflow
    Code:
    printf("\ NThe conversion of binary is% d:% .0 f\ N", (int)number, num);
    This is what i am talking about.

Similar Threads

  1. Replies: 4
    Last Post: 02-01-2011, 12:25 AM
  2. How to convert string to float c#
    By Pratyush in forum Software Development
    Replies: 3
    Last Post: 14-08-2009, 01:09 PM
  3. CSS float disappeared the background
    By addie in forum Software Development
    Replies: 3
    Last Post: 29-06-2009, 05:49 PM
  4. CSS : How to float outside parent div
    By Valerian in forum Software Development
    Replies: 4
    Last Post: 21-05-2009, 12:03 PM
  5. How to make a CSS div float OVER another div
    By Warner in forum Software Development
    Replies: 3
    Last Post: 21-05-2009, 10:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,536,309.69078 seconds with 17 queries