Thread: Int to Float

  1. #1
    Banned
    Join Date
    Mar 2008
    Posts
    78

    Int to Float

    I've tried to do this:

    Code:
    #include <stdio.h>
    
    
    int main() {
    int a=3,b=2;
    float c;
    
    c = (float)(a/b);
    printf("%f\n",c);
    
    return 0;
    }
    But instead of 3/2 he returns 1.00000000...

    How can i convert Int to float?

    Thanks!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Make sure at least one of the operands is converted to float, and the expression will be evaluated properly:
    Code:
    c = (float)a/b;
    What you have presently is an integer operation that's cast to float. 3/2 is 1 (note that both sides are integers), and converted to float it's 1.0.
    My best code is written with the delete key.

  3. #3
    Banned
    Join Date
    Mar 2008
    Posts
    78
    is there any error here:

    Code:
    xmax = (((float)x1 + ((float)wire_data[0] /2)) > xmax) ? ((float)x1 + ((float)wire_data[0] /2)) : xmax;
    
    xmin = (((float)x1 - ((float)wire_data[0] /2)) < xmin) ? ((float)x1 - ((float)wire_data[0] /2)) : xmin;
    ymax = (((float)y1 + ((float)wire_data[0] /2)) > ymax) ? ((float)y1 + ((float)wire_data[0] /2)) : ymax;
    
    ymin = (((float)y1 - ((float)wire_data[0] /2)) < ymin) ? ((float)y1 - ((float)wire_data[0] /2)) : ymin;

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Milhas View Post
    is there any error here:
    I think it's okay.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If wire_data[0] is an int, and you want the result of the divide by 2 to be a float, you could save a few keystrokes and simply:
    Code:
    wire_data[0] /2.0
    In this case, the 2.0 is a floating point value (a double), but you could also code
    Code:
    wire_data[0] /2.0f
    if you really wanted a float and not a double.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Hey guys, sorry to bring back an old thread but I'd rather do this than start a new one!

    I'm basically having trouble converting an int to a float. I've tried the following code and ever single time floatadd = 1077280768
    Code:
    floatadd = newadd;
    Code:
    floatadd = (double)newadd;
    Code:
    floatadd = double(newadd);
    I.e I've tried every option I've seen listed around here. Newadd is 22 and I need to convert it to a floating point to do some jazz on it, but all the above give 1077280768 as an answer. Any ideas?

    I'm sorry if this is a noobie question, I am very new to C programming.

    Thanks,

    pgcrooks

  7. #7
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by pgcrooks View Post
    I.e I've tried every option I've seen listed around here. Newadd is 22 and I need to convert it to a floating point to do some jazz on it, but all the above give 1077280768 as an answer. Any ideas?
    float and double are two different things. The below code will work:
    Code:
    float myfloat = (float) newadd;
    
    printf ("myfloat = &#37;f\n", myfloat);
    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  8. #8
    Registered User
    Join Date
    Aug 2008
    Posts
    12
    Thanks Pete, I realise float and double are different, I just copied and pasted the same thing 3 times above. Found the problem, turns out I'm a complete idiot and used %d in the printf command. Oh well... Stared at the convert line for ages without ever looking at the printf. Sorry!

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    1. This probably SHOULD be a new thread.
    2. You will need to give us a bit more code. One very likely scenario is that you are outputting a floating point number as an integer. The 1077280768 value when it turns to hex is 0x40360000, which looks suspiciously like a floating point number to me [yes, I know, I'm a geek by being able to "spot" floating point numbers in hex, but so be it].

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM