I am talking about converting floats that already store integer values (like 10.0)
I need to do this because I want to create a simple function that reverses a given integer, and it uses a line of code that looks something like this: ans += x % (int)pow(10, i);
(the % operator needs both arguments to be integer, and pow returns a double)
C++ is not very happy doing this conversion properly. For pow(10, 4)
, I get a value of 9999, which is very irritating. Why this error occurs is obvious, 10^4 must have been stored as 9999.999999... or something like that. I could possibly fix this error by using the lround()
function, but that would be a less than optimal solution because what if I overlook a similar error like this?
Is there a way to do mathematics in C++ without worrying about such trivialties? If not, what language should I choose to do computation like this? I have briefly used the bigInt library in python. Any suggestions regarding which language and library to use to tackle this issue would be very helpful.