0

This code is part of an original code

Why the output of this code displays the number 24?

The output of this Code should be number 25!

Where's the problem with this code?

The compiler used is CodeBlocks

Thanks

int A=7;
A = pow((((A+1)/2)+1),2);
printf("%d", A);
o-0
  • 1
  • 4
  • Please do not post code as images or links to images - [reasoning](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Please edit your question and paste in the code as formatted text. – kaylum Feb 29 '20 at 10:18
  • My guess is that it is essentiall 25 if you print it as a double but most likely 25 cannot be represented as a real number therefore it is something like 24.999999... and if you print it as an int it will be 24. – Eraklon Feb 29 '20 at 10:21
  • 1
    I sincerely doubt that "the output of this code displays the number 24"!!! – goodvibration Feb 29 '20 at 10:23
  • 2
    @Eraklon: I believe that any number which can be represented as a quotient of an integer and a power of 2 (in this case - 25 / 2 ** 0), is stored accurately in floating-point format. – goodvibration Feb 29 '20 at 10:25
  • @goodvibration Yes it is not likely that is the case, but the only one I can think of. – Eraklon Feb 29 '20 at 10:30
  • 2
    [Why does pow(5,2) become 24?](https://stackoverflow.com/q/22264236/995714), [Why does pow(n,2) return 24 when n=5, with my compiler and OS?](https://stackoverflow.com/q/25678481/995714), [Why pow(10,5) = 9,999 in C++](https://stackoverflow.com/q/9704195/995714), [Why does gcc compiler output pow(10,2) as 99 not 100?](https://stackoverflow.com/q/25474351/995714), [Why a+=b*pow(10,c-i-1) == 99?](https://stackoverflow.com/q/39756656/995714)... – phuclv Feb 29 '20 at 10:51
  • @Eraklon: 25 cannot be represented as a real number? Really? You think the set of real numbers does not include 25? Or the set of numbers representable in floating-point does not include 25? – Eric Postpischil Feb 29 '20 at 12:13
  • @EricPostpischil Obviously talking about the limitation of what FP could represent. And as I said it is a guess I not checked or sure of that in this (otherwise I would have give it as an answer), but the only thing that came to my mind. And as seeing *phuclv* linked answer I may even correct here. – Eraklon Feb 29 '20 at 12:37
  • @Eraklon: It is unfathomable why one might think floating-point cannot represent 25. Floating-point is simply a sign and some digits in some base scaled by some exponent (C 2018 5.2.4.2.2). 25 is representable in base 2, 10, 16, or whatever, and the required ranges of floating-point characteristics (5.2.4.2.2 12) show that 25 is within them. 25 is encoded with 01000001110010000000000000000000 in IEEE-754 binary32. Saying that maybe it cannot be represented as a real number or a floating-point number is as bizarre as saying maybe 25 cannot be represented as an integer.It makes no sense. – Eric Postpischil Feb 29 '20 at 12:47

0 Answers0