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

查看:60
本文介绍了如何在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.

更多详细信息:

我知道我可以告诉编译器宽度,但是如何区分两个不同的浮点宽度呢?也许有人也建议更改我的解析方式,即重新解释(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.

推荐答案

这似乎很明显:

在Intel平台和许多其他平台上, 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.

浮点和双精度算法都在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天全站免登陆