0

I'm trying out the pow-function in c, and something goes wrong.

#include <math.h>
#include <stdio.h>
int main(){
    int highnumber = 18; 
    int input = 344;
    int diff[18];
    for(int j = 1; j<=highnumber; j++){
       diff[j-1] = input - pow(j,2);
       printf("%d\n",diff[j-1]);
    }
}

I get the output:

343
340
335
328
319
308
295
280
263
244
223
200
175
147
119
88
54
19

Now the last two entries, doesn't make sense to me. They should be 55 and 20. Anyone knows why this happens?

Okay, I change the indexing, and show that pow function works:

#include <math.h>
#include <stdio.h>
int main(){
    int highnumber = 18; 
    int input = 344;
    int diff[18];
    for(int j = 0; j<=highnumber; j++){
       diff[j] = input - pow(j,2);
       printf("%d\n",diff[j]);
    }
    int a = input - pow(17,2);
    int b = input - pow(18,2);
    printf("%d\n%d",a,b);
}

Output:

344 343 340 335 328 319 308 295 280 263 244 223 200 175 147 119 88 54 19 55 20

jjm
  • 431
  • 3
  • 19
12344343243
  • 43
  • 1
  • 10
  • 1
    Although the indexing is a bit weird (`j = 0; j < highnumber; j++){ diff[j] ..` is more idiomatic), it isn't wrong or UB. This is not a duplicate of the other question – Kninnug Mar 24 '17 at 10:30
  • It is working fine for me. – Marievi Mar 24 '17 at 10:31
  • @Kninnug is right, this is not a duplicate. The indexing is pretty fine. – Marievi Mar 24 '17 at 10:33
  • This doesn't make sense. pow(17,2) is defined, and if I put it outside the for loop, this gives the correct result. I can also change the starting integer from 0 instead of 1, so I get 344 with from the beginning. it doesn't change the fact that the two last integers are wrong. – 12344343243 Mar 24 '17 at 10:34
  • More than likely, there is a typo in your original code that wasn't copied here or there's a bug in your compiler. – Nathan Mar 24 '17 at 10:34
  • This is the exact copy, and I also tried to run the code in cmd, still same result. – 12344343243 Mar 24 '17 at 10:37
  • It looks like, when we go above 16^2 = 256 , it crashes.. – 12344343243 Mar 24 '17 at 10:50
  • Please post your actual code. The code you have provided doesn't exhibit the behavior you describe, but the behavior you expect. – moooeeeep Mar 24 '17 at 10:59
  • no this is the exact code I compile and execute. And the output is exact copy of the output I'm given. – 12344343243 Mar 24 '17 at 11:01
  • Then please give details on which compiler and which OS you use and how you build and run it. Here you can see it works: http://ideone.com/UGw6r0 – moooeeeep Mar 24 '17 at 11:13
  • [https://ideone.com/sixwi9](https://ideone.com/sixwi9) – jjm Mar 24 '17 at 11:17
  • 1
    It's a well-known problem that `pow(a, b)` doesn't always give you the result you expect when `b` is a small integer like 2 or 3. This is because `pow` may use logarithms instead of straight multiplications. It's likely that in this case, the compiler is doing a compile-time optimization when `a` is constant (that is, in the last two lines the OP added), and that the compile-time optimization gets a slightly different answer than the run-time `pow` call. – Steve Summit Mar 24 '17 at 11:17
  • @SteveSummit can you provide a reference for this? – moooeeeep Mar 24 '17 at 11:23
  • 1
    @moooeeeep See the footnote at http://c-faq.com/fp/pow.html . – Steve Summit Mar 24 '17 at 11:40
  • So I'm using something called git-bash. – 12344343243 Mar 24 '17 at 12:07
  • i use gcc to compile and my OS is windows 7 – 12344343243 Mar 24 '17 at 12:08
  • okay the compiler is mingw64 – 12344343243 Mar 24 '17 at 13:13
  • I fixed it using (int) pow(i,2) – 12344343243 Mar 24 '17 at 13:37
  • @12344343243 I'm surprised that casting to `(int)` helped. In general the problem with calling `pow()` to square things is that it sometimes returns an answer that's a tiny bit too small. But since conversion from double to int *truncates* (that is, since it does *not* round), you sometimes get a result that's too small by 1. – Steve Summit Mar 24 '17 at 13:48
  • 1
    @moooeeeep and 12344343243: see these other questions: http://stackoverflow.com/questions/18155883/ , http://stackoverflow.com/questions/16923249/ . – Steve Summit Mar 24 '17 at 13:48
  • 1
    @12344343243 To see what's going on, try running `for(int j = 0; j<=highnumber; j++) printf("%d %.20f\n", j, pow(j, 2));`. – Steve Summit Mar 24 '17 at 13:52
  • You're right all. it's the integer input to pow that's the problem. (int) infront fixes the last two entries, but then messes up some of the others. As I only needed a square function, I just created this one. How to solve this problem in general, I don't know. – 12344343243 Mar 24 '17 at 14:43
  • @SteveSummit Nice - thanks for these resources! These questions you've linked seem to better fit for this being a duplicate. – moooeeeep Mar 24 '17 at 19:40
  • Possible duplicate of [return value of pow() gets rounded down if assigned to an integer](http://stackoverflow.com/questions/7937286/return-value-of-pow-gets-rounded-down-if-assigned-to-an-integer) – Dolda2000 Mar 25 '17 at 03:24
  • Possible duplicate of [unusual output from pow](http://stackoverflow.com/questions/16923249/unusual-output-from-pow) – moooeeeep Mar 25 '17 at 08:08
  • Possible duplicate of [Strange behaviour of the pow function](http://stackoverflow.com/questions/18155883/strange-behaviour-of-the-pow-function) – Steve Summit Mar 25 '17 at 15:35

0 Answers0