12.34f vs(float)12.34 [英] 12.34f vs (float) 12.34

查看:100
本文介绍了12.34f vs(float)12.34的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

常数如12.34f之间是否存在差异?和浮动

12.34" ;?


原则上,至少,后者是一个双重常数被投射到

a float;虽然这两个都生成实际常量,后者

是否实际在编译时进行转换?也就是说,是否有常数

其中


< constant> f!=(float)< constant>





-


程序员,作者 http://www.midnightbeach.com

和父亲 http://www.midnightbeach.com/hs

Is there a difference between a constant like "12.34f" and "(float)
12.34"?

In principle, at least, the latter is a double constant being cast to
a float; while the two both generate actual constants, does the latter
ACTUALLY do a conversion at compile time? That is, are there constants
where

<constant>f != (float) <constant>

?

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs

推荐答案

没有。假设可以,编译器将优化抛出。它可能是

生成这样的东西:


private static void Blah()

..locals(float V_0 ,浮动V_1)

L_0000:ldc.r4 1.23

L_0001:stloc.0

L_0002:ldc.r4 4.56

L_0003:stloc.1


来自:

static void Blah()

{

浮点数f1 = 1.23F;

浮点数f2 =(浮点数)4.56;

}

-

____________________

Klaus H. Probst,MVP
http://www.vbbox.com/

" Jon Shemitz" < jo*@midnightbeach.com>在消息中写道

新闻:40 *************** @ midnightbeach.com ...
No. The compiler will optimize the cast away assuming it can. It''s probably
generating something like this:

private static void Blah()
..locals (float V_0, float V_1)
L_0000: ldc.r4 1.23
L_0001: stloc.0
L_0002: ldc.r4 4.56
L_0003: stloc.1

from this:

static void Blah()
{
float f1 = 1.23F;
float f2 = (float) 4.56;
}
--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/
"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
是否存在差异常数如12.34f和浮动
12.34"

原则上,至少,后者是一个双重常数被投射到一个浮子;虽然这两个都生成实际常量,后者是否实际上在编译时进行转换?也就是说,是否有常数


< constant> f!=(float)< constant>



-

程序员,作者 http://www.midnightbeach.com
和父亲 http://www.midnightbeach.com/hs
Is there a difference between a constant like "12.34f" and "(float)
12.34"?

In principle, at least, the latter is a double constant being cast to
a float; while the two both generate actual constants, does the latter
ACTUALLY do a conversion at compile time? That is, are there constants
where

<constant>f != (float) <constant>

?

--

programmer, author http://www.midnightbeach.com
and father http://www.midnightbeach.com/hs



" Klaus H. Probst"写道:
"Klaus H. Probst" wrote:
否。编译器将假设它可以优化抛出。它可能会生成这样的东西:

private static void Blah()
.locals(float V_0,float V_1)
L_0000:ldc.r4 1.23
L_0001:stloc.0
L_0002:ldc.r4 4.56
L_0003:stloc.1

由此:

静态无效Blah()
{
浮点数f1 = 1.23F;
浮点数f2 =(浮点数)4.56;
}
No. The compiler will optimize the cast away assuming it can. It''s probably
generating something like this:

private static void Blah()
.locals (float V_0, float V_1)
L_0000: ldc.r4 1.23
L_0001: stloc.0
L_0002: ldc.r4 4.56
L_0003: stloc.1

from this:

static void Blah()
{
float f1 = 1.23F;
float f2 = (float) 4.56;
}




确实如此。但问题是,它是否正在优化演员

,无论代码是否像1.23f一样。和(浮动)1.23可能会生成

略有不同的常数。


-


程序员,作者 http://www.midnightbeach.com

和父亲 http://www.midnightbeach.com/hs


这两个问题的答案是:


如何:它被称为常量折叠,由C#编译器执行,并且

那里'那里有大量的信息,并且

无论如何:理论上我认为他们可以在你的例子中(但那只是我的

解释); CLI文档的第11.1.3节说明:


浮点数的存储位置(静态,数组元素和

类的字段)是固定大小。支持的存储大小为
float32和float64。其他地方(在评估堆栈上,作为参数,

作为返回类型,以及作为局部变量)浮点数是使用内部浮点类型表示的
。在每个这样的实例中,

变量或表达式的名义类型是R4或R8,但其

值可以在内部用额外的范围和/或精度表示。

内部浮点表示的大小是

依赖于实现,可能会有所不同,并且至少应具有精确度,因为

很好表示变量或表达式的那个。


根据我的经验,内部表示在调试和

发布之间可能有所不同。


希望有所帮助,但请不要把我的答案当成福音。


Stu


" Jon Shemitz" < jo*@midnightbeach.com>在消息中写道

news:40 *************** @ midnightbeach.com ...
The answers to those two questions are:

HOW: it''s called constant folding, and is performed by the C# compiler, and
there''s loads of information out there, and
WHETHER: in theory I think they could in your example (but that''s just my
interpretation); section 11.1.3 of the CLI docs states:

"Storage locations for floating point numbers (statics, array elements, and
fields of classes) are of fixed size. The supported storage sizes are
float32 and float64. Everywhere else (on the evaluation stack, as arguments,
as return types, and as local variables) floating point numbers are
represented using an internal floating-point type. In each such instance,
the nominal type of the variable or expression is either R4 or R8, but its
value may be represented internally with additional range and/or precision.
The size of the internal floating-point representation is
implementation-dependent, may vary, and shall have precision at least as
great as that of the variable or expression being represented."

From my experience the internal representation can differ between debug and
release.

Hope that helps, but please don''t take my answer as gospel.

Stu

"Jon Shemitz" <jo*@midnightbeach.com> wrote in message
news:40***************@midnightbeach.com...
" Klaus H. Probst" ;写道:
"Klaus H. Probst" wrote:
否。编译器将假设它可以优化抛出。可能会产生这样的


private static void Blah()
.locals(float V_0,float V_1)
L_0000:ldc.r4 1.23
L_0001:stloc.0
L_0002:ldc.r4 4.56
L_0003:stloc.1

由此:

静态无效Blah()
{
浮点数f1 = 1.23F;
浮点数f2 =(浮点数)4.56;
}
No. The compiler will optimize the cast away assuming it can. It''s probably generating something like this:

private static void Blah()
.locals (float V_0, float V_1)
L_0000: ldc.r4 1.23
L_0001: stloc.0
L_0002: ldc.r4 4.56
L_0003: stloc.1

from this:

static void Blah()
{
float f1 = 1.23F;
float f2 = (float) 4.56;
}



确实是。但问题是,它是否正在优化演员表格,无论是否像1.23f这样的代码。和(浮动)1.23可能会产生稍微不同的常数。

-

程序员,作者 http://www.midnightbeach.com
和父亲 http://www.midnightbeach.com/hs



这篇关于12.34f vs(float)12.34的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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