C++ 为什么要在 double 上使用 float 或 double 在 long double 上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17603940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 21:17:59  来源:igfitidea点击:

Why would you use float over double, or double over long double?

c++floating-pointdoublelong-double

提问by floatfil

I'm still a beginner at programming and I always have more questions than our book or internet searches can answer (unless I missed something). So I apologize in advance if this was answered but I couldn't find it.

我仍然是编程的初学者,我的问题总是比我们的书或互联网搜索可以回答的要多(除非我错过了什么)。因此,如果有人回答了这个问题,但我找不到,我提前道歉。

I understand that float has a smaller range than double making it less precise, and from what I understand, long double is even more precise(?). So my question is why would you want to use a variable that is less precise in the first place? Does it have something to do with different platforms, different OS versions, different compilers? Or are there specific moments in programming where its strategically more advantageous to use a float over a double/long double?

我知道 float 的范围比 double 小,因此精度较低,据我所知,long double 甚至更精确(?)。所以我的问题是你为什么要首先使用一个不太精确的变量?它是否与不同的平台、不同的操作系统版本、不同的编译器有关?或者在编程中是否有特定的时刻,在战略上使用浮点数而不是双精度数/长双精度数更有利?

Thanks everyone!

谢谢大家!

回答by Mats Petersson

In nearly all processors, "smaller" floating point numbers take the same or less clock-cycles in execution. Sometimes the difference isn't very big (or nothing), other times it can be literally twice the number of cycles for doublevs. float.

在几乎所有的处理器中,“较小的”浮点数在执行时采用相同或更少的时钟周期。有时差异不是很大(或没有),其他时候它可能是doublevs.周期数的两倍float

Of course, memory foot-print, which is affecting cache-usage, will also be a factor. floattakes half the size of double, and long doubleis bigger yet.

当然,影响缓存使用的内存占用也是一个因素。float占用 的一半大小double,并且long double更大。

Edit: Another side-effect of smaller size is that the processor's SIMD extensions (3DNow!, SSE, AVX in x86, and similar extensions are available in several other architectures) may either only work with float, or can take twice as many floatvs. double(and as far as I know, no SIMD instructions are available for long doublein any processor). So this may improve performance if floatis used vs. double, by processing twice as much data in one go. End edit.

编辑:更小的尺寸的另一个副作用是,处理器的SIMD扩展(3DNow!SSE,AVX在86,以及类似的扩展是在几个其他架构可提供)可以或者仅与工作float,或可以作为许多采取两次floatdouble(据我所知,long double任何处理器都没有可用的 SIMD 指令)。因此,如果float使用 与 相比double,这可以通过一次性处理两倍的数据来提高性能。结束编辑。

So, assuming 6-7 digits of precision is good enough for what you need, and the range of +/-10+/-38is sufficient, then floatshould be used. If you need either more digits in the number, or a bigger range, move to double, and if that's not good enough, use long double. But for most things, doubleshould be perfectly adequate.

因此,假设 6-7 位精度足以满足您的需要,并且 +/-10 +/-38的范围就足够了,那么float应该使用。如果您需要数字中的更多位数或更大的范围,请移至double,如果这还不够好,请使用long double。但是对于大多数事情,double应该是完全够用的。

Obviously, the importance of using "the right size" becomes more important when you have either lots of calculations, or lots of data to work with - if there are 5 variables, and you just use each a couple of times in a program that does a million other things, who cares? If you are doing fluid dynamics calculations for how well a Formula 1 car is doing at 200 mph, then you probably have several tens of million datapoints to calculate, and every data point needs to be calculated dozens of times per second of the cars travel, then using up just a few clockcycles extra in each calculation will make the whole simulation take noticeably longer.

显然,当您有大量计算或大量数据需要处理时,使用“正确大小”的重要性变得更加重要 - 如果有 5 个变量,并且您只需在程序中使用每个变量几次一百万其他的东西,谁在乎?如果您正在对一级方程式赛车以 200 英里/小时的速度进行流体动力学计算,那么您可能需要计算数千万个数据点,并且每个数据点都需要在汽车行驶的每秒钟内计算数十次,然后在每次计算中仅使用几个时钟周期额外的时间将使整个模拟花费明显更长的时间。

回答by Patricia Shanahan

