可以在C#中缩短数学参考吗?

栏目: ASP.NET · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/11107688/can-math-references-be-shortened-in-c

在VB.NET中,我可以通过导入System.Math并直接引用其方法来使我的数学代码更加清晰:

Imports System.Math
[...]
Return New Vector3(Sin(az) * Cos(el), Cos(az) * Cos(el), Sin(el))

但是我不认为C#可以使用类来隐式访问它们的方法,所以我必须做类似的事情:

using System;
[...]
return new Vector3(Math.Sin(az) * Math.Cos(el), Math.Cos(az) * Math.Cos(el), Math.Sin(el));

但那很难看;它需要自己的滚动条!有没有办法在C#中编写一些看起来像我的VB.NET代码的东西?我可以为Sin和Cos编写局部包装器方法,但这不会降低性能(因为函数调用的开销)?而且,这需要为我正在使用的每个类中使用的每个Math函数编写包装函数;这也不是那么令人满意.

您可以使用using指令为System.Math提供别名:

using M = System.Math;

return new Vector3(M.Sin(az) * M.Cos(el), M.Cos(az) * M.Cos(el), M.Sin(el));

这是我得到的最好的.

I could write local wrapper methods for Sin and Cos, but wouldn’t that reduce performance (because of the overhead of function calls)?

你可以,并且在那时你可以使用泛型和其他细节使它更方便,但你仍然会引用这样的库,除非所有的数学都发生在这个代码作为方法出现的同一个类中.

“从开销中获得性能”这一问题的答案是“额外的堆栈帧,这对于标准应用程序来说非常明显.”如果你处于紧张的循环中,那么是的,这将是一个问题.

翻译自:https://stackoverflow.com/questions/11107688/can-math-references-be-shortened-in-c


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Computational Geometry

Computational Geometry

Mark de Berg、Otfried Cheong、Marc van Kreveld、Mark Overmars / Springer / 2008-4-16 / USD 49.95

This well-accepted introduction to computational geometry is a textbook for high-level undergraduate and low-level graduate courses. The focus is on algorithms and hence the book is well suited for st......一起来看看 《Computational Geometry》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具