0

Here is a simple C program, which accepts a number from the user and results it's square.

#include <stdio.h>
#include <math.h>
int main()
{
    int number;
    int result;
    printf("\nEnter the number\n");
    scanf("%d",&number);
    result=(pow(number,2));
    printf("\nThe result is %d\n",result);
    return 0;
}

The problem is, whenever i enter 5,25,26 etc as input, the output is 24,624,675 i.e. it decreases by 1 and this does not happen with all numbers. I am using CodeBlocks IDE. I figured out a fix for this problem but I want to know what is happening behind the scene, which is causing this error.

Gaurav
  • 80
  • 1
  • 5
  • Because `pow` is a floating point function and better not to be used for integer calculations. – Eugene Sh. Mar 06 '20 at 21:50
  • @EugeneSh. can you explain why this is not happening with every numbers? – Gaurav Mar 06 '20 at 21:52
  • If you want to square an integer type, just multiply it by itself... `result = number * number;` – Shawn Mar 06 '20 at 21:54
  • Add `printf("%.25f\n", pow(number,2));` to see why. – chux - Reinstate Monica Mar 07 '20 at 00:30
  • 1
    @EugeneSh.: Please do not close these questions about `pow` as duplicates of [that](https://stackoverflow.com/questions/588004/is-floating-point-math-broken). When `pow(25, 2)` returns a result other than 625, this is not due to floating-point arithmetic. It is entirely possible for `pow` to return a correct result (because it is representable) and it is feasible (because there are existing implementations that due it, with high quality in accuracy and performance). The actual cause is due to a low quality implementation of `pow`, not due to any inability of floating-point arithmetic. – Eric Postpischil Mar 07 '20 at 01:41

1 Answers1

0

From this post, Sourav tells us:

pow() takes double arguments and returns a double.

If you store the return value into an int and print that, you may not get the desired result.

As for an explanation on why pow() is returning 1 less that you expect, see this post. Specifically:

pow() works with double numbers. These represent numbers of the form s * 2^e where s is a 53 bit integer. Therefore double can store all integers below 2^53, but only some integers above 2^53. In particular, it can only represent even numbers > 2^53, since for e > 0 the value is always a multiple of 2.

Community
  • 1
  • 1
agillgilla
  • 859
  • 1
  • 7
  • 22
  • 1
    If you say it is a duplicate, then mark it as a duplicate instead of citing other answer. – Eugene Sh. Mar 06 '20 at 21:54
  • 1
    That’s not a correct original. In that problem, the results were too large to be represented, and `pow` was necessarily returning an approximation. In this question, the numbers within a range where these particular results are represented (exactly), and the reason `pow` is returning incorrect results is not because of any problem representing them but because it is a low-quality `pow` implementation that returns wrong results. – Eric Postpischil Mar 07 '20 at 01:35