初始化常量 [英] Initializing constants

查看:108
本文介绍了初始化常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码块将r / w变量(通常为.bss)初始化为pi的

值。其中一个问题是任何链接的编译单元可能会改变全局变量
。调整


// rodata

const long double const_pi = 0.0;


行到
< br $>
// rodata

const long double const_pi = init_ldbl_pi();


会增加额外的保护,但不是合法的C,并且,正确如此,

在GCC / i386上失败。


是否有任何C标准定义了将常数初始化为值的方法

从硬件获得,或者常量总数和/或
交叉编译是否完全禁止它(但是,当交叉编译时,编译器可以创建一个b $ b使用可用资源的价值,即

仿真)?


我希望这样的事情是可能的......


// rodata

const long double const_pi = LDBL_PI;


在i386系统上编译i386目标并使用

协处理器,LDBL_PI评估fldpi指令的80位值,

和编译时用于i386目标的非i386系统,仿真

值是根据可用资源计算的。


BEGIN CODE

#include< stdio.h>


//原型

long double init_ldbl_pi();


// rodata

const long double const_pi = 0.0;


//全局函数

long double init_ldbl_pi(){

long double ldbl = 3.14;


#if(已定义__GNUC__)&& (定义__i386__)

asm(" fldpi" :::" st");

asm(" fstpt%0":" = m"( ldbl)::" st");

#endif

返回ldbl;

}


//主函数

int main(){

long double ldbl_pi = init_ldbl_pi();

printf("%Le \\ \\ n",ldbl_pi);

返回0;

}

结束代码

The code block below initialized a r/w variable (usually .bss) to the
value of pi. One, of many, problem is any linked compilation unit may
change the global variable. Adjusting

// rodata
const long double const_pi=0.0;

lines to

// rodata
const long double const_pi=init_ldbl_pi();

would add additional protections, but is not legal C, and, rightly so,
fails on GCC/i386.

Do any C standards define a means to initalize constants to values
obtained from hardware, or does the total number of constants and/or
cross-compiling prohibit it completely (although, when cross-compiling,
the compiler could create a value using the resources available i.e.
emulation)?

I''m hoping something like this is possible...

// rodata
const long double const_pi= LDBL_PI;

When compiling on i386 systems for i386 targets and using the
coprocessor, LDBL_PI evaluates to the 80bit value of fldpi instruction,
and when compiling on non i386 systems for i386 targets, an emulation
value is calculated from available resources.

BEGIN CODE
#include <stdio.h>

// prototype
long double init_ldbl_pi();

// rodata
const long double const_pi=0.0;

// global function
long double init_ldbl_pi() {
long double ldbl=3.14;

#if (defined __GNUC__) && (defined __i386__)
asm("fldpi":::"st");
asm("fstpt %0":"=m" (ldbl)::"st");
#endif
return ldbl;
}

// main function
int main() {
long double ldbl_pi=init_ldbl_pi();
printf("%Le\n", ldbl_pi);
return 0;
}
END CODE

推荐答案

" newsposter0123" < ne ************ @ yahoo.comwrites:
"newsposter0123" <ne************@yahoo.comwrites:

我希望这样的事情可能......


// rodata

const long double const_pi = LDBL_PI;


在i386系统上编译i386目标和使用

协处理器,LDBL_PI评估为fldpi指令的80位值,

以及在非i386系统上编译i386目标时,仿真

值是根据可用资源计算的。
I''m hoping something like this is possible...

// rodata
const long double const_pi= LDBL_PI;

When compiling on i386 systems for i386 targets and using the
coprocessor, LDBL_PI evaluates to the 80bit value of fldpi instruction,
and when compiling on non i386 systems for i386 targets, an emulation
value is calculated from available resources.



从i386指令中获取pi有什么价值?我会

建议只写出足够数量的pi来覆盖所需的

水平的重要性。

-

适合主人的东西不适合新手。

你必须在超越结构之前理解道。

- 编程之道

What''s the value in getting pi from an i386 instruction? I would
suggest just writing out enough digits of pi to cover the desired
level of significance.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming




Ben Pfaff写道:

Ben Pfaff wrote:

在针对i386目标的i386系统上编译并使用

协处理器时,LDBL_PI评估为fldpi指令的80位值,

以及在非i386系统上编译i386目标时,仿真

值是根据可用资源计算的。
When compiling on i386 systems for i386 targets and using the
coprocessor, LDBL_PI evaluates to the 80bit value of fldpi instruction,
and when compiling on non i386 systems for i386 targets, an emulation
value is calculated from available resources.



从i386指令中获取pi有什么价值?我会

建议只写出足够数量的pi来覆盖所需的

水平的重要性。


What''s the value in getting pi from an i386 instruction? I would
suggest just writing out enough digits of pi to cover the desired
level of significance.



硬件通常提供除pi之外的常量。


换一个,转换数字时。理想情况下,您希望获得

原始号码。特别是调整碱时。但那场辩论

在其他地方肆虐。


如果提前知道测量设备的精度,那么
然后是可以预先确定有效数字。如果使用了具有更高精度的

设备,那么常量将是

需要调整(并且不再是常数)。


一般来说,常量意味着不受限制的使用,不仅限于特定的应用程序,或限制

计算中的有效位数。否则,为什么要在精密设备上花费

Hardware generally provides constants other than pi.

For one, when converting numbers. Ideally you would like to get the
original number back. Especially when adjusting bases. But that debate
rages elsewhere.

Provided the precision of the measurements device was known in advance,
then the number of significant digits could be predetermined. If a
device with greater precision were used, then the constant would be
require adjustment (and no longer be a constant).

In general, constants imply unrestricted usage, not limited to a
specific application, or limiting the number of significant digits in a
calculation. Otherwise, why spend




on precision equipment?


这篇关于初始化常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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