发新话题
打印

25 D语言 浮点 Floating Point

25 D语言 浮点 Floating Point

知识若不分享 实在没有意义 http://www.d-programming-language-china.org 20070425

点击下面网址查看原文:
http://www.d-programming-language-china.org

by: uFramer D语言论坛 http://www.d-programming-language-china.org
from: http://www.digitalmars.com/d/float.html
version: 基于D 1.013 (Apr 19, 2007)

Floating Point Intermediate Values浮点运算中间值

在许多计算机上,使用较高精度的运算并不比使用较低精度的运算耗费的时间长,所以为内部的临时变量采用机器允许的最高精度对于数值运算是有意义的。这里采用的哲学是不强求语言为统一而采用各种硬件的最低精度,从而充分利用目标硬件的最佳性能。
对于浮点运算表达式的中间值来说,可能会使用高于表达式类型所要求的精度。操作数的类型只决定最低精度而不是最高精度。实现注记:例如,在 Intel x86 机器上,计算的中间步骤最好(但不是必需)使用硬件提供的全部 80 位精度。

如果临时变量和公共子表达式的使用量很大的话,优化后的代码很可能会得到比未优化代码更精确的解。

算法应该以计算的最小精度为基础。如果实际的精度更高,它们也不应该退化或者失败。同实数(扩展)类型不同,float 或者 double 类型应该用于:

减少大型数组的内存消耗
速度比正确性重要
维持同 C 的数据和函数参数的兼容性

Floating Point Constant Folding

Regardless of the type of the operands, floating point constant folding is done in real or greater precision. It is always done following IEEE 754 rules and round-to-nearest is used.

Floating point constants are internally represented in the implementation in at least real precision, regardless of the constant's type. The extra precision is available for constant folding. Committing to the precision of the result is done as late as possible in the compilation process. For example:( 本文出处: http://www.d-programming-language-china.org )

const float f = 0.2f;
writefln(f - 0.2);

will print 0. A non-const static variable's value cannot be propagated at compile time, so:

static float f = 0.2f;
writefln(f - 0.2);

will print 2.98023e-09. Hex floating point constants can also be used when specific floating point bit patterns are needed that are unaffected by rounding. To find the hex value of 0.2f:

import std.stdio;

void main()
{
    writefln("%a", 0.2f);
}

which is 0x1.99999ap-3. Using the hex constant:

const float f = 0x1.99999ap-3f;
writefln(f - 0.2);

prints 2.98023e-09.

Different compiler settings, optimization settings, and inlining settings can affect opportunities for constant folding, therefore the results of floating point calculations may differ depending on those settings.( 本文出处: http://www.d-programming-language-china.org )

Complex and Imaginary types复数和虚数类型

D语言论坛 D语言论坛 http://www.d-programming-language-china.org 按:
原译作“负数和虚数类型”,明显是笔误。
在现有的语言中,为了将复数类型添加到现存类型体系中可谓费尽周折:模板、结构、运算符重载等等,并且最终的结果几乎都是失败。失败的原因可能是由于复数运算的语义很微妙,是由于编译器不理解程序员想要做什么,因而无法对语义的实现进行优化。( 本文出处: http://www.d-programming-language-china.org )

所有这些的目的都只是为了避免加入一个新类型。添加一个新类型意味着编译器可以使所有的复数语义工作“正常”。然后程序员就可以依靠复数的正确(至少是稳定)的实现。

伴随而来的是对虚数的需求。虚数类型使我们可以避开一些微妙的语义问题,并且由于不用处理隐含的 0 实部,可以提高运算的性能。

虚数文字量有一个 i 作为后缀:

ireal j = 1.3i;

复数文字量没有自身特殊的语法,只需写成实数类型和虚数类型相加即可:

cdouble cd = 3.6 + 4i;
creal c = 4.5 + 2i;

复数,实数,虚数有两个属性:( 本文出处: http://www.d-programming-language-china.org )

QUOTE:
.re    实数部分(虚数0)
.im    虚数部分    (实数0)

例如:

QUOTE:
cd.re        是 4.5 double
cd.im        是 2 double
c.re        是 4.5 real
c.im        是 2 real
j.im        是 1.3 real
j.re        是 0 real

Rounding Control取整控制

IEEE 754 浮点数算术包括了设置四种不同的取整模式的能力。D 加入了支持它们的语法:std.c.fenv

Exception Flags异常标志

IEEE 754 浮点数算术可以为计算中发生的事件设立标志:

FE_INVALID
FE_DENORMAL
FE_DIVBYZERO
FE_OVERFLOW
FE_UNDERFLOW
FE_INEXACT

这些标志可以用std.c.fenv设置/重置。( 本文出处: http://www.d-programming-language-china.org )

Floating Point Comparisons浮点比较

除了常用的 <、<=、>、>=、== 和 != 比较运算符外,D 另外提供了专用于浮点数的运算符。它们是

QUOTE:
!<>= <> <>= !<= !< !>= !> !<>

并符合 C 扩展 NCEG 的语义。
参见浮点比较:
http://www.digitalmars.com/d/expression.html#floating_point_comparisons

( lastupdate:20070426 最新文章请访问http://www.d-programming-language-china.org )

关于一大步成功社区:
yidabu提倡在交流中学习,在分享中提高
收集感兴趣的知识,写下心得,通过网络与别人一起分享
理解一点就实践一步,收获什么就分享什么,成功就是这样一点点一步步累积起来的
网络只是一个工具,只有自己身心提高才是实实在在的。d-programming-language-china.org为大家提供一个学习交流各种知识的平台

TOP

发新话题