2
#include <iostream>
#include <string>
#include <sstream>
//#include <bits/stdc++.h>
#include <iomanip>      // std::setprecision
#include <math.h> 
using namespace std;

I want to remove the header #include <bits/stdc++.h>, because it significantly slows down my compile time.

When I remove it, I get the following error:

error: cannot convert ‘long double*’ to ‘double*’ for argument ‘2’ to ‘double modf(double, double*)’
       fractpart = modf(val, &intpart);

I think the problem is with a missing header file, but have no clue which one it is.

The code I'm getting the error for is:

fractpart = modf(val, &intpart);
if (fractpart != 0) {
    throw Error("ERR");
}
Karina Kozarova
  • 1,145
  • 3
  • 14
  • 29
  • Your error doesn't have anything to do with the header files included. What type is `intpart?` – user0042 Oct 18 '17 at 16:27
  • 2
    You need to differentiate between compile time and run-time, but in either case you should never have included that header - it's an implemantation feature that might change or be removed at any time. –  Oct 18 '17 at 16:27
  • 2
    More reasons not to `#include `: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h – user4581301 Oct 18 '17 at 16:41

1 Answers1

11

The solution for problems like this is to consult a suitable reference for the function in question. One well-regarded C++ reference site is cppreference.com. In this case, its reference for modf starts with:

Defined in header <cmath>

There's your answer.

Compare the above reference for the C++ version (a family of overloaded functions) defined in the C++ header <cmath> with reference for the C version defined in the C header <math.h>:

float modff( float arg, float* iptr );
double modf( double arg, double* iptr );
long double modfl( long double arg, long double* iptr );

C doesn't have function overloading, so modf in <math.h> is only the double version. <cmath>, being C++, declares all the 3 C++ overloads (float, double, long double), of which you're using the last one.

This is actually one of the reasons to stay clear of C standard library headers (<*.h>) and use C++ standard library ones (<c*>).

Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
  • By adding cmath it works, but the c++ reference(http://www.cplusplus.com/reference/cmath/modf/) says to include – Karina Kozarova Oct 18 '17 at 16:29
  • 3
    @KarinaK cplusplus.com has a rather poor reputation regarding its accuracy. Anyway, see my previous comment about the difference between the two headers. – Angew is no longer proud of SO Oct 18 '17 at 16:30
  • 2
    @Karina K `math.h` is deprecated in modern versions of the C++ standard. The *correct* C++ header is `cmath`. Also; stay away from cplusplus.com - too much bad info there. Prefer cppreference.com. See also: http://en.cppreference.com/w/cpp/header – Jesper Juhl Oct 18 '17 at 16:40
  • 1
    @KarinaK it says to include cmath (look at the upper right corner), but indeed shows math.h in the example. The example seems to show C code, maybe because they explain the C and the C++ version on one page. –  Oct 18 '17 at 17:56