我应该使用双精度还是浮点?

发布于 2024-07-25 22:46:54 字数 36 浏览 5 评论 0原文

在 C++ 中使用其中一种而不是另一种的优点和缺点是什么?

What are the advantages and disadvantages of using one instead of the other in C++?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

银河中√捞星星 2024-08-01 22:46:55

除非您有特殊原因要这样做,否则请使用 double。

也许令人惊讶的是,C(和 C++)中的“正常”浮点类型是 double 而不是 float。 标准数学函数(例如 sinlog)采用双精度数作为参数,并返回双精度数。 普通浮点文字(如在程序中编写 3.14 时)的类型为 double。 不浮动。

在典型的现代计算机上,双精度数可以与浮点数一样快,甚至更快,因此即使对于大型计算,性能通常也不是需要考虑的因素。 (这些计算必须是大型,否则您根本不应该考虑性能。我的新 i7 台式计算机可以在一秒钟内执行 60 亿次双精度乘法。)

Unless you have some specific reason to do otherwise, use double.

Perhaps surprisingly, it is double and not float that is the "normal" floating-point type in C (and C++). The standard math functions such as sin and log take doubles as arguments, and return doubles. A normal floating-point literal, as when you write 3.14 in your program, has the type double. Not float.

On typical modern computers, doubles can be just as fast as floats, or even faster, so performance is usually not a factor to consider, even for large calculations. (And those would have to be large calculations, or performance shouldn't even enter your mind. My new i7 desktop computer can do six billion multiplications of doubles in one second.)

反目相谮 2024-08-01 22:46:55

这个问题无法回答,因为没有上下文。 以下是一些可能影响选择的因素:

  1. 浮点型、双精度型和长双精度型的编译器实现。 C++ 标准规定:

    <块引用>

    浮点类型共有三种:float、double 和 long double。 double 类型至少提供与 float 一样多的精度,而 long double 类型至少提供与 double 一样多的精度。

    因此,这三个在内存中的大小可以相同。

  2. 存在 FPU。 并非所有 CPU 都有 FPU,有时会模拟浮点类型,有时只是不支持浮点类型。

  3. FPU 架构。 IA32 的 FPU 内部为 80 位 - 32 位和 64 位浮点在加载时扩展为 80 位并在存储时减少。 还有 SIMD 可以并行处理四个 32 位浮点或两个 64 位浮点。 标准中没有定义 SIMD 的使用,因此需要编译器进行更复杂的分析来确定是否可以使用 SIMD,或者需要使用特殊函数(库或内在函数)。 80 位内部格式的结果是,根据数据保存到 RAM 的频率,您可能会得到略有不同的结果(因此会损失精度)。 因此,编译器不会特别好地优化浮点代码。

  4. 内存带宽。 如果双精度数比浮点数需要更多的存储空间,则读取数据将花费更长的时间。 这就是天真的答案。 在现代 IA32 上,这完全取决于数据的来源。 如果它位于 L1 缓存中,则只要数据来自单个缓存行,负载就可以忽略不计。 如果它跨越多个缓存行,则会产生很小的开销。 如果它来自 L2,则需要更长的时间,如果它在 RAM 中,则需要更长的时间,最后,如果它在磁盘上,则需要很长的时间。 因此,选择 float 或 double 并不重要,重要的是数据的使用方式。 如果您想对大量连续数据进行小型计算,则最好选择小型数据类型。 在小数据集上进行大量计算将允许您使用更大的数据类型,从而产生显着的效果。 如果您非常随机地访问数据,那么数据大小的选择并不重要 - 数据加载在页面/缓存行中。 因此,即使您只需要 RAM 中的一个字节,也可以传输 32 个字节(这很大程度上取决于系统的架构)。 最重要的是,CPU/FPU 可以是超标量(也称为流水线)。 因此,即使加载可能需要几个周期,CPU/FPU 也可能忙于执行其他操作(例如乘法),从而在一定程度上隐藏了加载时间。

  5. 该标准不强制执行浮点值的任何特定格式。

如果您有规格,那么它将指导您做出最佳选择。 否则,使用什么就取决于经验了。

This question is impossible to answer since there is no context to the question. Here are some things that can affect the choice:

  1. Compiler implementation of floats, doubles and long doubles. The C++ standard states:

    There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double.

    So, all three can be the same size in memory.

  2. Presence of an FPU. Not all CPUs have FPUs and sometimes the floating point types are emulated and sometimes the floating point types are just not supported.

  3. FPU Architecture. The IA32's FPU is 80bit internally - 32 bit and 64 bit floats are expanded to 80bit on load and reduced on store. There's also SIMD which can do four 32bit floats or two 64bit floats in parallel. Use of SIMD is not defined in the standard so it would require a compiler that does more complex analysis to determine if SIMD can be used, or requires the use of special functions (libraries or intrinsics). The upshot of the 80bit internal format is that you can get slightly different results depending on how often the data is saved to RAM (thus, losing precision). For this reason, compilers don't optimise floating point code particularly well.

  4. Memory bandwidth. If a double requires more storage than a float, then it will take longer to read the data. That's the naive answer. On a modern IA32, it all depends on where the data is coming from. If it's in L1 cache, the load is negligible provided the data comes from a single cache line. If it spans more than one cache line there's a small overhead. If it's from L2, it takes a while longer, if it's in RAM then it's longer still and finally, if it's on disk it's a huge time. So the choice of float or double is less important than the way the data is used. If you want to do a small calculation on lots of sequential data, a small data type is preferable. Doing a lot of computation on a small data set would allow you to use bigger data types with any significant effect. If you're accessing the data very randomly, then the choice of data size is unimportant - data is loaded in pages / cache lines. So even if you only want a byte from RAM, you could get 32 bytes transferred (this is very dependant on the architecture of the system). On top of all of this, the CPU/FPU could be super-scalar (aka pipelined). So, even though a load may take several cycles, the CPU/FPU could be busy doing something else (a multiply for instance) that hides the load time to a degree.

  5. The standard does not enforce any particular format for floating point values.

If you have a specification, then that will guide you to the optimal choice. Otherwise, it's down to experience as to what to use.

ぶ宁プ宁ぶ 2024-08-01 22:46:55

Double 更精确,但编码为 8 个字节。 float 只有 4 个字节,因此空间较小且精度较低。

如果您的应用程序中有 double 和 float,您应该非常小心。 由于过去我有一个错误。 代码的一部分使用 float,而代码的其余部分使用 double。 将 double 复制到 float,然后将 float 复制到 double 可能会导致精度错误,从而产生很大的影响。 就我而言,它是一家化工厂......希望它没有造成严重后果:)

