Portable Doubles

This is a discussion on Portable Doubles within the C Programming forums, part of the General Programming Boards category; Originally Posted by siavoshkc I don't understand why. What is the 400921FB54442D18? Hexadecimal of 3.14...? Why its character string uses ...

  1. #31
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    Quote Originally Posted by siavoshkc
    I don't understand why. What is the 400921FB54442D18? Hexadecimal of 3.14...? Why its character string uses 8 bytes?
    It's a case of people making remarks out of context. The two points are:

    1)3.14... as a double takes up just as much space if you print in in hex.
    2)as a character string, printing hex is shorter than desimal in this particular case.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  2. #32
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by EvilGuru
    does anyone known of a smarter method to swap the bytes than this:
    Code:
    void my_htond (double *out, double in)
    {
        unsigned char *bin = (unsigned char *) ∈
        unsigned char *bot = (unsigned char *) out;
        int j;
        for (j = 0; j < sizeof(double); j++) bout[j] = bin[(sizeof(double) - 1) - j];
    }
    This is pretty much the same thing, except done in place -- just for something else to look at.
    Code:
    #include <stdio.h>
    
    void reverse_bytes(void *object, size_t size)
    {
       unsigned char *beg, *end;
       for ( beg = object, end = beg + size - 1; beg < end; ++beg, --end )
       {
          unsigned char temp = *beg;
          *beg = *end;
          *end = temp;
       }
    }
    
    void show_bytes(const void *object, size_t size)
    {
       const unsigned char *byte;
       for ( byte = object; size--; ++byte )
       {
          printf("%p : %02X\n", byte, (unsigned)*byte);
       }
    }
    
    int main(void)
    {
       double value = 123.456;
       puts("a:");
       show_bytes(&value, sizeof value);
       puts("b:");
       reverse_bytes(&value, sizeof value);
       show_bytes(&value, sizeof value);
       return 0;
    }
    
    /*
    http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html:
    405EDD2F1A9FBE77
    */
    
    /* my output
    a:
    0012FF84 : 77
    0012FF85 : BE
    0012FF86 : 9F
    0012FF87 : 1A
    0012FF88 : 2F
    0012FF89 : DD
    0012FF8A : 5E
    0012FF8B : 40
    b:
    0012FF84 : 40
    0012FF85 : 5E
    0012FF86 : DD
    0012FF87 : 2F
    0012FF88 : 1A
    0012FF89 : 9F
    0012FF8A : BE
    0012FF8B : 77
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. parse doubles from socket read?
    By willy in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:32 AM
  2. C - Portable?
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 07-15-2007, 09:09 AM
  3. sscanf with doubles
    By ufsargas in forum C Programming
    Replies: 1
    Last Post: 06-28-2006, 03:05 PM
  4. Replies: 3
    Last Post: 09-01-2005, 11:47 AM
  5. which Portable mp3 player?
    By Raihana in forum A Brief History of Cprogramming.com
    Replies: 27
    Last Post: 01-09-2004, 06:58 AM

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