0

I wrote a program that calculates the cubes of the first five numbers:

#include <stdio.h>
#include <math.h>
int main() {
  for(int i = 1; i <= 5; i++) {
    printf("%d ",pow(i,3));
  }
}

This does not work. It prints:

0 0 0 0 0

However, using float it works:

#include <stdio.h>
#include <math.h>
int main() {
  printf("%f\n",pow(3,3));
  for(float i = 1; i <= 5; i++) {
    printf("%f ",pow(i,3));
  }
}

And prints:

1.000000 8.000000 27.000000 64.000000 125.000000

What is the problem with integers? Why does this only work with floating point numbers?

  • 1
    Did you check the documented return value type of pow? https://en.cppreference.com/w/c/numeric/math/pow Using the wrong format specifier in printf causes undefined behavior. Knowing that, what is your question? – Yunnosch Jul 09 '18 at 22:13
  • 1
    Your issue doesn't have anything to do with `pow` – user3483203 Jul 09 '18 at 22:14
  • @user3483203 Please elaborate. I do see a relevance of `pow()` and its specification. – Yunnosch Jul 09 '18 at 22:15
  • Was responding to the question, not your comment, sorry if that was unclear. The issue is because of the `printf`. It doesn't really have anything to do with `pow`, since he is giving it the same inputs in both cases. – user3483203 Jul 09 '18 at 22:16
  • The syntax for `pow()` is `double pow(double x, double y);` Notice that nothing in the parameters nor the returned value is `int`. And when passing it `float` parameters, they are automatically promoted to `double`. Strongly suggest reading/understanding the MAN pages for those C library functions you are using. – user3629249 Jul 09 '18 at 22:30
  • if you really want to use `float`, then you 'should' use: `float powf(float x, float y);` – user3629249 Jul 09 '18 at 22:32
  • Your question isn't a question at all--merely a correct observation. If your real question is "why", the simplest answer is that integer exponentiation doesn't come up that often in real life, so they didn't bother putting it into the library. It's easy to find code to do this, but make sure you find code that uses the correct shift-and-square method rather than naive repeated multiplication. – Lee Daniel Crocker Jul 09 '18 at 22:51

1 Answers1

2

First of all pow(i,j) returns double value, not an integer. Try this code

int main() {
     for(int i = 1; i <= 5; i++) {
         printf("%f ",pow(i,3));
     }
return 0;
}
user2760375
  • 2,238
  • 2
  • 22
  • 29
  • The question is, *why* float should be used, *not* what should be used. – Mysterious User Jul 09 '18 at 22:22
  • 1
    Or you can use `printf("%d ",(int)pow(i,3));` but don't be surprised if it sometimes prints a value that you don't expect. – stark Jul 09 '18 at 22:23
  • @MysteriousUser go through this link http://pubs.opengroup.org/onlinepubs/007904975/basedefs/math.h.html . The pow() supports only double and float returns only. – user2760375 Jul 09 '18 at 22:26
  • @MysteriousUser Since `pow` returns float, that's the type you *must* use in the `printf` format statement. `printf` is special (or, you might say, deficient): it has no way of noticing that you passed a `double`, but `%d` expects an int. – Steve Summit Jul 09 '18 at 22:44