HELP - cast (int *) to (double *) 
Author Message
 HELP - cast (int *) to (double *)

int main ()
{  int i[2];
   double d;
   i[0]=4;
   i[1]=5;
  d = * (double *) i;

Quote:
}

I thought d would become 45 or 54, but d remains 4.  And actually d is an
int 4, not a double 4.0.  Why?  By casting i as a double ptr, I thought I
expand its scope to 8 bytes and thus *i would now point to i[0] and i[1].
Apparently i was wrong.  Why?
Also, is it always true that when we use the %f format specifier to print an
int, the output would be 0.0000?  Why 0.0000?
many thanks.


Sat, 10 Jan 2004 21:04:42 GMT  
 HELP - cast (int *) to (double *)

Quote:
> int main ()
> {  int i[2];
>    double d;
>    i[0]=4;
>    i[1]=5;
>   d = * (double *) i;
> }
> I thought d would become 45 or 54, but d remains 4.  And actually d is an

Danger danger. double is not a 2-byte or 4-byte integer data type. It is
a floating point data type, with a fundamentally different binary
representation than int.
Making doubles from ints is a very complex procedure and what you are
doing isn't even halfway there.
You have obviously mixed up longs and doubles. And even with using
longs, d will probably end up as (4<<16) + 5 == 262149 or
(5<<16) + 4 == 327684. The size of int isn't one decimal digit, you
know.

Quote:
> int 4, not a double 4.0.  Why?  By casting i as a double ptr, I thought I
> expand its scope to 8 bytes and thus *i would now point to i[0] and i[1].
> Apparently i was wrong.  Why?

What makes you think it is an int 4? Anything assigned to a double
variable implicitly coerces into a double. If you're trying to printf()
it to see its value, notice that printf() could leave the dot out if
the fraction portion is zero.

Quote:
> Also, is it always true that when we use the %f format specifier to print an
> int, the output would be 0.0000?  Why 0.0000?

You're lying to your compiler, saying an int is supposed to be a double.
So the compiler may output anything it wants. But usually the reason is
that your int has a binary representation that doesn't represent any
valid double, so the compiler thinks it's 0.0000.

Quote:
> many thanks.

You're welcome.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"I will never display my bum in public again."
   - Homer Simpson



Sat, 10 Jan 2004 21:17:26 GMT  
 HELP - cast (int *) to (double *)



Quote:

> > int main ()
> > {  int i[2];
> >    double d;
> >    i[0]=4;
> >    i[1]=5;
> >   d = * (double *) i;
> > }

> > I thought d would become 45 or 54, but d remains 4.  And actually d is
an

> Danger danger. double is not a 2-byte or 4-byte integer data type. It is
> a floating point data type, with a fundamentally different binary
> representation than int.
> Making doubles from ints is a very complex procedure and what you are
> doing isn't even halfway there.
> You have obviously mixed up longs and doubles. And even with using
> longs, d will probably end up as (4<<16) + 5 == 262149 or
> (5<<16) + 4 == 327684. The size of int isn't one decimal digit, you
> know.

Actually I am not intending to convert two integers to a double and I do not
"invent" this "technique".  This "technique" was used by someone who wrote a
software for academic research.  The i[0] is the IP address of a machine and
i[1] is an int starting from 1, then 2, 3, etc.  He "combines" these two,
using the "technique" above, to get a 8-byte long unique identification
number, which he calls a double.  I think his technique is flawed because
somehow, as shown above, that unique number appears to capture only i[0];
but I do not know exactly the reason.

Quote:
> > int 4, not a double 4.0.  Why?  By casting i as a double ptr, I thought
I
> > expand its scope to 8 bytes and thus *i would now point to i[0] and
i[1].
> > Apparently i was wrong.  Why?

> What makes you think it is an int 4? Anything assigned to a double
> variable implicitly coerces into a double. If you're trying to printf()
> it to see its value, notice that printf() could leave the dot out if
> the fraction portion is zero.

"What makes you think it is an int 4?"  When I printf("%d", d), the output
is 4.  When I printf("%f", d), the output is 0.0000.  So I think d must be
an int.  Is there a C function that prints the bit pattern of a variable, so
that I can see actually what the d's bit pattern is?  Also, if I 'if
((d-4)<0.000001) printf("d=4")' the output is 4, that shows d is 4. i.e. d
captures only i[0].

My real question is not really what the value of d would be (it's OK as long
as it is unique) but rather whether d "includes" both i[0] and i[1] (if not,
then d is not unique).

- Show quoted text -

Quote:
> > Also, is it always true that when we use the %f format specifier to
print an
> > int, the output would be 0.0000?  Why 0.0000?

> You're lying to your compiler, saying an int is supposed to be a double.
> So the compiler may output anything it wants. But usually the reason is
> that your int has a binary representation that doesn't represent any
> valid double, so the compiler thinks it's 0.0000.



Sun, 11 Jan 2004 06:06:47 GMT  
 HELP - cast (int *) to (double *)
Hi Mklai,


Quote:
> Actually I am not intending to convert two integers to a double and I do
> not "invent" this "technique".  This "technique" was used by someone who
> wrote a software for academic research.  The i[0] is the IP address of a

Tell him to commit suicide (chances are he'll fail anyway).

Quote:
> machine and i[1] is an int starting from 1, then 2, 3, etc.  He
> "combines" these two, using the "technique" above, to get a 8-byte long
> unique identification number, which he calls a double.  I think his
> technique is flawed because somehow, as shown above, that unique number
> appears to capture only i[0]; but I do not know exactly the reason.

The internal representation of a double is not specified by the standard -
yes, there is a standard representation, but the C standard doesn't
require it. Also, the internal representation of an int is not specified.

Quote:
> "What makes you think it is an int 4?"  When I printf("%d", d), the
> output is 4.  When I printf("%f", d), the output is 0.0000.  So I think

This is undefined (supplying arguments different from specifications in
the format string to printf).

Quote:
> d must be an int.  Is there a C function that prints the bit pattern of
> a variable, so that I can see actually what the d's bit pattern is?

No. C doesn't know anything about the bit patterns of variables (Of course
you could write such a function, but its behavior would be undefined).

