• Advertisement
Sign in to follow this  

cmath and math.h in Linux

This topic is 3247 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm having problem regarding these 2 math functions using conde::BLocks in Linux: 1. acos 2. sqrt If I include math.h it coudn't find sqrt, and if I include cmath it says that acos is undefined. So I view the cmath header file and inside it includes math.h, but when I try to open math.h it says that file is not found. So ithink my math.h is missing in Linux. Can someone please help. Regards, Dana

Share this post


Link to post
Share on other sites
Advertisement
How are you compiling? To use some math functions you need to include the math library (libm). make sure to give -lm to your compiler.

Share this post


Link to post
Share on other sites
Beware that if you use the C++ variant (i.e. cmath), all functions are in the std namespace, i.e. acos() becomes std::acos() and so on.

Share this post


Link to post
Share on other sites
Post the actual error you get. In particular, it's important to know if the error you're seeing is a compiler or linker error.

Share this post


Link to post
Share on other sites
Remember that C++ is more strongly typed than C. C++ provides overloads of the trancendental functions for the various floating-point types in the std:: namespace. If you're using the C functions that appear in the :: namespace that happen to sneak in when you include <cmath> on C-based OSes like Linux, OS X, and most Unixes, you may not have the overloaded support.

For example, if you try to pass a float to ::acos(double) you will get an error. You should use ::acosf(float). On the other hand, C++ provides both std::acos(float) and std::acos(double), so things will just compile if you use the C++ standard library as documented.

There is a reason why C++ provides <cmath> instead of the <math.h> in the C standard library.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma
if you try to pass a float to ::acos(double) you will get an error


Only when acos() is defined to take a reference, otherwise float can be implicitly converted to double. Apart from that, good post :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement