Marc said:
Come on, thats obvious:
I want an integer value close to the float value (if its in the
range).
It's not so obvious what "close to" means exactly. Do you expect the
fractional part to be discarded, sometimes called
"round-towards-zero" (which is what actually happens), or round-down,
round-up, round-to-nearest?
If its out of range. Well what will it do? (largest-smallest
int, random...)
Undefined in C++.
That the conversion is done and that homogenously over the whole value
range of the int.
Well, that's not exactly what I meant. An example:
float f = 0.1f;
f *= 10.0f;
int i = static_cast<int>(f);
You might expect i to contain 1 now, but depending on the
implementation, it might instead contain 0. That's because 0.1f cannot
be represented exactly as a floating point value, so the variable will
contain a value slightly greater or smaller than 0.1. That means that
the result of the multiplication might e.g. be someting like 0.999998,
in which case discarding the frational part results in 0. If you expect
round-to-nearest, you'd expect a result of 1.
Depends on what it will do. Ie there isn't any caveeat.
Well, there is none, if you know what to expect ;-)