1

I'm writing a simple C++ program using an example from the book: Programming principles and practice using C++.But I can not understand a thing, my book told me that the sqrt() function applies only to objects of type double, so in my program I first read an integer value then i should assign it to double and use the sqrt function.

cout << "Please enter a floating-point value\n"; 
int n; 
cin >> n;   // read a double from the keyboard  into n 

cout << "n == " << n
<< "\nn+1 == " << n + 1
<< "\nthree times == " << 3 * n
<< "\ntwice n == " << n + n
<< "\nn squared == " << n * n
<< "\nhalf of n == " << n / 2
<< "\nsquare root of n == " << sqrt(n)
<< '\n'; 

So I tried to use the sqrt function on an object of type int, and my compiler did not report any error, why?

Piero Borrelli
  • 1,151
  • 2
  • 11
  • 15
  • May be a duplicate of [In C++, why does sqrt() work fine on an int variable if it is not defined for an int?](http://stackoverflow.com/q/19613191/1708801) depends if you include `std_lib_facilities.h` or not. – Shafik Yaghmour Sep 23 '14 at 14:25
  • 1
    Because sqrt() supports long, which your int is probably being typecast to. Also because the sqrt of 16 is 4 – Nathan Wride Sep 23 '14 at 14:25

0 Answers0