CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2011
    Posts
    7

    valarray and math.h

    Hi, I'm using valarray in my Visual C++ 2010 program. I'm also using two non-valarray math features. First is sqrt - not the one in valarray (its argument is a double not a valarray). I've included valarray header file in the program while I haven't included math.h. The compiler doesn't complain about sqrt and the program runs fine. Then I needed to use the constant M_PI which is defined in math.h. I thought since the compiler wasn't complaining about sqrt, it somehow included math.h, so I should be able to use M_PI. But I get an error for that. The compiler doesn't know M_PI. Is my assumption that math.h is included in my program correct? Is it possible that sqrt that I'm running comes from somewhere else (complex header file)? Do I need to define something else to enable using this constant even if math.h is included? May be the more important question is what's the relationship between valarray, math and complex? I would really appreciate your response. Thanks!

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: valarray and math.h

    I don't think that M_PI is standard C++. Try putting the following
    at the top of your code (but after #include "stdafx.h")

    Code:
    #define _USE_MATH_DEFINES

  3. #3
    Join Date
    Mar 2011
    Posts
    7

    Re: valarray and math.h

    Thanks Philip for your reply. I did what you said but it didn't work. In math.h there's a comment saying that _USE_MATH_DEFINES must be defined before math.h. I defined it before including any other header file in my main C++ file but after stdafx (also before stdafx just for testing) to make sure that it is defined before math.h, and I still didn't get the result. I think I still need to play a bit more with this. Thanks again!

  4. #4
    Join Date
    Jan 2001
    Posts
    253

    Re: valarray and math.h

    Check if you are including math.h inside of stdafx.h.

    If you are, you need to put the #define inside of stdafx.h, before including math.h. Otherwise any include of math.h after stdafx.h is ignored (as the file has already been included).

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: valarray and math.h

    You can do something like

    Code:
    #ifndef M_PI
    #define M_PI 3.14159
    #endif
    in your own header file you want.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured