0

I am using Dev C++ 4.9.9.2 with default compiler as provided in Dev C++ options. Consider the following two programs:

First:

#include <iostream>
#include <math.h>
using namespace std;
int main (){
    cout <<(int)pow (10,2);    //100
    return 0;
}

Second:

#include <iostream>
#include <math.h>
using namespace std;
int main (){
    int k = 2;
    cout <<(int)pow (10,k);    //99
    return 0;
}

Is that a bug in implicit coercion from int to datatype of arument of called pow in the compiler shipped with Dev C++ 4.9.9.2? OR is that an expected behaviour? Exactly which version of pow is called (I tried to debug but was unable to locate called one)?

Note : If we replace math.h by cmath, the program does not compile with a warning of ambiguous call to pow (seems a better condition). Also on newer version of Dev C++ on the same system, the output is correct in both programs.

Edit1 : I don't think that my question is duplicate of this question as I am giving same input but getting different result. 2 and k both are integers.

Edit2 : I feel that people are less interested in understanding my question than seeing my rep. That's why they marked the question as a duplicate. They don't bother that I am giving the same inputs in different ways but getting different results.

Community
  • 1
  • 1
Birendra Singh
  • 842
  • 11
  • 19
  • 1
    Looks like dev c++ 4.9.9.2 was released in 2005, who cares about its strange bugs, use the newer one :) – Paul Rooney Sep 18 '17 at 06:06
  • If you closely see the result of `pow` (as it is a `double` value), the double value is usually not stored as it is, an approximation is stored. So the value returned might be 99.9999999999998 or something like that, which when you convert to `int` leaves you with only `99`. – Ahmad Khan Sep 18 '17 at 06:25
  • @MuhammadAhmad why did it not happen in the case I passed **2** to `pow` for second parameter. – Birendra Singh Sep 18 '17 at 06:42
  • 1
    [Why pow(10,5) = 9,999 in C++](https://stackoverflow.com/q/9704195/995714) – phuclv Sep 18 '17 at 06:44
  • 1
    probably because one is the builtin version of gcc and one is the pow function from stdlib – phuclv Sep 18 '17 at 06:44
  • Read http://floating-point-gui.de/ – Basile Starynkevitch Sep 18 '17 at 06:53
  • Regarding the warning, are you aware of [this](https://stackoverflow.com/a/24749982/4944425)? The same may hold for your question, it looks like your compiler chooses different overloads for literals and variables when is included. – Bob__ Sep 18 '17 at 08:06
  • @Bob__ I understand that. I just want a reference. – Birendra Singh Sep 28 '17 at 08:22

0 Answers0