[try Beta version]
Not logged in

 
How does #define _USE_MATH_DEFINES work?

May 18, 2013 at 3:48am
How does

#define _USE_MATH_DEFINES

work? How can you just magically define it like that and math constants will be available to you?

Is there something in cmath or math.h that checks if it is defined and based on that defines the math constants?

Also How do you look at the standard library files in Visual Studio?

Thanks!
Last edited on May 18, 2013 at 3:49am
May 18, 2013 at 3:54am
Ok I googled it and figured out how it works.

It checks if you defined USE_MATH_DEFINES and based on that defines the constants.

It also checks if it is already DEFINED, I dont think it has to do this because pragma once and the include guards will prevent it from being included twice in a translation unit.

But here is the code that does that if anyone wants to see for future reference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#if defined(_USE_MATH_DEFINES) && !defined(_MATH_DEFINES_DEFINED)
#define _MATH_DEFINES_DEFINED

/* Define _USE_MATH_DEFINES before including math.h to expose these macro
 * definitions for common math constants.  These are placed under an #ifdef
 * since these commonly-defined names are not part of the C/C++ standards.
 */

/* Definitions of useful mathematical constants
 * M_E        - e
 * M_LOG2E    - log2(e)
 * M_LOG10E   - log10(e)
 * M_LN2      - ln(2)
 * M_LN10     - ln(10)
 * M_PI       - pi
 * M_PI_2     - pi/2
 * M_PI_4     - pi/4
 * M_1_PI     - 1/pi
 * M_2_PI     - 2/pi
 * M_2_SQRTPI - 2/sqrt(pi)
 * M_SQRT2    - sqrt(2)
 * M_SQRT1_2  - 1/sqrt(2)
 */

#define M_E        2.71828182845904523536
#define M_LOG2E    1.44269504088896340736
#define M_LOG10E   0.434294481903251827651
#define M_LN2      0.693147180559945309417
#define M_LN10     2.30258509299404568402
#define M_PI       3.14159265358979323846
#define M_PI_2     1.57079632679489661923
#define M_PI_4     0.785398163397448309616
#define M_1_PI     0.318309886183790671538
#define M_2_PI     0.636619772367581343076
#define M_2_SQRTPI 1.12837916709551257390
#define M_SQRT2    1.41421356237309504880
#define M_SQRT1_2  0.707106781186547524401

#endif  /* _USE_MATH_DEFINES */
May 18, 2013 at 3:56am
Also is Math.h better or Cmath?
May 18, 2013 at 4:02am
Google tells me that Cmath is for C++ and newer so I will go with it, was using math.h before.
May 18, 2013 at 4:05am
cmath is for C++. math.h is better suited for C.

#pragma is nonstandard. It is more for individual use.

If you are referring to the code pasted when you stated that they need not to check if _USE_MATH_DEFINES, it is indeed necessary. Note that they don't check for the define to avoid duplication. They are checking for the define to determine whether the following macros should be added to the translation unit. In addition, they include a formal define they use for whatever later purposes.
May 18, 2013 at 4:14am
@Daleth

"They are checking for the define to determine whether the following macros should be added to the translation unit"

They only wouldn't be added if this statement executed

#define _MATH_DEFINES_DEFINED

Which would be preventing duplication, I guess?

Also what is a formal define?
Last edited on May 18, 2013 at 4:14am
May 18, 2013 at 4:29am
Oops, my bad. Yeah, you're right about that point.
May 18, 2013 at 4:31am
xD

Also what is a formal defination?
May 18, 2013 at 5:15am
It also checks if it is already DEFINED, I dont think it has to do this because pragma once and the include guards will prevent it from being included twice in a translation unit.

Who says that is the only header in which these constants may be defined?
May 18, 2013 at 5:31am
Oh thats true. Good point
Topic archived. No new replies allowed.