[Lemon-devel] Math constants in C++

Kovács Péter kpeter at inf.elte.hu
Sun Feb 3 14:21:30 CET 2008


Hi,

 > I'm about to write a lemon include file providing math constants.

What solutions are used by other cross-platform libraries?

 >         Nowadays,
 >         compilers will optimize the expressons like '2*M_PI', won't
 >         they?

I don't know.

 >       * Is that OK to declare all these constants as 'const long
 >         double'?

Does it cause warnings, e.g. in 'double x = 2 * PI;'?

 >       * Do we need to provide all of the original math.h constants for
 >         'compatibility'?

I don't think so, I suggest only the ones that are used in LEMON or that 
are really important for another reason. (E.g. E is important even if it 
is not used in LEMON yet.)

 >         As all these constants are prefixed
 >         with M_, my first idea was to just remove this prefix.

I support this idea.

What about a separate namespace, e.g. lemon::math (or lemon::constants)?
I think lemon::math::PI, lemon::math::E would be very good.

What about prefix 'C_'?

 >                 We cannot call then 1_PI, or 2_PI.
 >                 But do we need them at all?

I don't think so.

Regards,
Peter


Alpár Jüttner írta:
> Hi,
> 
> I'm about to write a lemon include file providing math constants. This
> however raises more questions.
> 
>       * Is that OK to declare all these constants as 'const long
>         double'? In math.h (in gcc), there are two series of math
>         constants, e.g. M_LN2 is provides enough digits only for double
>         while M_LN2l is the long double declaration.
>       * Do we need to provide all of the original math.h constants for
>         'compatibility'? For example there are several constants which
>         are just some other constants multiplied by two. Nowadays,
>         compilers will optimize the expressons like '2*M_PI', won't
>         they?
>       * How to call these constants? In math.h they are #define's, thus
>         we cannot use the same name. As all these constants are prefixed
>         with M_, my first idea was to just remove this prefix. For PI or
>         LOG2 this seems quite reasonable. However,
>               * what about M_E? Isn't it too harsh have a constant E?
>                 Actually, it is in our namespace, so it will not cause
>                 name conflict.
>               * what about M_1_PI, M_2_PI etc.? (These define 1/pi and
>                 2/pi respectively) We cannot call then 1_PI, or 2_PI.
>                 But do we need them at all?
> 
> 
> Best regards,
> Alpar
> 
> 
> _______________________________________________
> Lemon-devel mailing list
> Lemon-devel at lemon.cs.elte.hu
> http://lemon.cs.elte.hu/mailman/listinfo/lemon-devel




More information about the Lemon-devel mailing list