There are two costs to using float, the obvious one of its limited range and precision, and, less obviously, the more difficult analysis those limitations impose.

使用浮点数有两个代价,一个显而易见的代价是其有限的范围和精度,而且不太明显的是,这些限制强加的分析越困难。

It is often relatively easy to determine that double is sufficient, even in cases where it would take significant numerical analysis effort to show that float is sufficient. That saves development cost, and risk of incorrect results if the more difficult analysis is not done correctly.

确定 double 是否足够通常相对容易,即使在需要进行大量数值分析才能证明 float 足够的情况下也是如此。如果更困难的分析没有正确完成,这可以节省开发成本和错误结果的风险。

Float's biggest advantage on many processors is its reduced memory footprint. That translates into more numbers per cache line, and more memory bandwidth in terms of numbers transferred per second. Any gain in compute performance is usually relatively slight - indeed, popular processors do all floating point arithmetic in one format that is wider than double.

Float 在许多处理器上的最大优势是它减少了内存占用。这意味着每个缓存行的数量更多,每秒传输的数量也更多,内存带宽也更多。计算性能的任何提升通常都相对较小——事实上,流行的处理器以一种比两倍宽的格式执行所有浮点运算。

It seems best to use double unless two conditions are met - there are enough numbers for their memory footprint to be a significant performance issue, and the developers can show that float is precise enough.

除非满足两个条件,否则最好使用双精度数 - 有足够的数字使其内存占用成为一个重要的性能问题,并且开发人员可以证明浮点数足够精确。

回答by Paul Renton

You might be interested in seeing the answer posted here Should I use double or float?

您可能有兴趣查看此处发布的答案我应该使用 double 还是 float?

But it boils down to memory footprint vs the amount of precision you need for a given situation. In a physics engine, you might care more about precision, so it would make more sense to use a double or long double.

但这归结为内存占用与给定情况下所需的精度。在物理引擎中,您可能更关心精度,因此使用 double 或 long double 会更有意义。

Bottom line: You should only use as much precision as you need for a given algorithm

底线:您应该只使用给定算法所需的精度

回答by aaronman

The basic principle here would be don't use more than you need.

这里的基本原则是不要使用超过你需要的。

The first consideration is memory use, you probably realized that already, if you are making only one double no big deal, but what if you create a billion than you just used twice as much memory space as you had too.

第一个考虑是内存使用,你可能已经意识到,如果你只做一个两倍没什么大不了的,但是如果你创造了十亿,而不是你使用的两倍的内存空间呢?

Next is processor utilization, I believe on many processors if you use smaller data types it can do a form of threading where it does multiple operations at once.

接下来是处理器利用率,我相信在许多处理器上,如果您使用较小的数据类型,它可以执行一种线程化形式,同时执行多个操作。

So an extension to this part of the answer is SSE instructionsbasically this allows you to used packed datato do multiple floating point operations at once, which in an idealizedcase can double the speed of your program.

因此,这部分答案的扩展是SSE 指令,这基本上允许您使用打包数据一次执行多个浮点运算,在理想情况下,这可以使程序速度加倍。

Lastly is readability, when someone is reading your code if you use a float they will immediately realize that you are not going over a certain number. IMO sometimes the right precision number will just flow better in the code.

最后是可读性,当有人阅读你的代码时,如果你使用浮点数,他们会立即意识到你没有超过某个数字。IMO 有时正确的精度数字只会在代码中更好地流动。

回答by Charlie Seligman

A float uses less memory than a double, so if you don't need your number to be the size of a double, you might as well use a float since it will take up less memory.

浮点数使用的内存比双精度数少,所以如果你不需要你的数字是双精度数的大小,你不妨使用浮点数,因为它会占用更少的内存。

Just like you wouldn't use a bus to drive yourself and a friend to the beach... you would be far better off going in a 2 seater car.

就像您不会乘坐公共汽车将自己和朋友带到海滩一样……您最好乘坐 2 座汽车。

The same applies for a double over a long double... only reserve as much memory as you are going to need. Otherwise with more complex code you run the risk of using too much memory and having processes slow down or crash.

这同样适用于双倍长双倍......只保留你需要的内存。否则,对于更复杂的代码,您将面临使用过多内存和进程变慢或崩溃的风险。