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 OverflowHow do I cast my int to float?
How to actually convert float to int?
c - Convert integer to float while also dividing down the scale, without data loss - Game Development Stack Exchange
Are int to float conversions slow?
How to convert int to float in C?
Does implicit conversion always work in int to float in C?
Can we convert integer to float in C program automatically?
Videos
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
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);
}
I think you're making this too complicated. The trick to preserving precision in your floating point numbers is just to keep their magnitude small. No chaining through doubles or two-stage fractional/whole conversion required.
If you want millimetre accuracy, single-precision floats will keep that as long as your numbers are less than about 16 km.
(If your play space is even smaller than that, then you actually lose precision with your millimetre integer scheme, so you might as well stick with floats throughout)
"But my world is bigger than 16 km" — that's not a problem, because the immediate neighbourhood of your camera, where millimetre-sized errors might actually be visible, is not.
When you compute the matrices to send down the pipe to your GPU, all you need to do is form the translation component as a relative offset from the camera.
Vector3Float translation = (object.integerPosition - camera.integerPosition) / 1000f;
So as far as the GPU is concerned, your camera is always at (0, 0, 0) this way, giving you maximum precision in any direction you look. Objects a very long way from the camera can still suffer precision loss, but a 1 mm offset over a 16 km distance barely changes the direction of the vector at all — your rounding to the pixel grid of the screen is actually a much larger source of error than anything you'd get from floats in this scenario.
The other members of the object transformation matrices will already tend to be near the -1...1 range where floats have very high precision, so you don't have to do anything fancy with them.
This goes in general for other values too. If it's an absolute measure, like a position in the world or a time stamp, use integers so that you have the same precision no matter where/when you are. If it's a relative measure, like an offset/displacement, duration (like deltaTime), velocity, etc. then use a float. This gets you consistent relative precision, where the errors stay small in proportion to the quantity you're measuring.
In a comment I mentioned in passing that you could switch to representing 1024ths of meters instead of millimeters. That also helps with representability generally. Your code would look like this:
int i = 1234567890;
float f = ((float)i) / 1024.f;
You don't need to worry about representability at all here. The temporary float value will already be the best representation of i possible in a single precision float, and the divisor 1024.f is not only a perfectly representable floating point number (feel free to write it with the wonky hexadecimal floating literal notation if you prefer) but will actually reduce to a trivial subtraction of the exponent of your floating point value when executed.
Of course you (and your compiler) will probably notice that a division isn't necessary at all at this point. The above code could we written with a similar multiplication (or even maybe some fancy hardware instructions):
int i = 1234567890;
float f = ((float)i) * 0x1p-10f;
(0x1p-10f is just a direct representation of the value 1.f / 1024.f)
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?