Thread: What's the difference between cmath and math.h?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    What's the difference between cmath and math.h?

    Are they the same

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Yes and no?

    I don't know exactly how it goes, but it's something like this: When the C99 standard came out, all the old C libraries were changed from the .h suffix to the 'c' prefix. math.h became cmath, stdio.h became cstdio, string.h became cstring... etc. They were also defined under the std namespace, which is why you use that namespace in your programs.

    I'm sure there is a C history buff that'd be willing to break it down in better detail (or completely rebut what I just told you. )
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Sunny, Windy Florida
    Posts
    8
    I just searched the site and found an answer to that (amazing, the things you can find when you search the site). "math.h" is the header file with functions and such that has been used since C. "cmath.h" is the same, but for C++. I don't know if there are differences other than that, though (I didn't even know cmath existed before I read this).

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Not quite. <cmath> is a C++ headerr provides the same functionality as the C header named <math.h>, but everything in it lives in namespace std.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by KingDubya
    "cmath.h" is the same, but for C++.
    There is no such thing as cmath.h. Read my post.
    Sent from my iPadŽ

  6. #6
    Registered User
    Join Date
    Dec 2005
    Location
    Sunny, Windy Florida
    Posts
    8
    Quote Originally Posted by SlyMaelstrom
    There is no such thing as cmath.h. Read my post.
    Meh, I always include header files using a ".h" at the end. It still works.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In C++, both <math.h> and <cmath> are allowed (both are standard). However, you should prefer using the C++ version, <cmath>. The C version was deprecated by the C++ standard (C99 is different, you meant the one and only C++ standard from 1998). Deprecated just means it is allowed, but might be removed from the standard in the future, so you should prefer the alternative.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Meh, I always include header files using a ".h" at the end. It still works.

    It is wrong and probably only "works" in some cases if you are lucky. Use cmath or math.h, cmath.h makes no sense.

  9. #9
    Registered User
    Join Date
    Dec 2005
    Location
    Sunny, Windy Florida
    Posts
    8
    Quote Originally Posted by Daved
    >> Meh, I always include header files using a ".h" at the end. It still works.

    It is wrong and probably only "works" in some cases if you are lucky. Use cmath or math.h, cmath.h makes no sense.
    Well, since I've never used cmath before (refer to my first post in this thread), I've never experienced that. I don't think I've really ever used any that have the "c" prefix in any of my programs really. If that seems a little amateur-ish, I must let you know that I've only taken one year of C++ at my high school so far, and even then we had a teacher who was farther behind in programming than me by the end of the year; that and we had a really old textbook that spoke about libraries our compiler didn't have (for one, we didn't have "string.h").
    Last edited by KingDubya; 12-02-2005 at 03:44 PM.

  10. #10
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    The reason your probably using math.h and iostream.h is because highschools are cheap and you're probably still using Turbo C++ which doesn't have the modern libraries.

    Just soak in the knowledge as we explain it, though.
    Sent from my iPadŽ

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    In Visual Studio .NET 2003, <cmath> includes math.h, then adds a lot of using statements like this one:

    using ::abs; using ::acos; using ::asin;

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> If that seems a little amateur-ish

    I was referring to cmath.h which was probably just a typo where you meant math.h. Using math.h is not amateurish, it is just becoming out of date for C++ programmers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. math.h
    By tsutharssan in forum C Programming
    Replies: 3
    Last Post: 07-03-2009, 02:24 PM
  2. undefined reference to `sqrt' when using math.h
    By Milhas in forum C Programming
    Replies: 4
    Last Post: 03-31-2008, 06:11 PM
  3. Implementation of the math.h libary
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 07-25-2006, 09:21 PM
  4. Replies: 5
    Last Post: 06-01-2006, 04:37 PM
  5. gcc and math.h
    By viaxd in forum C Programming
    Replies: 3
    Last Post: 10-08-2003, 06:18 AM