my_var = (int)my_var;
As simple as that. Basically you don't need it if the variable is int.
Answer from Zach P on Stack OverflowVideos
How to convert float to int in C?
How to convert int to float in C?
How can I safely convert float to int in C?
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);
}
In Java it is not allowed, is it allowed in C? Why so?
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
13F / 10 cannot be represented precisely with binary encoded float variables.
If you try and print more decimals, you will see what happens:
#include <stdio.h>
int main(void) {
float current_i;
float floating_part;
int float_to_int_part;
current_i = 13;
printf("current_i = %.16f\n", current_i);
current_i = current_i / 10;
printf("new current_i = %.16f\n", current_i);
floating_part = (current_i - (int)current_i);
printf("floating_part = %.16f\n", floating_part);
float_to_int_part = (int)(floating_part * 10.0);
printf("float_to_int_part = %d\n", float_to_int_part);
return 0;
}
Output:
current_i = 13.0000000000000000
new current_i = 1.2999999523162842
floating_part = 0.2999999523162842
float_to_int_part = 2
Note also that float values are converted to double when passed to printf, and the expression (int)(floating_part * 10.0) is evaluated using double arithmetics because 10.0 is a double constant. These values would not be represented precisely as double either, but converting float to double makes the imprecision much more visible and the approximation for the float representation is smaller than the actual value 1.3 whereas with double type, the representation is slightly larger, so the final conversion produces the expected value:
#include <stdio.h>
int main(void) {
double current_i;
double floating_part;
int float_to_int_part;
current_i = 13;
printf("current_i = %.23f\n", current_i);
current_i = current_i / 10;
printf("new current_i = %.23f \n", current_i);
floating_part = (current_i - (int)current_i);
printf("floating_part = %.23f\n", floating_part);
float_to_int_part = (int)(floating_part * 10.0);
printf("float_to_int_part = %d\n", float_to_int_part);
return 0;
}
Output:
current_i = 13.00000000000000000000000
new current_i = 1.30000000000000004440892
floating_part = 0.30000000000000004440892
float_to_int_part = 3
For these computations to be evaluated reliably, you would need to use a decimal based representation for floating point values. The C Standard does not specify such types by default but as an extension described in a Technical Report . Types _Decimal32, _Decimal64 and _Decimal128 might be available on your system (for example gcc supports them on selected targets).
This answer is not a proper solution, and I do not know if one exists. As said below, this is a "band-aid" solution, as double precision does not mean it will fix the issue in all cases (it only happened to 'fix' it in this one)
Try with double instead of float. And see this as bolov stated.
It works here
You also do not need the (int) cast as it is implicit when you declare float_to_int_part as int