加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 对数字文字使用f后缀

发布时间:2020-12-16 01:40:34 所属栏目:百科 来源:网络整理
导读:我看到一些像这样的代码: float num2 = ( ( this.X * this.X ) + ( this.Y * this.Y ) ) + ( this.Z * this.Z );float num = 1f / ( ( float ) Math.Sqrt ( ( double ) num2 ) );this.X *= num;this.Y *= num;this.Z *= num; 它是否像这样重要?: float nu
我看到一些像这样的代码:

float num2 = ( ( this.X * this.X ) + ( this.Y * this.Y ) ) + ( this.Z * this.Z );
float num = 1f / ( ( float ) Math.Sqrt ( ( double ) num2 ) );
this.X *= num;
this.Y *= num;
this.Z *= num;

它是否像这样重要?:

float num2 = ( ( this.X * this.X ) + ( this.Y * this.Y ) ) + ( this.Z * this.Z );
float num = 1 / ( ( float ) Math.Sqrt ( ( double ) num2 ) );
this.X *= num;
this.Y *= num;
this.Z *= num;

编译器是否会使用(float)/(float)或尝试使用(double)/(float)作为第2行的第2个示例?

编辑:顺便说一下会有任何性能差异?

解决方法

它实际上使用(int)/(float)作为第二个例子.由于Int32可以隐式转换为Single,编译器不会抱怨,它会正常工作.

话虽如此,如果你这样做,它会抱怨:

float num = 1.0 / ( ( float ) Math.Sqrt ( ( double ) num2 ) );

这会导致它尝试使用(double)/(float),这将有效地变成(double)/(double).然后编译器会在尝试将double隐式设置为float变量时进行抱怨.

EDIT: Btw would there be any performance difference?

可能不是一个可衡量的.话虽这么说,你将在IL中创建额外的转换操作.这些可能在JIT期间被淘汰 – 但同样,它将是微观的.

就个人而言,我可能会使用双精度数学处理它,因为它会使代码更容易阅读:

double num2 = (this.X * this.X) + (this.Y * this.Y) + (this.Z * this.Z);
float num = (float) (1.0 / Math.Sqrt(num2));
this.X *= num;
// ...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读