c++ - 将double转换为long double的算法

标签 c++ algorithm floating-point ieee-754

如果 double 是 64 位 IEEE-754 类型并且 long double 是 80 位或 128 位 IEEE-754 类型,则使用的算法是什么硬件(或编译器?)以执行转换:

double d = 3.14159;
long double ld = (long double) d;

此外,如果有人能列出该算法的来源,那就太棒了,因为到目前为止我还没有找到一个来源。

最佳答案

对于像3.14159这样的普通数,过程如下:

separate the number into sign, biased exponent, and significand
add the difference in the exponent biases for long double and double
    (0x3fff - 0x3ff) to the exponent.
assemble the sign, new exponent, and significand (remembering to make the
    leading bit explicit in the Intel 80-bit format).

实际上,在具有 Intel 80 位格式的常见硬件上,“转换”只是 x87 堆栈 (FLD) 的加载指令。除非针对没有硬件支持的平台,否则很少需要处理实际的表示细节。

关于c++ - 将double转换为long double的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23206009/

相关文章:

c++ - 如何使用函数指针从其内存地址调用成员函数?

algorithm - 在哪里可以找到 O(n^2) 和 O(n) 等的含义?

algorithm - 决策树学习算法

c# - 简单 float 计算错误

c++ - AVX2 中排序数组的高效稳定总和

java - 如何从 SplittableRandom 获取 nextFloat() ?

c++ - 如何在 Linux 上用 gcc 编译 intel rand_sse?

c++ - 如何将对象作为参数传递给类

c++ - 如何在类中有一个二维数组作为私有(private)变量,然后在构造函数中设置

c - 查找给定数字的因子的算法..最短方法?