我认为正是由于这种错误,几年前阿丽亚娜 6 号火箭才爆炸!

仔细考虑变量使用的类型

Double is more precise but is coded on 8 bytes. float is only 4 bytes, so less room and less precision.

You should be very careful if you have double and float in your application. I had a bug due to that in the past. One part of the code was using float while the rest of the code was using double. Copying double to float and then float to double can cause precision error that can have big impact. In my case, it was a chemical factory... hopefully it didn't have dramatic consequences :)

I think that it is because of this kind of bug that the Ariane 6 rocket has exploded a few years ago!!!

Think carefully about the type to be used for a variable

笑看君怀她人 2024-08-01 22:46:55

我个人一直追求双倍,直到看到一些瓶颈。 然后我考虑转向浮动或优化其他部分

I personnaly go for double all the time until I see some bottlenecks. Then I consider moving to float or optimizing some other part

朦胧时间 2024-08-01 22:46:55

这取决于编译器如何实现 double。 double 和 float 是同一类型是合法的(在某些系统上也是如此)。

话虽这么说,如果它们确实不同,主要问题是精度。 由于大小不同,双精度数具有更高的精度。 如果您使用的数字通常会超过浮点数的值,则使用双精度数。

其他几个人提到了性能问题。 这将是我考虑事项清单上的最后一个。 正确性应该是您的首要考虑因素。

