This is generally (i.e., always) a bad idea. As you suspected, the comparison from 3 to 3.0000001 will indeed fail.
What most people do, if an int-float comparison is really necessary, is pick some threshold of tolerance and go with that, like so:
int x = 3;
float y = 3.0;
// some code here
float difference = (float) x - y;
float tolerableDifference = 0.001;
if ((-tolerableDifference <= difference) && (difference <= tolerableDifference)) {
// more code
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…