So I have a homework thing that asks me "How do you convert an int to a float?".
Google searches gives me alot of different results, the most promising being Type Casting.
But what I'm unsure of is how I make the variable itself a float from an int.
I.e, i have int n=5 , and want to later convert it so that it is treated as a float until the code specifies otherwise. How can I do that?
Can i just do: n = (float)n?
Thanks in advance!
you can convert int type var to float type val by type casting and also float to int. it is the best way to convert data type.
for int to float =>
int a = 6;
float b = (float)a;
and for float to int =>
float a = 6.0;
int b = (int)a;
u can use type casting
int x=11;
float = (float)x;
Related
Based on C's implicit casting rule, data types are converted to higher type.
Nerveless when I try:
float a;
int b;
a = b = 3.4;
The output is always an integer number for both a and b.
Can I know the reason behind this? Why is it not converting int to float?
Assignment (=) has right-to-left associativity (see operator precedence) so
float a; int b; a = b = 3.4; is the same as:
float a;
int b;
b = 3.4; // b is now 3 (since it can only hold integer values)
a = b; // a is now 3.f
This question already has answers here:
How to verify if a void pointer (void *) is one of two data types?
(3 answers)
Closed 3 years ago.
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.
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;
I am not able to understand how the following code is working:
#include<stdio.h>
int main() {
int i = 100;
int *a = &i;
float *f = (float *)a;
(*f)++;
printf("%d", *a); //getting some garbage value
}
'f' is pointing to the same memory location as that of 'a'. So, (*f)++ should in turn increment the value of i to 101. Where am I going wrong?
Floats and ints are stored with different binary representations. When you cast a float to an int or vice versa, the compiler takes care of this for you. But in your case, you're casting an int* to a float*, so you're modifying a float that has the wrong value, since the binary representation didn't get converted.
Having a little difficulty with pointers. I have to store a float in an array of unsigned ints and be able to pull it out.
I know there is a special way to cast this so I don't reorder the bits, I think this is the correct way to store it when I want to put it into the array:
float f = 5.0;
int newF = (int *) f;
arrayOfInts[0] = *newF
Which seems to successfully store the value in the array.
However, at some point I have to pull the value back out of the array of ints, this is where my confusion comes in (assuming I inputed into the array correctly)
float * f = (float *) arrayOfInts[0]
int result = *f;
however, that gives me the warning: 'cast to pointer from integer of different size'
I can't really think of how to solve that without some sort of long cast.. which doesn't seem right..
I don't want to lose the value or damage the bits.. obviously It will lose decimal point precision.. but I know theirs some way to safety convert back and forth
I have to store a float in an array of unsigned ints and be able to pull it out.
Use a union and unsigned char[]. unsigned char is specified to not have any padding and all bit combinations are valid. This is not always true of many other number types. By overlaying the float with unsigned char[], code can examine each "byte" of the float, one at a time.
union {
float f;
unsigned char uc[sizeof (float)];
} x;
// example usage
x.f = 1.234f;
for (unsigned i = 0; i<sizeof x.uc; i++) {
printf("%u:%u\n", i, 1u*x.uc[i]);
}
Sample output: Yours may vary
0:182
1:243
2:157
3:63
float --> unsigned char[] --> float is always safe.
unsigned char[] --> float --> unsigned char[] is not always safe as a combination of unsigned char[] may not have a valid float value.
Avoid pointer tricks and casting. There are alignment and size issues.
// Poor code
float f = 5.0f;
int newF = *((int *) &f); // Conversion of `float*` to `int*` is not well specified.
Code can also overlay with fixed-width no-padding types like (u)int32_t if they exist (they usually do) and match in size.
#include <stdint.h>
union {
float f;
uint32_t u32;
} x32;
#include <assert.h>
#include <inttypes.h>
// example usage
assert(sizeof x32.f == sizeof x32.u32);
x32.f = 1.234f;
printf("%" PRNu32 "\n", x32.u32);
}
Example output: yours may vary
1067316150
To convert a float to an int
float fval = 123.4f;
int ival = *(int*)&fval;
To convert back
int ival = /* from float */
float fval = *(float*) &ival;
it won't work if float and int are different sizes, but presumably you know that already. The unsigned char union method outlined in other answer for chux is more robust, but over-complicated for what you probably want to do.
When i was dividing a int variable with a float variable, it was giving correct answer. I thought data types needed typecasting to be, for one data type cant operate with another kind of data type. If not, then how does the processor decide it can operate int and float, but not int and char.
In cases where you have not explicitly typecast the variables, implicit typecasting is performed. An example of how implicit typecasting works :
int variable = (int / float ) -> variable will have an integer value.
float variable = (int / float ) -> variable will have a float value.
An example using char and int :
int a=0;
char c='c';
a = c;
cout<<"Ascii value of"<<c<<"is "<<a;