Thread: 'splitting' a double

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Your problem is the wrong using of pointer arithmetics

    +delta moves pointer not to delta bytes but to delta units, each unit size depends on the pointre type

    So you can use high_bits = low_bits +1;
    Or if you prefer your approach - cast &x to unsinged char before adding sizeof(int)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Or something like:
    Code:
            unsigned int *high_bits = ((unsigned int *)(&x)) + 1;
    --
    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
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by matsp View Post
    Or something like:
    Code:
            unsigned int *high_bits = ((unsigned int *)(&x)) + 1;
    This is undefined behaviour, as is the original poster's code. You may only use a character pointer. If you have the ISO/IEC 9899:1999 standard handy, see section 6.3.2.3 paragraph 7.

    The following, for example, is well defined:
    Code:
    void split(double x)
    {
        unsigned char *y = (unsigned char *)&x;
        int i;
        for (i = 0; i < sizeof(double); i++)
            printf("%02x", y[i]);
        puts("");
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Correct, but it's only undefined if a double is not aligned to the same as (or a multiple of) "unsigned int", and whilst I'm sure there are machines where such a construction may be the case (someone implementing for example 56 or 80 bit "double" would fall into this category), it is not a common occurrence.

    Does anyone here actually know of (and are actually using, as opposed to knowing of the existence) a machine where this is the case?

    --
    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. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Conversion From C++ To C
    By dicon in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 02:54 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 09:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM