如何在 C++ 中同时拥有 32 位和 64 位浮点数 [英] How to have both 32bit and 64bit float in C++

查看:31
本文介绍了如何在 C++ 中同时拥有 32 位和 64 位浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件规范(这里:http://www.septentrio.com/secure/asterx1v_2_1/SBF%20Reference%20Guide.pdf),其中的字段标记为 32 位和 64 位浮点数(参见第 8 页).如何在我的程序中同时使用这两种宽度?我现在正在 Mac OSX 上开发,但我也会在 Linux 机器上部署.

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). 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.

更多详情:

我知道我可以告诉编译器宽度,但是如何区分两种不同的浮点宽度呢?也许有人也有改变我解析方式的建议,即 reinterpret_cast(buffer+offset) 然后使用这些值.这些文件很大 (4GB),所以我需要性能.

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. These file sizes are huge (4GB) so I need performance.

推荐答案

这似乎很明显,但:

在英特尔平台和许多其他平台上,float 是 32 位浮点值,而 double 是 64 位浮点值.试试这个方法.很可能会奏效.

On Intel platform and many others float is 32-bit floating point value, and double is 64-bit floating point value. Try this approach. Most likely it will work.

如果编译器允许,请确保在程序开始时或在编译期间静态检查类型的 sizeof.

To be absolutely sure check sizeof of your types at the start of your program or statically during compilation if your compiler allows this.

再一次,先尝试简单的解决方案.

Once again, try the simple solution first.

Float 和 double 算法都在 Intel 上实现,而且速度很快.在任何情况下,本机算术都是您可以从 CPU 获得的最快的算术.

Float and double arithmetic is both implemented on Intel and it is fast. In any case native arithmetic is the fastest of what you can get from the CPU.

IEEE 754(http://en.wikipedia.org/wiki/IEEE_floating_point)定义不是一种浮点格式,而是几种,如 4、8、16 字节等.它们都有不同的范围和精度,但它们仍然是 IEEE 值.

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.

这篇关于如何在 C++ 中同时拥有 32 位和 64 位浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