Why is sqrt working without cmath?

Why does this programn work without cmath? The code works fine. I was playing around and thought cmath was necessary. However, the code works the same without it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
//#include <cmath>
#include <Windows.h>
using namespace std;

// Function Prototype
double sqrRoot(double);

int main()
{
	double num, result;

	cout << "Enter a number and this program\n"
		<< "will return its square root: ";
	cin >> num;

	try 
	{
		result = sqrRoot(num);
		cout << "The square root of " << num << " is " << result << endl;
	}

	catch (const char *exceptionString)
	{
		cout << exceptionString;
	}
	cout << "End of program.\n";
	system("pause");
	return 0;
}


double sqrRoot(double num)
{
	if (num <= 0 || sqrt(num) != static_cast<int>(sqrt(num)))
		throw "Error: The number is not a perfect square.\n";

	return sqrt(num);
}
Look inside of windows.h, I imagine cmath is included as a child header in it or one of its #includes. I'm on Unix so I don't have a copy of it but an easy way to find out if that's the case would be to comment out #include <Window.h> and see if it still compiles.
> Why does this programn work without cmath?

A standard C ++ header may include other standard C++ headers (either in full or in part). In this particular implementation, the declaration of at least one of the overloads of std::sqrt was present in some header included by <iostream> http://rextester.com/HOB4139


> I was playing around and thought cmath was necessary.

It is necessary.
The entities in the C ++ standard library are defined in headers, whose contents are made available to a translation unit when it contains the appropriate #include preprocessing directive


On some implementations, we may get an error: http://coliru.stacked-crooked.com/a/efb18dbf64b3cab7

Even in the case where it appeared to work, all the overloads of std::sqrt() need not be visible; we may not always be calling the overload that would have been called had we included <cmath>
Okay thank you JLBorges. That makes perfect sense. edge6768 Windows.h would not, respectively, have anything to do with sqrt. Thank you both for the response.
Topic archived. No new replies allowed.