This depends on how the compiler implements double. It's legal for double and float to be the same type (and it is on some systems).

That being said, if they are indeed different, the main issue is precision. A double has a much higher precision due to it's difference in size. If the numbers you are using will commonly exceed the value of a float, then use a double.

Several other people have mentioned performance isssues. That would be exactly last on my list of considerations. Correctness should be your #1 consideration.

回眸一遍 2024-08-01 22:46:55

我认为无论差异如何(正如每个人都指出的那样,浮动占用的空间更少并且通常更快)......有人曾经遇到过使用 double 的性能问题吗? 我说使用 double...如果稍后您决定“哇,这真的很慢”...找到您的性能瓶颈(这可能不是您使用 double 的事实)。 然后,如果它对你来说仍然太慢,看看哪里可以牺牲一些精度并使用浮动。

I think regardless of the differences (which as everyone points out, floats take up less space and are in general faster)... does anyone ever suffer performance issues using double? I say use double... and if later on you decide "wow, this is really slow"... find your performance bottleneck (which is probably not the fact you used double). THEN, if it's still too slow for you, see where you can sacrifice some precision and use float.

我很OK 2024-08-01 22:46:55

使用获得适当结果所需的精度。 如果您随后发现代码的性能不如您想要的那样(您使用的分析正确吗?)请查看:

Use whichever precision is required to achieve the appropriate results. If you then find that your code isn't performing as well as you'd like (you used profiling correct?) take a look at:

谁人与我共长歌 2024-08-01 22:46:55

它在很大程度上取决于 CPU,最明显的权衡是精度和内存之间。 对于 GB 的 RAM,内存并不是什么大问题,因此通常最好使用 double 。

至于性能,很大程度上取决于CPU。 在 32 位机器上,float 通常比 double 获得更好的性能。 在 64 位上,double 有时更快,因为它(通常)是本机大小。 尽管如此,比您选择的数据类型更重要的是您是否可以利用处理器上的 SIMD 指令。

It depends highly on the CPU the most obvious trade-offs are between precision and memory. With GBs of RAM, memory is not much of an issue, so it's generally better to use doubles.

As for performance, it depends highly on the CPU. floats will usually get better performance than doubles on a 32 bit machine. On 64 bit, doubles are sometimes faster, since it is (usually) the native size. Still, what will matter much more than your choice of data types is whether or not you can take advantage of SIMD instructions on your processor.

私藏温柔 2024-08-01 22:46:55

double 具有更高的精度,而 float 占用更少的内存并且速度更快。 一般来说,您应该使用浮动,除非您遇到不够准确的情况。

double has higher precision, whereas floats take up less memory and are faster. In general you should use float unless you have a case where it isn't accurate enough.

丶视觉 2024-08-01 22:46:55

float 和 double 之间的主要区别在于精度。 维基百科有更多关于
单精度(浮点)和双精度

The main difference between float and double is precision. Wikipedia has more info about
Single precision (float) and Double precision.

又爬满兰若 2024-08-01 22:46:54

如果您想知道真正的答案,您应该阅读 什么每个计算机科学家都应该了解浮点运算

简而言之,尽管double在其表示中允许更高的精度,但对于某些计算,它会产生更大的误差。 “正确”的选择是:使用所需的精度,但不要更高选择正确的算法

许多编译器无论如何都会在“非严格”模式下进行扩展浮点数学(即使用硬件中可用的更广泛的浮点类型,例如80位和128位浮点),这也应该考虑在内。 在实践中,您几乎看不到速度上的任何差异——无论如何,它们都是硬件本身的。

If you want to know the true answer, you should read What Every Computer Scientist Should Know About Floating-Point Arithmetic.

In short, although double allows for higher precision in its representation, for certain calculations it would produce larger errors. The "right" choice is: use as much precision as you need but not more and choose the right algorithm.

Many compilers do extended floating point math in "non-strict" mode anyway (i.e. use a wider floating point type available in hardware, e.g. 80-bits and 128-bits floating), this should be taken into account as well. In practice, you can hardly see any difference in speed -- they are natives to hardware anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文