Quote:
> Also, if I 'if ((d-4)<0.000001) printf("d=4")' the output is 4, that
> shows d is 4. i.e. d captures only i[0].

No. The value of d is undefined because you messed (which is undefined)
with its internal representation (which also is undefined :).

Quote:
> My real question is not really what the value of d would be (it's OK as
> long as it is unique) but rather whether d "includes" both i[0] and i[1]
> (if not, then d is not unique).

You forgot option 3: The behavior is undefined (and thus d is not
unique).

Daniel

--
IMO, anyway.

` { } \ | [ ] '



Sun, 11 Jan 2004 08:16:16 GMT  
 HELP - cast (int *) to (double *)

Quote:

>  The i[0] is the IP address of a machine and
> i[1] is an int starting from 1, then 2, 3, etc.  
> He "combines" these two, using the "technique" above,
> to get a 8-byte long unique identification
> number, which he calls a double.

It sounds like he's just calling it that, and not using
double in the C sense of the word.

Quote:
>  Is there a C function that prints the bit pattern of a variable, so
> that I can see actually what the d's bit pattern is?  

/* BEGIN new.c */

#include <stdio.h>
#include <limits.h>

char *Bitstr(char *str, void const *obj, size_t n);

int main ()
{
    int i[2];
    double d;
    char ibits[CHAR_BIT * sizeof i + 1], dbits[CHAR_BIT * sizeof d + 1];

    i[0]=4;
    i[1]=5;
    d = * (double *) i;
    printf("i = %i = %s\n", *i, Bitstr(ibits, &i, sizeof i));
    printf("d = %f = %s\n", d, Bitstr(dbits, &d, sizeof d));
    return 0;

Quote:
}

char *Bitstr(char *str, void const *obj, size_t n)
{
    unsigned char mask;    
    char *strp = str;

    while(n){
        --n;
        mask = 1 << CHAR_BIT - 1;
        do{
            *str++ = (char)(((unsigned char*)obj)[n] & mask ? '1': '0');
            mask >>= 1;
        }while(mask);
    }
    *str = '\0';
    return strp;

Quote:
}

/* END new.c */

--
 pete



Sun, 11 Jan 2004 10:22:00 GMT  
 HELP - cast (int *) to (double *)
haha lol.


Quote:
> int main ()
> {  int i[2];
>    double d;
>    i[0]=4;
>    i[1]=5;
>   d = * (double *) i;
> }

> I thought d would become 45 or 54, but d remains 4.  And actually d is an
> int 4, not a double 4.0.  Why?  By casting i as a double ptr, I thought I
> expand its scope to 8 bytes and thus *i would now point to i[0] and i[1].
> Apparently i was wrong.  Why?
> Also, is it always true that when we use the %f format specifier to print
an
> int, the output would be 0.0000?  Why 0.0000?
> many thanks.



Mon, 12 Jan 2004 01:11:02 GMT  
 HELP - cast (int *) to (double *)

Quote:
> haha lol.

Thank you for this entirely useless reply. Now did you have something to
actually say or are you just bashing newbies?

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/

"Insanity is to be shared."
   - Tailgunner



Mon, 12 Jan 2004 01:11:23 GMT  
 HELP - cast (int *) to (double *)

Quote:

>int main ()
>{  int i[2];
>   double d;
>   i[0]=4;
>   i[1]=5;
>  d = * (double *) i;
>}

>I thought d would become 45 or 54, but d remains 4.  And actually d is an
>int 4, not a double 4.0.  Why?  By casting i as a double ptr, I thought I
>expand its scope to 8 bytes and thus *i would now point to i[0] and i[1].
>Apparently i was wrong.  Why?
>Also, is it always true that when we use the %f format specifier to print an
>int, the output would be 0.0000?  Why 0.0000?
>many thanks.

Consider what you told the compiler to do:

        i by itself is treated as the address of the first element of the
array.

        The cast says to take that address and consider it the address of
a double.

        The dereference operator says to extract the value of the double
at that address.

        The assignment says to store this value in the double d.

The last two together indicate that a double value is being stored in
a double variable so there is no need for any conversion at all.  It
is the logical equivalent of saying copy from i to d for the length of
d.

How do you know what value is in d?

Why do you think the size of d will be exactly the size of the two
integers in i?

On your system, what is the result of storing an integer bit pattern
(or two) into an area to be treated as a double?  On my IBM mainframe,
4 is 0x00000004 and moving these two integers to a double produces an
unnormalized floating point value on the order of 4 times (16 to the
-70th power) which is close enough to 0 to fool the %f modifier.

<<Remove the del for email>>



Mon, 26 Jan 2004 07:10:57 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. (double)=(double)-(int)*(int) valid???

2. casting double as int

3. VC++ 1.52 and type casting doubles to int

4. cast long int to short int

5. casting int to unsigned int

6. Casting : int and (int)

7. int / int = double

8. int func(int) versus int func(int *) efficacy.

9. Need help with reinterpret_cast, a C-style cast or function-style cast

10. Need help with reinterpret_cast, a C-style cast or function-style cast

11. Need help with reinterpret_cast, a C-style cast or function-style cast

12. Need help with reinterpret_cast, a C-style cast or function-style cast

 

 
Powered by phpBB® Forum Software