IEEE Representation of Floats

This is a discussion on IEEE Representation of Floats within the C Programming forums, part of the General Programming Boards category; It is my understanding that float in c is stored as IEEE single precision representation. However void main (int argv, ...

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

    IEEE Representation of Floats

    It is my understanding that float in c is stored as IEEE single precision representation. However

    void main (int argv, char argc[])
    {
    float a = -341.274;
    printf("%X\n",a);
    return;
    }

    When I execute that, the result is:

    C0755462

    The IEEE 754 single precision representation for -341.274 is: C3AAA312
    and double precision is: C07554624DD2F1AA

    So wtf is happening here? It seems like the float is stored as a double precision but then for some reason truncated after 8 bits. Whats going on!??!?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, printf receives all float values as "double" - this is part of the C specification for "variadic arguments" (that is, functions that take variable number of arguments). And it's not truncated, it's your output that is truncated.

    If you want to print the hex representation of a number, you could do something like this:
    Code:
       printf("%X", *(unsigned int *)&x);
    That forces the compiler to generate code that interprets the float value as an integer for passing to printf, instead of promoting it to double.

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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    1. Your main() is all wrong.
    2. Use [code][/code] tags around your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    3
    Quote Originally Posted by matsp View Post
    Yes, printf receives all float values as "double" - this is part of the C specification for "variadic arguments" (that is, functions that take variable number of arguments). And it's not truncated, it's your output that is truncated.

    If you want to print the hex representation of a number, you could do something like this:
    Code:
       printf("%X", *(unsigned int *)&x);
    That forces the compiler to generate code that interprets the float value as an integer for passing to printf, instead of promoting it to double.

    --
    Mats
    Thanks a lot! I understand it really well and my program works perfectly, absolutely fantastic.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by Pnevma View Post
    It is my understanding that float in c is stored as IEEE single precision representation.
    Your understanding is incorrect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. variables larger than floats
    By a.mlw.walker in forum C Programming
    Replies: 13
    Last Post: 04-23-2009, 02:28 AM
  2. Replies: 3
    Last Post: 11-25-2006, 03:14 PM
  3. 90 ASCII representation?
    By Axel in forum C Programming
    Replies: 8
    Last Post: 09-12-2005, 05:47 PM
  4. comparing two arrays of floats
    By COBOL2C++ in forum C++ Programming
    Replies: 7
    Last Post: 07-16-2003, 03:22 AM
  5. IEEE Society for Students
    By Ben_Robotics in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 06-26-2003, 08:53 AM

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