简体   繁体   English

C语言中long long的实现

[英]Implementation of long double in c++

Recently I was doing some research about types in c++ and I have couple questions about long double . 最近我正在做一些有关c ++类型的研究,我对long double有几个问题。 For instance I have some number long double x = -48.12e4 , then how accurate should I translate 0.12 to hex or bin (I prefer to use hex , easer to read). 例如,我有一些数字long double x = -48.12e4 ,那么我应该将0.12转换为十六进制或bin的精度是多少(我更喜欢使用hex,更容易阅读)。 In the scheme of implementation I have 1 extra bit and I have no idea about its role - 在实施方案中,我多了1位,并且我不知道它的作用-

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

For example lets take number that I have written before. 例如,以我以前写的数字为准。

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 

Have I done everything right? 我做对了吗?

From cppreference (emphasis mine) : cppreference (强调我的):

long double - extended precision floating point type. long double-扩展精度浮点类型。 Does not necessarily map to types mandated by IEEE-754. 不一定映射到通过IEEE-754规定的类型。 Usually 80-bit x87 floating point type on x86 and x86-64 architectures. 通常在x86和x86-64体系结构上使用80位x87浮点类型。

You assume a specific layout of long double that is by no means guaranteed by the standard but may vary from compiler to compiler and depending on the target architecture. 您假定标准不保证使用long double的特定布局,但是该布局可能因编译器的不同而有所不同,具体取决于目标体系结构。

The problem you have is that you're doing decimal math. 您遇到的问题是您正在做十进制数学运算。 That's logical for humans, but not how computers work. 对于人类来说,这是合乎逻辑的,但对于计算机的工作方式却不是。

In particular, -48.12e4 is specified as sign,mantissa,exponent, and that's also how the C++ implementation of long double works, but the implicit base for the exponent differs. 特别是, -48.12e4被指定为符号,尾数,指数,这也是long double的C ++实现的工作方式,但指数的隐含基础不同。 e4 signifies a power of 10, but long double uses base-2 (or base-16, or another binary base). e4表示10的幂,但long double使用base-2(或base-16,或另一个二进制基数)。

The consequence is that the compiler cannot just convert exponents. 结果是编译器不能只转换指数。 10^3 is approximately 2^10, but not even close enough for float let along long double . 10 ^ 3 大约为 2 ^ 10,但距离float long double距离还不够近。

So, the proper solution is a lot harder. 因此,正确的解决方案要困难得多。 Perhaps the easiest solution is to calculate long double(4812) and long double(1e2) , and to multiply those. 也许最简单的解决方案是计算long double(4812)long double(1e2)并将其相乘。 That way, you only need to implement the conversion of integers to long double and the conversion of powers of 10 to long double . 这样,你只需要实现整数的转换long double和10次幂的转换long double

The conversion of small positive powers of 10 to long double is also easy, reuse the integer code. 将10的小正幂转换为long double整数也很容易,可以重用整数代码。 For large even positive powers, use squaring. 对于大正功率,请使用平方。 For negative powers of 10, use the fact that pow(10,n)=1.0/pow(10,-n) . 对于10的负幂,请使用pow(10,n)=1.0/pow(10,-n)的事实。

To get an idea how real compilers do it (which is a fair bit more efficient), look at open-source implementations of strtod() . 为了了解真正的编译器是如何做到的(效率要高得多),请查看strtod()的开源实现。

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

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