0

I have on situation, where i need to keep the value float as well as int. so tried like below. but no help. can any one help on this?

union Val {
        int a;
        float b;
    };

    Val p;
    p.b = 45.56;

    int k = p.a; // i want k should be 45;
EZEROFIVE EMD
  • 55
  • 2
  • 9
  • 1
    unions dont work like this. Your union holds either an `int` or a `float` but not both. What do you actually want to do? Seems like simple rounding would do the job – 463035818_is_not_an_ai Apr 19 '16 at 12:01
  • yes but i dont want each time it to be converted from float to int. any other idea – EZEROFIVE EMD Apr 19 '16 at 12:04
  • if you really want to save computation at the expense of memory then simply use a `struct Val { int a; float b; }` ... or make `a` private and provide a lazy access function, that does the rounding only the first time the value is accessed. – 463035818_is_not_an_ai Apr 19 '16 at 12:05
  • you don't need anything special to make int k = p.b to be 45. That will happen automatically. When you assign a floating point value to an integer, the fractional part is dropped. – xaxxon Apr 19 '16 at 12:05
  • you mean assign value to both a and b on initialization and use occordingly – EZEROFIVE EMD Apr 19 '16 at 12:06
  • @xaxxon right, I wasnt sure if he really just wants simple rounding or perhaps a more complicated conversion. For simple rounding, a `struct {int a;float b}` is of course compete overkill – 463035818_is_not_an_ai Apr 19 '16 at 12:07
  • please clarify if the `int` should be simply the rounded `float` or if there is some heavy computation involved to get the `int` from the `float` – 463035818_is_not_an_ai Apr 19 '16 at 12:08
  • @xaxxon i am afraid if i use that in loop which will go for 50000 iteration will it affect performance, because i believe, for int k = p.b, it is internally converting float to in each time. – EZEROFIVE EMD Apr 19 '16 at 12:09
  • dont be afraid unless you really observe some performance problem. When in doubt, you should always profile your code before(!) doing any optimizations. To make the implicit conversion have any effect on the runtime of your code it has to be code that does little else than this conversion. – 463035818_is_not_an_ai Apr 19 '16 at 12:13
  • 3
    @EZEROFIVEEMD you have no idea what kind of code the compiler will generate. If you're worried about performance, then profile an optimized build. Also, 50,000 iterations is trivial even in the worst case. – xaxxon Apr 19 '16 at 12:14
  • @EZEROFIVEEMD [I've suggested a solution](http://stackoverflow.com/a/36718608/2642059) but I'm unclear what you want to happen when you assign an `int` to your object. Are you expecting the fractional portion of the `float` to be preserved? That's what I assume in my answer. – Jonathan Mee Apr 19 '16 at 12:49
  • @JonathanMee not exactly, but your code helped me, thank you so much – EZEROFIVE EMD Apr 19 '16 at 13:30
  • @EZEROFIVEEMD If you want to clarify, I'd be happy to edit my answer. – Jonathan Mee Apr 19 '16 at 13:31
  • @JonathanMee the answer you have posted gave me more idea. the operator overloading concept is what i wanted, now i can do any operation on that value just on assignment(no need of internal or external conversion). thanks for sharing. – EZEROFIVE EMD Apr 19 '16 at 13:41

3 Answers3

1

I have on situation, where i need to keep the value float as well as int. so tried like below

You can't do it with a union. A union can only hold a single value inside at any time. You need two separate variables. You can keep them in side a struct if you like:

struct Val {
    int a;
    float b;
};

Now you can have both an int and a float.

 Val p;
 p.b = 45.56;
 p.a = p.b;

 int k = p.a; // no conversion

That said, since you apparently only use a to store a converted value of b, you should measure whether the conversions even affect performance.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

I see that you say:

i dont want each time it to be converted from float to int [sic]

To do that you could use user-defined conversions to accomplish this.

So your struct would look like this:

class Val {
    int a;
    float b;
public:
    Val& operator= (const int _a) {a = _a; b = _a + fmod(b, 1.0F); return *this;}
    Val& operator= (const float _b) {b = _b; a = trunc(_b); return *this;}
    operator int() {return a;}
    operator float() {return b;}
};

Please note that what you really want to use is simply a float with static_cast<int> For astatic_cast:

No checks are performed during runtime to guarantee that the object being converted is in fact a full object of the destination type. Therefore, it is up to the programmer to ensure that the conversion is safe. On the other side, it does not incur the overhead of the type-safety checks of dynamic_cast.

I've provided an example of using Val here: http://ideone.com/XUesib but you could accomplish the exact same thing given float foo like this:

foo = 1.3F;
cout << static_cast<int>(foo) << endl << foo << endl;
foo = 13 + fmod(foo, 1.0F);
cout << static_cast<int>(foo) << endl << foo << endl;
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
0

You can use structure with constructor, and initialize your variables in it as you wish.

struct Val {
    int a;
    float b;
    Val(float value) {
        a = b = value;
    }
};

So you can use it in loop and don't worry about conversations each time, just create your Val variable outside loop and use it.

Binary Mind
  • 309
  • 3
  • 13