在多种编程语言中寻找具有类似精度的数据类型,例如C/C++,D,Go。

huangapple go评论90阅读模式
英文:

Looking for datatypes of similar precision in multiple programming languages e.g. C/C++, D, Go

问题

我正在尝试使用两种或更多编程语言实现一个涉及浮点数的程序。该程序需要进行50k次迭代,最终将误差降到非常小的值。

为了确保我的结果是可比较的,我想确保在不同的语言中使用相同精度的数据类型。请问C/C++中的float/double与D和Go中的对应关系是什么?我预计C/C++和D在这方面应该非常接近,但不确定。非常感谢。

英文:

I am trying to implement a program with floating point numbers, using two or more programming languages. The program does say 50k iterations to finally bring the error to very small value.

To ensure that my results are comparable, I wanted to make sure I use data types of same precision in different languages. Would you please tell if there is correspondence between float/double of C/C++ to that in D and Go. I expect C/C++ and D to be quite close in this regard, but not sure. Thanks a lot.

答案1

得分: 2

通常情况下,对于编译型语言来说,浮点数的格式和精度取决于两个因素:

  1. 用于实现在硬件上不直接支持的浮点数函数的库。
  2. 系统运行的硬件。

这也可能取决于您提供的编译器选项(以及编译器的智能程度)- 许多现代处理器具有矢量指令,结果可能与使用“常规”浮点指令(例如在x86处理器上的FPU vs. SSE)时略有不同。有时,您可能还会看到差异,因为x86 FPU上的内部计算是80位,当计算完成时存储为64位。

但通常情况下,假设硬件相同,编译器类型相似,我期望从两种不同的(足够相似的)语言中获得相同的结果[和大致相同的性能]。

大多数语言只有“double”(通常为64位)或“single和double”(例如C / C ++中的float-通常为32位和double-通常为64位-可能D也是如此,但我对D不太了解)。

英文:

Generally, for compiled languages, floating point format and precision comes down to two things:

  1. The library used to implement the floating point functions that aren't directly supported in hardware.
  2. The hardware the system is running on.

It may also depend on what compiler options you give (and how sophisticated the compiler is in general) - many modern processors have vector instructions, and the result may be subtly different than if you use "regular" floating point instructions (e.g. FPU vs. SSE on x86 processors). You may also see differences, sometimes, because the internal calculations on an x86 FPU is 80-bits, stored as 64-bits when the computation is completed.

But generally, given the same hardware, and similar type of compilers, I'd expect to get the same result [and roughly the same performance] from two different [sufficiently similar] languages.

Most languages have either only "double" (typically 64-bit) or "single and double" (e.g. float - typically 32-bit and double - typically 64-bit in C/C++ - and probably D as well, but I'm not that into D).

答案2

得分: 2

在Go语言中,浮点数类型遵循IEEE-754标准。

直接从规范中摘录(http://golang.org/ref/spec#Numeric_types)

float32     所有IEEE-754 32位浮点数的集合
float64     所有IEEE-754 64位浮点数的集合

我对D语言不熟悉,但是这个页面可能会有所帮助:http://dlang.org/float.html。

对于C/C++,标准并不要求使用IEEE-754,但是在C++中,你可以使用is_iec559()来检查编译器是否使用IEEE-754。参考这个问题:https://stackoverflow.com/questions/5777484/how-to-check-if-c-compiler-uses-ieee-754-floating-point-standard

英文:

In Go, floating point types follow the IEEE-754 standard.

Straight from the spec (http://golang.org/ref/spec#Numeric_types)

float32     the set of all IEEE-754 32-bit floating-point numbers
float64     the set of all IEEE-754 64-bit floating-point numbers

I'm not familiar with D, but this page might be of interest: http://dlang.org/float.html.

For C/C++, the standard doesn't require IEEE-754, but in C++ you could use is_iec559() to check if your compiler is using IEEE-754. See this question: https://stackoverflow.com/questions/5777484/how-to-check-if-c-compiler-uses-ieee-754-floating-point-standard

huangapple
  • 本文由 发表于 2013年2月1日 01:36:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/14631761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定