0

Initial situation/ task

I want to reinterpret the double-precision floating point number to a long integer number. The byte size of both data types is equal on my platform (i.e. 8 bytes). I do not want to touch the bit pattern, i.e. it is important to keep the pattern and treat the pattern as if it was the other data type.

What I have tried so far

I have done the following:

    /* Cast the data to an integral type [long], so floats and doubles can be read as well */
    double  double_val;
    double* double_ptr;
    long* long_ptr;
    long  long_val;

    double_val = out1[it]; //23.5f;
    double_ptr = &double_val; // set address of new memory location with double-precision number
    long_ptr = (long*)double_ptr; // cast float pointer to uint64_t pointer
    long_val = *long_ptr;

Question

C++ has the reinterpret_cast. Does my code perform something equal?

What I do is to cast the pointer, but I use two memory locations. Is there a shorter, more convenient way to accomplish what I want?

Are there any potential side effects of my code?

"Why would you want to do this?"

I have to trick my automatic debug scripting. The debugging API can only read variables of integral type (long values).

Another reason is that I want to print the bit pattern at the memory location of that variable. For integral types this can be done with the format specifier 0x%08X. I am not sure if this would be applicable for a memory location with a variable of type double directly as well, or if this is implementation specific or would cause any other side effects.

Matthias W.
  • 1,039
  • 1
  • 11
  • 23
  • 1
    "The byte size of both data types is equal on my platform" - How about alignment and used bits? Why are you tgalking about `long`, but use `uint64_t`? – too honest for this site Jul 01 '16 at 06:06
  • 1
    Isn't it similar to: [Get the integer representation value of a float type variable in C](http://stackoverflow.com/q/36125638/45249)? – mouviciel Jul 01 '16 at 06:06
  • Did you check the code? Does your compiler **really** use pointers and a memory location? – too honest for this site Jul 01 '16 at 06:07
  • To print the representation of _any_ object, consider [this](http://stackoverflow.com/a/35367414/2410359) – chux - Reinstate Monica Jul 01 '16 at 06:26
  • @Olaf: What do you mean by used bits? It's an IEEE-754 double, so all 64 bits of the floating-point number should be used. By alignment you mean endianness? Or memory alignment issues? Edit: I haven't checked the (assembly?) code so far. – Matthias W. Jul 01 '16 at 06:59
  • @Olaf: thanks for pointing me to `uint64_t`, I will fix that in the code. – Matthias W. Jul 01 '16 at 07:36
  • 1
    There is no requirement by the standard to use IEEE floats. And if I meant endianess, I'd written that (that I anticipated you are aware of) and not "alignment". Use the typical pattern with a `union`. – too honest for this site Jul 01 '16 at 14:41

1 Answers1

0

The standard way to do it in C is

long y = *(long*)&x;

Which is similar to what you did, but you put it in ~8 lines.

user31264
  • 6,557
  • 3
  • 26
  • 40
  • 1
    it violates aliasing rule and it's not the standard way in C – phuclv Jul 01 '16 at 06:19
  • He NEEDS to violate the aliasing rule. The whole question is how to violate the aliasing rule. And yes, it is the standard way to do this in C: http://stackoverflow.com/questions/1382051/what-is-the-c-equivalent-for-reinterpret-cast – user31264 Jul 01 '16 at 06:22
  • 1
    [there are several ways to do it without aliasing](http://stackoverflow.com/a/4328396/995714), http://stackoverflow.com/a/21007877/995714. The standard way to do it since C99 is via a union – phuclv Jul 01 '16 at 06:27