简体   繁体   English

如何在C ++中同时具有32位和64位浮点数

[英]How to have both 32bit and 64bit float in C++

I have a file spec (here: http://www.septentrio.com/secure/asterx1v_2_1/SBF%20Reference%20Guide.pdf ) that has fields marked as both 32-bit and 64-bit floats (see page 8). 我有一个文件规范(在这里: http : //www.septentrio.com/secure/asterx1v_2_1/SBF%20Reference%20Guide.pdf ),其中的字段标记为32位和64位浮点数(请参见第8页)。 How can I use both widths in my program? 如何在程序中同时使用两种宽度? I am developing on Mac OSX right now but I will also deploy on a Linux machine. 我现在正在Mac OSX上进行开发,但是我还将在Linux机器上进行部署。

More details: 更多细节:

I know I could tell the compiler the width, but how could I distinguish two different float widths? 我知道我可以告诉编译器宽度,但是如何区分两个不同的浮点宽度呢? Maybe someone also has a suggestion for changing the way I parse, which is to reinterpret_cast(buffer+offset) and then use the values. 也许有人对我解析的方式提出了建议,那就是reinterpret_cast(buffer + offset)然后使用这些值。 These file sizes are huge (4GB) so I need performance. 这些文件大小很大(4GB),因此我需要性能。

This might seem obvious, nevertheless: 尽管如此,这似乎很明显:

On Intel platform and many others float is 32-bit floating point value, and double is 64-bit floating point value. 在Intel平台和其他平台上, float是32位浮点值,而double是64位浮点值。 Try this approach. 试试这种方法。 Most likely it will work. 最有可能的。

To be absolutely sure check sizeof of your types at the start of your program or statically during compilation if your compiler allows this. 要绝对确保在程序开始时或在编译过程中(如果编译器允许)静态检查类型的sizeof。

Once again, try the simple solution first. 再一次,请首先尝试简单的解决方案。

Float and double arithmetic is both implemented on Intel and it is fast. 浮点运算和双重运算都在Intel上实现,并且速度很快。 In any case native arithmetic is the fastest of what you can get from the CPU. 无论如何,本机算术是您从CPU中获得最快的算术。

IEEE 754 ( http://en.wikipedia.org/wiki/IEEE_floating_point ) defines not one floating point format, but several, like 4, 8, 16 bytes, etc. They all have different range and precision but they are all still IEEE values. IEEE 754( http://en.wikipedia.org/wiki/IEEE_floating_point )定义的不是一种浮点格式,而是几种,如4、8、16字节等。它们都具有不同的范围和精度,但是它们仍然是IEEE值。

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

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