View Full Version : GLM math constants

Scotopik

05-22-2011, 08:55 PM

Hi,

I'm new to GLM (what an awesome library btw), and can't find any math constants, which seems odd given its functional coverage.

Specifically I'm looking for just good old M_PI. I see hardwired constants all over the place, which again seems odd to me. For instance in glm::core::function::trigonometric the 'degrees' function (and all other associated trigonometric functions) defines pi in place (3.141...), like this:

// radians

template <typename genType>

GLM_FUNC_QUALIFIER genType radians

(

genType const & degrees

)

{

GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'radians' only accept floating-point input");

const genType pi = genType(3.1415926535897932384626433832795);

return degrees * (pi / genType(180));

}

Can anyone lead me in the right direction?

Cheers,

Shane

Groovounet

05-23-2011, 05:41 AM

That's a good point, there is no constant provided by GLM.

Which constant would be useful for you?

ZbuffeR

05-23-2011, 05:56 AM

Wouldn't the ones in math.h be useful ?

Alfonse Reinheart

05-23-2011, 06:51 AM

Useful but redundant since they're already in math.h.

Now, M_PI is not part of math.h anymore thanks to C99. So it's reasonable to define that. Though if they're using C++, then C++ standard library still includes it (I think) as part of its math.h. So you may want to check to see if it is defined first.

Scotopik

05-23-2011, 07:29 PM

Well usually M_PI (and crew) are defined in math.h as:

#define M_E 2.71828182845904523536028747135266250 /* e */

#define M_LOG2E 1.44269504088896340735992468100189214 /* log 2e */

#define M_LOG10E 0.434294481903251827651128918916605082 /* log 10e */

#define M_LN2 0.693147180559945309417232121458176568 /* log e2 */

#define M_LN10 2.30258509299404568401799145468436421 /* log e10 */

#define M_PI 3.14159265358979323846264338327950288 /* pi */

#define M_PI_2 1.57079632679489661923132169163975144 /* pi/2 */

#define M_PI_4 0.785398163397448309615660845819875721 /* pi/4 */

#define M_1_PI 0.318309886183790671537767526745028724 /* 1/pi */

#define M_2_PI 0.636619772367581343075535053490057448 /* 2/pi */

#define M_2_SQRTPI 1.12837916709551257389615890312154517 /* 2/sqrt(pi) */

#define M_SQRT2 1.41421356237309504880168872420969808 /* sqrt(2) */

#define M_SQRT1_2 0.707106781186547524400844362104849039 /* 1/sqrt(2) */

However, so are many of the standard trigonometric functions:

extern double acos( double );

extern double asin( double );

extern double atan( double );

extern double atan2( double, double );

extern double cos( double );

extern double sin( double );

extern double tan( double );

extern double acosh( double );

extern double asinh( double );

extern double atanh( double );

extern double cosh( double );

extern double sinh( double );

extern double tanh( double );

However, these trigonometric functions are redefined in GLM in func_trigonometric.inl, despite the fact they call the std functions. For instance, 'sin':

// sin

template <typename genType>

GLM_FUNC_QUALIFIER genType sin

(

genType const & angle

)

{

GLM_STATIC_ASSERT(detail::type<genType>::is_float, "'sin' only accept floating-point input");

return ::std::sin(angle);

}

I guess the idea is that GLM is supposed to be a one-stop-shop for math, which is great and absolutely needed, so why not go the whole way and move over the standard constants too.

I would expect to see something like, glm::M_PI or glm::constants::pi .

Basically, I would love to more or less replace <math> and <math.h> with <gml.h> and be done with it.

Cheers,

Shane

Alfonse Reinheart

05-23-2011, 08:00 PM

I guess the idea is that GLM is supposed to be a one-stop-shop for math

I'm fairly sure the idea is to do what GLSL does. GLSL has a function called "sin" which can take a scalar float (as well as vectors). Therefore, GLM must have a function called "sin" which takes a scalar float.

Groovounet

05-24-2011, 04:49 AM

I have added a ticket for this:

https://sourceforge.net/apps/trac/ogl-math/ticket/94

I am not sure how this could be exposed but it could be interesting.

Scotopik

05-24-2011, 06:47 AM

Excellent.

Thanks and fingers crossed we find some constants in GLM soonish.

Cheers,

Shane

Scotopik

05-24-2011, 06:50 AM

True, however given PI is a fundamental mathematical constant, and used in graphics programming all the time (at least by me, but I use radians in my math), it struck me as odd that it was absent.

liquid11111

08-04-2011, 04:23 AM

What now, can I found some constants in GLM?

ticket link does not working

Groovounet

08-04-2011, 05:56 AM

The link is working but you may need a SF.net account

No progress on that side so far.

ScottManDeath

08-05-2011, 11:44 PM

http://www.boost.org/doc/libs/1_47_0/lib.../constants.html (http://www.boost.org/doc/libs/1_47_0/libs/math/doc/sf_and_dist/html/math_toolkit/toolkit/internals1/constants.html)

Has the advantage of making them templatable for float, double etc.

Groovounet

08-10-2011, 04:14 AM

I'll keep this is mind, thanks!

Groovounet

11-18-2012, 06:26 AM

There are many constants available in GLM now: http://glm.g-truc.net/api-0.9.4/a00145.html

Powered by vBulletin® Version 4.2.3 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.