(probably trivial) question about cmath library

Discussion in 'C++' started by Fab, Apr 17, 2012.

  1. Fab

    Fab Guest

    Dear all,

    I am a little confused about the std namespace used for all c++ libs.
    I am using g++ for compilation and I think my issue could also be
    related to g++ rather than the c++ libs. Here's my issue:

    When I use #include <iostream> in my code, I have to use the scope
    operator or global the namespace std to access e.g. cout. However, when
    I use #include <cmath> and want to use e.g. cos from that lib, the above
    mentioned does somehow not apply. Why?

    Thank you,
    Fab
     
    Fab, Apr 17, 2012
    #1
    1. Advertising

  2. Fab

    SG Guest

    On Apr 17, 8:06 am, Fab wrote:
    >
    > When I use #include <iostream> in my code, I have to use the scope
    > operator or global the namespace std to access e.g. cout.  However, when
    > I use #include <cmath> and want to use e.g. cos from that lib, the above
    > mentioned does somehow not apply.  Why?


    std::cos is guaranteed to work, unqualified cos is not guaranteed to
    work. If it works for you, chances are that cmath is implemented in
    terms of the C header math.h like this:

    #ifndef STD_CMATH_INCLUDED
    #define STD_CMATH_INCLUDED

    #include <math.h>

    namespace std {
    ...
    using ::cos;
    ...
    }

    #endif

    But a C++ compiler vendor might implement this differently in a way
    that only introduces math functions in std namespace and NOT the
    global scope (at least up to but excluding C++2011, I don't know if
    this changed in C++2011).

    > Thank you,
    > Fab


    Cheers!
    SG
     
    SG, Apr 17, 2012
    #2
    1. Advertising

  3. Fab <> wrote:
    > When I use #include <iostream> in my code, I have to use the scope
    > operator or global the namespace std to access e.g. cout. However, when
    > I use #include <cmath> and want to use e.g. cos from that lib, the above
    > mentioned does somehow not apply. Why?


    What's happening with gcc in particular (and possibly with other
    compilers) is that, AFAIK, <cmath> internally also includes <math.h>
    (which is the C equivalent), which in turn has the math functions in
    the global namespace.

    Note that if you use the unqualified math function names, you will
    be calling the 'double' versions of <math.h> instead of the type-specific
    versions in <cmath> (which are inside the std namespace). This can make
    a difference if you are eg. using long doubles.

    (Also, using the unqualified names is not guaranteed to be portable.)
     
    Juha Nieminen, Apr 17, 2012
    #3
  4. Fab

    Marc Guest

    SG wrote:

    > On Apr 17, 8:06 am, Fab wrote:
    >>
    >> When I use #include <iostream> in my code, I have to use the scope
    >> operator or global the namespace std to access e.g. cout.  However, when
    >> I use #include <cmath> and want to use e.g. cos from that lib, the above
    >> mentioned does somehow not apply.  Why?

    >
    > std::cos is guaranteed to work, unqualified cos is not guaranteed to
    > work. If it works for you, chances are that cmath is implemented in
    > terms of the C header math.h like this:

    [...]

    which most libraries have been illegally doing since the beginning.

    > But a C++ compiler vendor might implement this differently in a way
    > that only introduces math functions in std namespace and NOT the
    > global scope


    as required by C++03.

    > (at least up to but excluding C++2011, I don't know if
    > this changed in C++2011).


    C++11 makes the almost universal implementation you described above
    legal (except for a few details) but doesn't forbid the old one.

    The description (basically what you said):
    * math.h provides ::cos
    * cmath provides std::cos
    * either may provide the other, but you can't rely on it
    remains the safest.
     
    Marc, Apr 17, 2012
    #4
  5. Fab

    Marc Guest

    Juha Nieminen wrote:

    > Fab <> wrote:
    >> When I use #include <iostream> in my code, I have to use the scope
    >> operator or global the namespace std to access e.g. cout. However, when
    >> I use #include <cmath> and want to use e.g. cos from that lib, the above
    >> mentioned does somehow not apply. Why?

    >
    > What's happening with gcc in particular (and possibly with other
    > compilers) is that, AFAIK, <cmath> internally also includes <math.h>
    > (which is the C equivalent), which in turn has the math functions in
    > the global namespace.
    >
    > Note that if you use the unqualified math function names, you will
    > be calling the 'double' versions of <math.h> instead of the type-specific
    > versions in <cmath> (which are inside the std namespace). This can make
    > a difference if you are eg. using long doubles.


    Note that this is a gcc bug (a common one, so you might as well
    consider it standard to be on the safe side...). math.h is also
    supposed to provide all the overloads in C++ (not sure if that
    requirement will still be there in C++1y).
     
    Marc, Apr 17, 2012
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edwin Young

    minor bug in cmath.asin?

    Edwin Young, Apr 3, 2004, in forum: Python
    Replies:
    4
    Views:
    397
    Edwin Young
    Apr 4, 2004
  2. Mark Dickinson

    cmath, __float__ and __complex__

    Mark Dickinson, Feb 15, 2007, in forum: Python
    Replies:
    1
    Views:
    319
    Ziga Seilnacht
    Feb 16, 2007
  3. Replies:
    10
    Views:
    672
    Victor Bazarov
    Oct 22, 2005
  4. baibaichen

    trivial or non-trivial object

    baibaichen, Jan 12, 2006, in forum: C++
    Replies:
    3
    Views:
    955
    osmium
    Jan 12, 2006
  5. Replies:
    2
    Views:
    356
Loading...

Share This Page