-4

Here is the problem result Image

There result is 3024 but right result will be 3025; what is the problem please help ! thanks for advance.

  • 2
    [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/995714). Copy the code and output text and paste here in proper format – phuclv May 05 '22 at 03:35
  • 2
    duplicates: [Why the result of pow(10,2) 99 instead of 100?](https://stackoverflow.com/q/54057687/995714), [Why does pow(5,2) become 24?](https://stackoverflow.com/q/22264236/995714), [Why pow(10,5) = 9,999 in C++](https://stackoverflow.com/q/9704195/995714) – phuclv May 05 '22 at 03:36
  • [What every computer scientist should know about floating point arithmetic](https://www.itu.dk/~sestoft/bachelor/IEEE754_article.pdf). – OTheDev May 05 '22 at 04:06

2 Answers2

1

Because pow is a floating point function that does its job using logarithms. It converted your 55 to floating point, then did the pow function using logarithms. The result was probably 3024.999999, but when you converted back to an integer, it got truncated.

If you want to square an integer, use result = result * result;. If you must use pow for integer numbers, do

result = pow(result,2)+0.5;
Tim Roberts
  • 48,973
  • 4
  • 21
  • 30
0

I tried your code (ubuntu), the result is 3025. According to another answers, the problem is about the purpouse of pow implementation, with small calculations can be inaccurate, so, try with

result = result*result
Juan Botero
  • 71
  • 1
  • 2
  • 12