0
    int n = 15;
    unsigned long long int s,a;
    s=pow(10,n) + 2;
    a=1000000000000000+2;
    cout<<s<<endl;
    cout<<a;

here the output is 10000000000000001 and 1000000000000002 how is this even possible.I am using code blocks on windows 64 bit.

Swastik Singh
  • 181
  • 1
  • 1
  • 9
  • This is one issue of using pow with integers – drescherjm Jan 12 '20 at 16:04
  • @Fredrik it does in my system – Swastik Singh Jan 12 '20 at 16:06
  • @Fredrik there's no problem at all with that code, automatic conversion from integer to float/double takes care of it. You could enable warnings for automatic conversion `-Wconversion` in GCC/clang, but the normal behavior is to just do the conversion. – Marco Bonelli Jan 12 '20 at 16:08
  • @Fredrik To be fair, `-Wconversion` is a fairly uncommon flag. It's not included in `-Wall` nor `-Wextra`. – HolyBlackCat Jan 12 '20 at 16:12
  • To be clear, the erroneous outputs you've shown are for `n=16` (as indicated by the title) and not `n=15`, as you've shown in your code. `1000000000000001` (16 digits) is exactly representable as a double. `10000000000000001` (17 digits) is not - the closest doubles are `10000000000000000` and `10000000000000002`. – J... Jan 12 '20 at 16:16
  • 1
    duplicates: [Why pow(10,5) = 9,999 in C++](https://stackoverflow.com/q/9704195/995714), [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)... – phuclv Jan 12 '20 at 16:20

0 Answers0