Thread: how to use math functions in g++

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    2

    how to use math functions in g++

    #include <math>
    gives error on linux compiler g++
    can anybody guide how functions like sqrt() are to be used

  2. #2
    Registered User
    Join Date
    Mar 2008
    Posts
    2
    try #include <cmath>

    c libraries start with an extra c in c++(ie cmath, cstdio, ctime)

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The correct header from C is <math.h>. This is deprecated in C++ so <cmath> should be used instead (in which case math functions like sqrt() will live in namespace std).

    The other usual requirement (assuming you have #include'd required headers) is to add -lm to the command line when linking, so as to link functions from the math library into the executable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Is it legal to have functions within functions?
    By Programmer_P in forum C++ Programming
    Replies: 13
    Last Post: 05-25-2009, 11:21 PM
  2. how to use operator+() in this code?
    By barlas in forum C++ Programming
    Replies: 10
    Last Post: 07-09-2005, 07:22 PM
  3. Math Header?
    By Rune Hunter in forum C++ Programming
    Replies: 26
    Last Post: 09-17-2004, 06:39 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. toughest math course
    By axon in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-28-2003, 10:06 PM