cmath and math.h in Linux

Started by
4 comments, last by phresnel 14 years, 10 months ago
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
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.
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.
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.
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.

Stephen M. Webb
Professional Free Software Developer

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 :)

This topic is closed to new replies.

Advertisement