简体   繁体   English

C# - 是否有 32 位浮点数学库?

[英]C# - Is there a 32-bit float math library?

I'm planning on doing my next project in c# rather than c++ (using SlimDX).我打算用 c# 而不是 c++(使用 SlimDX)做我的下一个项目。

All of directX uses floats, however System.Math uses doubles.所有的 directX 都使用浮点数,但是 System.Math 使用双精度数。 This means constantly converting between floats and doubles.这意味着不断在浮动和双打之间转换。

So ideally I'd like to write all the code using floats, since I'm not getting any added precision converting to floats from doubles all the time anyways...所以理想情况下,我想使用浮点数编写所有代码,因为无论如何我都没有得到任何额外的精度从双打转换为浮点数......

However Ive been unable to find a set of maths functions for .net that uses floats.但是,我一直无法为使用浮点数的 .net 找到一组数学函数。 I could of course write my own library in c#, but id rather use an existing library which has been optimised etc...我当然可以在 c# 中编写自己的库,但 id 宁愿使用已经优化的现有库等...

XNA provides several math constants (including 3D specialists, like PI/2 etc.) and some advanced math functions with float precision (though not the trigonometric functions). XNA 提供了几个数学常数(包括 3D 专家,如 PI/2 等)和一些具有浮点精度的高级数学函数(尽管不是三角函数)。 While that is most likely not an option for you, since XNA is not as slim as SlimDX, it certainly means that System.Math is either actually missing something, or the performance hit is not that big.虽然这很可能不是您的选择,因为 XNA 不像 SlimDX 那样纤薄,这当然意味着 System.Math 要么实际上缺少某些东西,要么性能损失没有那么大。 In my experience, you don't actually calculate that much for yourself, because most of that is handled either by the geometry classes (Matrix, Quaternion, Vector) or the GPU itself.根据我的经验,您实际上并没有为自己计算那么多,因为其中大部分是由几何类(矩阵、四元数、向量)或 GPU 本身处理的。

From that point of view, I'd have a look into SlimDX, what they are providing.从这个角度来看,我会看看 SlimDX,他们提供了什么。 Seems like the typical things like matrices, vectors, quaternions (and even 16 bit floats) are already there.似乎矩阵、向量、四元数(甚至 16 位浮点数)等典型事物已经存在。 If you're missing some constants with float precisions, just create them yourself (don't have the cast to be done every time, just at startup).如果您缺少一些具有浮点精度的常量,只需自己创建它们(不必每次都进行转换,只需在启动时进行)。

Yes, you'll make very significant performance gains by working end-to-end in floats if that is all the precision that is required.是的,如果这是所需的全部精度,您将通过在浮点数中端到端工作来获得非常显着的性能提升。

Check out the NMath libraries from CenterSpace software.查看 CenterSpace 软件中的NMath库。 Float, double, and complex types are consistently supported throughout the library.整个库始终支持浮点、双精度和复杂类型。

Paul保罗

Answer on question in title: Yes .回答标题中的问题:是的

Starting with .NET Core 2.0 (.NET Standard 2.1) you can use built-in MathF class to work with float types to perform mathematic operations..NET Core 2.0 (.NET Standard 2.1) 开始,您可以使用内置MathF类来处理浮点类型以执行数学运算。

See docs about MathF type on MSDN请参阅MSDN 上有关MathF类型的文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM