- code
#include <iostream>
int main() {
int num = 2147483647;
std::cout << (int)(float)num << std::endl;
return 0;
}
- output
./main
-2147483648
-2147483648
I know that a float to int conversion is safe, but an int to float conversion is not. I also know that real floating point cannot be expressed accurately. It seems very dangerous to see overflows even though the maximum value(except inf) of int is not exceeded.
I want to be safe when converting int to float or float to int no matter what arbitrary number comes in.
Which way is best to handle it? Also, what other exceptions should I consider?
The above question isn't clear and doesn't fit the stackoverflow question, so I'll fix it specifically.
Numbers in normal categories like 2147483647 also overflow. What range should I handle?