To convert an integer to a float in Python you can use the following:
float_version = float(int_version)
The reason you are getting 0 is that Python 2 returns an integer if the mathematical operation (here a division) is between two integers. So while the division of 144 by 314 is 0.45~~~, Python converts this to integer and returns just the 0 by eliminating all numbers after the decimal point.
Alternatively you can convert one of the numbers in any operation to a float since an operation between a float and an integer would return a float. In your case you could write float(144)/314 or 144/float(314). Another, less generic code, is to say 144.0/314. Here 144.0 is a float so it’s the same thing.
How to convert int to float in C? - Stack Overflow
Int to float convertion[Solved]
c++ - Convert int to float: how it is done - Stack Overflow
Union - convert int to Float
How to convert int to float in C?
How does arithmetic conversion help in int to float conversion?
Can you use functions to perform int to float conversion?
Videos
To convert an integer to a float in Python you can use the following:
float_version = float(int_version)
The reason you are getting 0 is that Python 2 returns an integer if the mathematical operation (here a division) is between two integers. So while the division of 144 by 314 is 0.45~~~, Python converts this to integer and returns just the 0 by eliminating all numbers after the decimal point.
Alternatively you can convert one of the numbers in any operation to a float since an operation between a float and an integer would return a float. In your case you could write float(144)/314 or 144/float(314). Another, less generic code, is to say 144.0/314. Here 144.0 is a float so it’s the same thing.
Other than John's answer, you could also make one of the variable float, and the result will yield float.
>>> 144 / 314.0
0.4585987261146497
Integer division truncates, so (50/100) results in 0. You can cast to float (better double) or multiply with 100.0 (for double precision, 100.0f for float precision) first,
double percentage;
// ...
percentage = 100.0*number/total;
// percentage = (double)number/total * 100;
or
float percentage;
// ...
percentage = (float)number/total * 100;
// percentage = 100.0f*number/total;
Since floating point arithmetic is not associative, the results of 100.0*number/total and (double)number/total * 100 may be slightly different (the same holds for float), but it's extremely unlikely to influence the first two places after the decimal point, so it probably doesn't matter which way you choose.
integer division in C truncates the result so 50/100 will give you 0
If you want to get the desired result try this :
((float)number/total)*100
or
50.0/100
It is an effort-taking job, but any CPU that has floating-point support will provide an instruction that does it.
If you had to convert a 2's complement int to IEEE float format for yourself, you would:
- take the integer base-2 log (closely related to the index of the highest set bit), which gives you the exponent. Offset this and store it in the exponent bits of the float.
- copy the top
nbits of the int (starting from the bit after the first set non-sign bit) into the significand of the float.nis however many bits of significand there are in afloat(23 for a 32 bit single-precision float). If there are any remaining bits in theint(that is, if it's greater than 224), and the next bit after the ones you have room for is1, you may or may not round up depending on the IEEE rounding mode in operation. - copy the sign bit from the
intto thefloat.
If you look at the assembly
int i = 5;
000D139E mov dword ptr [i],5
float f = i;
000D13A5 fild dword ptr [i]
000D13A8 fstp dword ptr [f]
fild is what does the magic
int main (void)
{
string text = get_string("Text: ");
int Letters = count_letters(text);
int words = count_words(text);
int sen = count_sen(text);
int L = (100/words) * Letters;
int S = (100/words) * Letters;
int index = 0.0588 * L - 0.296 * S - 15.8;
int index1 = round(index);
printf("%i\n", Letters);
printf("%i\n", words);
printf("%i\n", sen);
printf("%i\n", index1);
}
If I have a value that is either 1 0 or -1, should I convert it to float and multiply with a float constant constant or index an array and add one to the value?