0

I have two values, one an int and one a float pointed to by void pointers (value1 and value2). They are received from a function call in any order. First the float then the int or vice versa. I would like to typecast the int to a float and then perform floating point math between them.

However if you were to typecast the int* to a float* then dereference it, you would get the bit pattern of the number interpreted as a float and it does not go through the int to float conversion.

So I was wondering if there was a clean way to cast one to the other without having to pass extra data about the types then doing a bunch of if statements.

For example

float x = -1.1;//types can be int on input
int y = 3;     //will be float if other is int
void* value1 = &x;
void* value2 = &y;
float z = 0;
void* token = &z;
*((float*)token) = *((float*)value1) - *((float*)value2); //but written to work

I want *((float*)token) to equal -4.1, but currently it would equal -1.1.

yosmo78
  • 489
  • 4
  • 13
  • In which way did it fail to use the obvious `*((float*)token) = *((float*)value1) - *((int*)value2);` or the more explicit `*((float*)token) = *((float*)value1) - 1.0*(*((int*)value2));` ? – Yunnosch Oct 23 '19 at 06:36
  • 1
    Is there any particular reason to pass those two numeric values as `void *`? Can't you declare a function accepting two `float`s (or `double`s)? – Bob__ Oct 23 '19 at 06:45
  • 1
    You need to fix the problem at the point where the function is called. But you haven't shown the function call. See [mcve]. – user3386109 Oct 23 '19 at 06:49
  • user3386109 Is on to something. Please explain how much and which influence you have on the source of those numbers. Can you make somehow sure that both void pointers actually do already point to floats? – Yunnosch Oct 23 '19 at 06:51
  • I have no influence and they must be void pointers. – yosmo78 Oct 23 '19 at 06:53
  • Please [edit] that info into your question. It might turn into a dupe of https://stackoverflow.com/a/58281061/7733418 – Yunnosch Oct 23 '19 at 06:54
  • @user3386109 the function header is just 2 void pointers as parameters. – yosmo78 Oct 23 '19 at 06:56
  • Please make a [mre] by editing your question. Also make sure that the MRE demonstrates all unknowns of your problem. I.e. call the function once with float, int and once with int, float, if that is the case. I assume that `token` is a parameter, too? The dupe seems more and more likely. – Yunnosch Oct 23 '19 at 06:59
  • @Yunnosch you are right, it is a dupe. I couldn't find your answer when I was searching earlier. – yosmo78 Oct 23 '19 at 07:00
  • With OP comment on my answer, I am now convinced that this is a duplicate. No problem. I only found it because I kind of happened to know. :-) – Yunnosch Oct 23 '19 at 07:01
  • Are there restrictions on the ranges of the int and the float? For example, most practical, everyday integers have several leading zero bits. Their bit pattern as a float would be very small. – Patricia Shanahan Oct 25 '19 at 03:02
  • This is not a duplicate because there is a much better chance of their being usable restrictions that will allow a bit pattern to be classified. – Patricia Shanahan Oct 25 '19 at 03:06
  • @PatriciaShanahan there was no restriction on input, well the restriction was INT_MAX and INT_MIN for ints ;/ – yosmo78 Oct 27 '19 at 04:29

1 Answers1

0

Don't cast. Convert.

*((float*)token) = *((float*)value1) - 1.0f*(*((int*)value2)); /* to be explicit about it */

or just trust the compiler to do math correctly

*((float*)token) = *((float*)value1) - *((int*)value2);

Apart from the fact that you need an actual place which token refers to.

anotherfloat = 0.0;
token = &anotherfloat;
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • Hmmm, I think you might answer "I don't kow which is the int and which the float." in that case allow me to refer to another answer of mine: https://stackoverflow.com/a/58281061/7733418 (I might actually make that question a dupe-proposal in this case). – Yunnosch Oct 23 '19 at 06:43
  • 1
    if that is the case, where there is not enough information. Then I will be forced to just pass extra information in ;( I was trying to avoid doing so. – yosmo78 Oct 23 '19 at 06:58