Why does pow(5,2) become 24? [closed]

pow() returns numbers as floating point. What is really returning is 24.99997 or something similar. But then you truncate it by assigning it to an integer and it comes out as 24. It’s ok to assignit to an integer in this case because you know then answer will always be an integer so you need to round it – not truncate it. The normal way to round to an integer is to add 0.5 like so:

int answer;
answer = pow(number1,number2)+0.5;

On the other hand the square root one probably should NOT be assigned to an integer – almost every square root will have decimal places.

Leave a Comment