常数的定义,如pi和e [英] definitions of constants such as pi and e

查看:139
本文介绍了常数的定义,如pi和e的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该语言是否包含包含数字定义的头文件

如pi和e?


我相信一些实现过去有这些,但我在Stroustrup找不到他们




谢谢。

解决方案

< blockquote> Matt写道:

该语言是否包含包含数字定义的头文件
如pi和e?

我相信一些实现曾经有过这些,但我在Stroustrup找不到它们。

谢谢。




如果你找不到,只需谷歌的价值和副本。




马特写道:

语言是否有头文件包含数字的定义
如pi和e?

我相信一些实现过去有这些,但我在Stroustrup中找不到它们。<谢谢。




FWIW ...


有一段历史可以尝试在优雅中解决常数问题
$ b

gmane.comp.lib.boost.devel新闻组提出并拒绝各种解决方案的$ b方式。


解决方案分为4个区域IIRC :


解决方案1:使用变量例如


//一些数学标题


extern const双pi;


请注意,由于ODR规则,不应在标题中定义它所以

它需要链接应用程序中的库或定义一个

源文件,感觉很不方便。另一个缺点是

,它不能同时浮动,双倍和长双,所以它将永远不适合所有人。


解决方案2:使用一个函数,例如


inline double pi()

{

return 3.141592653589793238462643383279502884197;

}


这使你无法控制浮点类型而且它似乎人们讨厌函数符号来调用它。当然,你可以将

设为模板,但是在调用时你必须提供模板

参数。


解决方案3:使用智能对象


struct pi {

模板< typename T>

T pi_function(); //得到pi作为T

模板< typename T>

运算符T(){return pi_function< T>();}

}

这可能是也可能不是。无论如何......这个问题已经在某些情况下推断出转换为IIRC的类型。


解决方案4:我的首选解决方案和我的解决方案使用,但遭受上述各种问题的影响


模板< typename T>

struct constant _ {


静态const T pi;

静态const T e;

};


/ /我最喜欢的漂浮的方便版本

#ifndef MY_FAVOURITE_FLOAT

#define MY_FAVOURITE_FLOAT double

#endif

struct constant :constant_< MY_FAVOURITE_FLOAT> {};


int main()

{

float val1 = constant_< float> :: pi;

double val = constant :: pi;

}


当然这需要一个库或源和可见的使用至少在第一次调用中使用
模板。

最后在某些版本的< cmath>

n中有一个M_PI宏可用或者通过它的嵌套include< math.h>,但这太可怕了...


所以如果有人有任何想法...... ???


问候

Andy Little


Matt写道:

语言是否有头文件包含数字的定义
如pi和e?

我相信一些实现过去有这些,但我在Stroustrup中找不到它们。




以下是D's std.math中使用的那些,好到80位:


const real E = 2.7182818284590452354L;

const real LOG2T = 0x1.a934f0979a3715fcp + 1;

const real LOG2E = 0x1.71547652b82fe178p + 0;

const real LOG2 = 0x1.34413509f79fef32p-2;

const real LOG10E = 0.43429448190325182765;

const real LN2 = 0x1.62e42fefa39ef358p-1;

const real LN10 = 2.30258509299404568402;

const r eal PI = 0x1.921fb54442d1846ap + 1;

const real PI_2 = 1.57079632679489661923;

const real PI_4 = 0.78539816339744830962;

const real M_1_PI = 0.31830988618379067154 ;

const real M_2_PI = 0.63661977236758134308;

const real M_2_SQRTPI = 1.12837916709551257390;

const real SQRT2 = 1.41421356237309504880;

const real SQRT1_2 = 0.70710678118654752440;


hex浮点符号与C99兼容。我更喜欢hex float for

常数,因为它确保最后一位的准确性而不用担心

十进制转换舍入错误。


-Walter Bright
www.digitalmars.com C,C ++,D编程语言编制者


Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can''t find them
in Stroustrup.

Thanks.

解决方案

Matt wrote:

Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can''t find them
in Stroustrup.

Thanks.



If you couldn''t find any, just google for the value and copy.

Ben


Matt wrote:

Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can''t find them
in Stroustrup.

Thanks.



FWIW...

There is a history to trying to solve constants problem in an elegant
way with various solutions having being proposed and rejected on
gmane.comp.lib.boost.devel newsgroup.

The solutions fell into 4 areas IIRC:

solution1: Use a variable e.g

//some math header

extern const double pi;

Note that one shouldnt define it in the header due to the ODR rule so
it would require linking a library in the application or defining in a
source file, which was felt to be inconvenient. The other drawback was
that it cant simultaneously be float, double and long double so it will
never be right for everybody.

solution 2: use a function e.g

inline double pi()
{
return 3.141592653589793238462643383279502884197;
}

This suffers in that you cant control the floating point type and it
seems that people hate the function notation to call it. You could make
it a template of course, but you would have to provide the template
parameter when invoked.

solution 3: Use a smart object

struct pi{
template <typename T>
T pi_function(); // get pi as a T
template <typename T>
operator T() { return pi_function<T>();}
}
This may or may not be how it went. Whatever... The problem this had
was deducing the type to convert to in some situations IIRC.

solution 4: My preferred solution and the one I use, but suffers from
various of the problems above

template <typename T>
struct constant_{

static const T pi;
static const T e;
};

// convenient version for My Favourite float
#ifndef MY_FAVOURITE_FLOAT
#define MY_FAVOURITE_FLOAT double
#endif
struct constant : constant_<MY_FAVOURITE_FLOAT>{};

int main()
{
float val1 = constant_<float>::pi;
double val = constant::pi;
}

Of course this requires a library or source and visible use of
templates in the first invocation at least.
Finally there is an M_PI macro available in some versions of <cmath>
normally via its nested include <math.h>, but that is horrible...

So If anyone has any ideas .... ???

regards
Andy Little


Matt wrote:

Does the language have header files containing definitions of numbers
such as pi and e?

I believe some implementations used to have those, but I can''t find them
in Stroustrup.



Here are the ones used in D''s std.math, good to 80 bits:

const real E = 2.7182818284590452354L;
const real LOG2T = 0x1.a934f0979a3715fcp+1;
const real LOG2E = 0x1.71547652b82fe178p+0;
const real LOG2 = 0x1.34413509f79fef32p-2;
const real LOG10E = 0.43429448190325182765;
const real LN2 = 0x1.62e42fefa39ef358p-1;
const real LN10 = 2.30258509299404568402;
const real PI = 0x1.921fb54442d1846ap+1;
const real PI_2 = 1.57079632679489661923;
const real PI_4 = 0.78539816339744830962;
const real M_1_PI = 0.31830988618379067154;
const real M_2_PI = 0.63661977236758134308;
const real M_2_SQRTPI = 1.12837916709551257390;
const real SQRT2 = 1.41421356237309504880;
const real SQRT1_2 = 0.70710678118654752440;

The hex float notation is C99 compatible. I prefer hex float for
constants, as it ensures accuracy to the last bit without worrying about
decimal conversion roundoff errors.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers


这篇关于常数的定义,如pi和e的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