The pow () function works without math libraries

It seems that in some programs / compilers, the pow() function works without any math libraries. Only with <iostream> . But in others he complains. Include math functions in the <iostream> library or elsewhere?

+5
source share
2 answers

Headings can - and often - include other headings. Standard library headers are no exception.

Even if you decide not to include the heading (let him call it a ) that you depended on, it is possible that the heading turned out to be included by another heading (let it be called b ) that you included. In this case, your program does not guarantee continuation of work if the header b ever changed so as not to include a . This is why you should always include all the headers you depend on, even if your program works without including some of them.

All different versions of different implementations of the standard library are different, and therefore a in one version may include b , and a in another version may also not include b . The same applies to all APIs that have multiple versions of implementations.

+3
source

As explicitly stated here, pow IS in the cmath header. http://www.cplusplus.com/reference/cmath/pow/ .

Which includes in <iostream> , I just checked that the current version of GCC (I use archlinux, so it is the latest) does not include cmath in any of the iostream internally includes.

In any case, even if it works, it is against the standard. Include <cmath> explicitly.

+1
source

Source: https://habr.com/ru/post/1246954/


All Articles