Which header file should I use instead of #include <bits/stdc++.h>
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*>
).
Karina Kozarova
Graduated ELSYS @ Sofia, Bulgaria Studying my Bachelor at Fontys University of Applied Sciences @ Eindhoven, Netherlands Working as an R&D Software Developer
Updated on June 05, 2022Comments
-
Karina Kozarova almost 2 years
#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"); }
-
user0042 over 6 yearsYour error doesn't have anything to do with the header files included. What type is
intpart?
-
Admin over 6 yearsYou 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.
-
user4581301 over 6 yearsMore reasons not to
#include <bits/stdc++.h>
: stackoverflow.com/questions/31816095/…
-
-
Karina Kozarova over 6 yearsBy adding cmath it works, but the c++ reference(cplusplus.com/reference/cmath/modf) says to include <math.h>
-
Angew is no longer proud of SO over 6 years@KarinaK cplusplus.com has a rather poor reputation regarding its accuracy. Anyway, see my previous comment about the difference between the two headers.
-
Jesper Juhl over 6 years@Karina K
math.h
is deprecated in modern versions of the C++ standard. The correct C++ header iscmath
. Also; stay away from cplusplus.com - too much bad info there. Prefer cppreference.com. See also: en.cppreference.com/w/cpp/header -
Admin over 6 years@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.