c++ - c++中long double的实现

标签 c++

最近我在研究 C++ 中的类型,我有几个关于 long double 的问题。例如,我有一些数字 long double x = -48.12e4,那么我应该将 0.12 转换为 hex 或 bin 的准确度如何(我更喜欢使用 hex ,更易于阅读)。在实现方案中,我有 1 个额外位,但我不知道它的作用 -

//scheme 
1bit    15bit   1bit   63bit 
sign    e        1       m

例如,让我们以我之前写的数字为例。

1)translate decimal to hex 
-48.12 = -3.1E (are 2 digits after decimal enough?)

2)normalization 
0011.0001 1110  * 10^0 =  001.1 0001 1110 * 10^1

3)calculation of "e"
16 383 + 4 + 1 = 16 388 = 4004(hex) = 0100 0000 0000 0100

4)collecting everything together
1    0100 0000 0000 0100   1    1000 1111 0000 0..0
sign|        e           |bit|      mantisa 
What is that 1 bit for?
5)reverse order
0..000  0011 1100 0110  0000 0010  1010 0000 

我做对了吗?

最佳答案

来自 cppreference (强调我的):

long double - extended precision floating point type. Does not necessarily map to types mandated by IEEE-754. Usually 80-bit x87 floating point type on x86 and x86-64 architectures.

您假定 long double 的特定布局,这绝不是标准所保证的,但可能因编译器和编译器的不同而有所不同,具体取决于目标体系结构。

关于c++ - c++中long double的实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46446846/

相关文章:

java - 从Java学C++,尝试做一个链表

c++ - OpenCL 在 NVidia 硬件上生成 QNaN

c++ - 具有继承性的可变参数模板和默认值

c++ - 创建多少个线程?

c++ - 2D 射弹 - 速度和位置 vector

c++ - 在 c++11 中,线程是否类似于 node.js/javascript 回调?

c++ - C++ 输出时出现段错误

c++ - GCC linux 上最好的(即快速且小)是atomic_fetch_ptr 和atomic_set_ptr

c++ - 我需要使用什么 Qt 类型来将 lambda 作为函数参数传递?

c++ - 如何强制调整 indexWidgets 的大小以适应 Qt5 QTableView 的单元格