简体   繁体   English

需要使用浮点数来提高性能,但需要进行双精度计算

[英]Need to use floats for performance yet want double-precision calculations

MonoGame is an open-source version of Microsoft's XNA. MonoGame是微软XNA的开源版本。 It's a framework for building cross-platform games. 它是构建跨平台游戏的框架。

It has a number of mathematical types such as Vector and Quaternion. 它有许多数学类型,如Vector和Quaternion。

I am a bit baffled by they way they are using doubles and floats. 我对他们使用双打和花车的方式感到有点困惑。

So far I have gathered the following information: 到目前为止,我收集了以下信息:

  • floats are likely to be more efficient than doubles; 花车可能比双打效率更高;
  • doubles have higher precision than floats. 双精度比浮点数更高。

Here is a kind of method that confuses me: 这是一种困惑我的方法:

/// <summary>
/// Transforms a single Vector2, or the vector normal (x, y, 0, 0), by a specified Quaternion rotation.
/// </summary>
/// <param name="value">The vector to rotate.</param><param name="rotation">The Quaternion rotation to apply.</param>
public static Vector2 Transform(Vector2 value, Quaternion rotation)
{
  float num1 = rotation.X + rotation.X;
  float num2 = rotation.Y + rotation.Y;
  float num3 = rotation.Z + rotation.Z;
  float num4 = rotation.W * num3;
  float num5 = rotation.X * num1;
  float num6 = rotation.X * num2;
  float num7 = rotation.Y * num2;
  float num8 = rotation.Z * num3;
  float num9 = (float) ((double) value.X * (1.0 - (double) num7 - (double) num8) + (double) value.Y * ((double) num6 - (double) num4));
  float num10 = (float) ((double) value.X * ((double) num6 + (double) num4) + (double) value.Y * (1.0 - (double) num5 - (double) num8));
  Vector2 vector2;
  vector2.X = num9;
  vector2.Y = num10;
  return vector2;
}

Why not use either doubles of floats throughout (eg inline num1..num8 as double expressions into num9 and num10)? 为什么不使用任何一个浮点数(例如内联num1..num8作为num9和num10的双重表达式)?

The key point here is that a series of calculations are all being done in double , without rounding the intermediate results to float . 这里的关键点是一系列计算都是以double形式完成的,而不是将中间结果舍入为float That may result in the final float result being closer to the one that would have resulted from infinitely precise arithmetic, given the float inputs. 这可能导致最终float结果更接近于无限精确算术产生的结果,给定float输入。

There is little performance difference between 32-bit and 64-bit floating point arithmetic. 32位和64位浮点运算之间的性能差异很小。 There is a big space difference between storing 32-bit and storing 64-bit. 存储32位和存储64位之间存在很大的空白差异。

Halving the number of bytes to store each value may make a big difference in performance. 将存储每个值的字节数减半可能会对性能产生很大影响。 It effectively doubles size of each cache, and the bandwidth of each data transfer path. 它有效地使每个高速缓存的大小加倍,以及每个数据传输路径的带宽。

floats are likely to be more efficient than doubles 花车可能比双打效率更高

This used to be true. 过去这是真的。 You have to go back decades, around the time that graphics algorithms were first designed and had to run on hardware that wasn't very good at accelerating floating point math. 你必须回到几十年前,大约是图形算法最初设计的时间,并且必须在不太擅长加速浮点数学运算的硬件上运行。 Either because it simply didn't have any and it had to be emulated in software, making single precision automatically faster. 或者因为它根本没有任何东西而且它必须在软件中进行仿真,从而使单精度自动更快。 Or because it ran on specially built graphics terminals, the kind that had a custom graphics processor that couldn't handle anything better than single-precision floats. 或者因为它运行在专门构建的图形终端上,那种具有自定义图形处理器的类型无法比单精度浮动更好地处理任何东西。 An FPU wasn't guaranteed on-board until the first Pentium, add a handful of years for a programmer to count on his software running on a machine that has one, a mere 16 years ago. 在第一台Pentium之前,FPU并没有得到保证,为程序员增加了几年的时间来依靠16年前只有一台机器运行的软件。

Of course all the known graphics algorithm were designed to use single-precision. 当然,所有已知的图形算法都设计为使用单精度。 Getting them re-written to use double-precision requires an enormous amount of courage . 让他们重写以使用双精度需要大量的勇气 Because that will inevitably introduce bugs, such an algorithm will not behave the same way as the single-precision one. 因为这将不可避免地引入错误,这样的算法将表现方式与单精度一个相同。 Floating point math is not precise math. 浮点数学不是精确的数学。 Just the fact that the outcome is different is enough to generate a bug report, the single-precision version will be held-up as the normative standard because that's what everybody has been using. 只是结果不同的事实足以产生错误报告,单精度版本将被视为规范标准,因为这是每个人一直在使用的。 With absolutely nothing the programmer can do to make the user happy, other than recommending "don't use it". 除了推荐“不要使用它”之外,程序员完全没有办法让用户满意。

So graphics code doesn't use it. 所以图形代码不使用它。

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

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