1

I have an array of 32-bit long integers. Some of the elements will afterwards be used as 32-bit floats. I would like to supply an initializer list to initialize these floating point values correctly. For example, if the first two elements are used as integers, and the third as float, and I wish to initialize the third element to be equal to 100.0, I am forced to do this:

long a[3]={10,20,0x42c80000};

This works fine, but is not very expressive.

If I do this:

long a[3]={10,20,100.0};

The compiler will convert the floating point literal to 0x64.

I do not have a C++11 compiler, so using a union will not work.

Any ideas?

2
  • 2
    How do you distinguish that the first 2 values should be interpreted as int and the 3rd as a double? Commented Sep 6, 2016 at 9:32
  • If the issue is just that "the reader of the code will easily understand that the 0x42C80000 is meant to represent a single-precision floating value of 100.0" then how about #define FLOATBITS_100 (0x42c80000) then put that in the array? Commented Sep 6, 2016 at 14:10

3 Answers 3

8

Unions don't require C++11 so go ahead and use them. However, it's generally a bad idea to mix objects/elements of different types in a single data structure, if they don't belong together semantically.

It will make your code for manipulating said data structure brittle and error-prone. I'd suggest rethinking your semantics and seeing why you need both longs and floats in the same array. They could be stored in separate data structures. Also floats can store all longs accurately (though with different bitwise representation), so why not just create a float array to begin with?

Sign up to request clarification or add additional context in comments.

3 Comments

The array must be initialized at compile time...it is a group of constants programmed to a Flash memory...so I cannot do it at run time. The union can only be initialized by its first member (before C++11), so it does not allow me to initialize an element as a float.
There are very legitimate reasons for putting different data types in a single structure...that is why there are unions in the C++ language. Each user of the data knows its function, and it is not at all error prone. My question is whether C++ allows doing what I wish, not to discuss whether this type of programming is "bad".
The floating point array has the same problem in reverse...it takes my integer values and converts them to floating point format. What I am really asking is if there is any way that I can initialize my constant integer array at compile time to contain the values {10,20,0x42c80000} but in such a way that the reader of the code will easily understand that the 0x42C80000 is meant to represent a single-precision floating value of 100.0.
1

What you are doing when you assign it to the long array it will do an implicit conversion. If you want to store the byte-value inside the variable you can use memcpy

So what you can do (and note this is pretty dangerous in case your type-sizes are not the same) have a function like this

long toLongRepresantation(float f)
{
   long ret;
   memcpy(&ret,&f,sizeof(long));
   return ret;
}

You can also template this but you should make sure that the size for both types are the same.

6 Comments

This has UB when sizeof(float) < sizeof(long)
While this may get you to where you want. I would strongy advice for considering the things in paul-g's answer
@user2079303 I have written in the answer that the sizes of the types are important to consider.
Oh, so you have. I just read the code. Why not fix the size so that it's not dangerous?
In case he wants other types. (I have also written below that is can be used with templates). A static_assert would be practical here but there is no easy native way for that in non-c++11
|
0

why you don't use struct or class instead?

struck a
{
    int    iValue1; 
    float  fValue1;
    double dValue1;
    char   cValue1;

    int    iValue2[10]; // array of ints
    float  fValue2[5 ]; // array of floats
    // ...
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.