关于IEEE 754,64位双待? [英] Question regarding IEEE 754, 64 bits double?

查看:143
本文介绍了关于IEEE 754,64位双待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面的内容看看:

我知道如何双转换为基于IEEE 754二进制,但我不明白什么公式用于

I understand how to convert a double to a binary based on IEEE 754. But I don't understand what the formula is used for.

谁能给我一个例子,当我们用上述公式,好吗?

Can anyone give me an example when we use the above formula, please?

非常感谢。

推荐答案

这是用红色突出显示该公式可用于计算的实数的一个64位的值,再presents如果作为一个IEEE 754双处理。如果你想手动计算从二进制它重新presents,如核实C库的实施的printf

The formula that is highlighted in red can be used to calculate the real number that a 64-bit value represents when treated as a IEEE 754 double. It's only useful if you want to manually calculate the conversion from binary to the base-10 real number that it represents, such as when verifying the correctness of a C library's implementation of printf.

例如,使用上 0x3fd5555555555555 配方,的 X 的发现是完全0.333333333333333314829616256247390992939472198486328125。这是实数 0x3fd5555555555555 重新presents。

For example, using the formula on 0x3fd5555555555555, x is found to be exactly 0.333333333333333314829616256247390992939472198486328125. That is the real number that 0x3fd5555555555555 represents.

#include <stdio.h>
#include <stdlib.h>

int main()
{
  union {
    double d;
    unsigned long long ull;
  } u;

  u.ull = 0x3fd5555555555555L;
  printf("%.55f\n", u.d);

  return EXIT_SUCCESS;
}

HTTP://$c$cpad.org/kSithgZQ

编辑:作为奥洛夫评论说,一个IEEE 754双正是重新presents方程中的值的 X 的,但不是所有的实数都完全相同重新presentable。事实上,只有实数的有限数字,如0.5,0.125和0.333333333333333314829616256247390992939472198486328125的的正是重新presentable,而绝大多数(的不可数许多),包括1/3,0.1,0.4,和的π

As Olof commented, an IEEE 754 double exactly represents the value x in the equation, but not all real numbers are exactly representable. In fact, only a finite number of reals such as 0.5, 0.125, and 0.333333333333333314829616256247390992939472198486328125 are exactly representable, while the vast majority (uncountably many) including 1/3, 0.1, 0.4, and π are not.

要了解是否真正的关键是准确-RE presentable作为一个IEEE 754双是计算实数的二进制重新presentation和它的科学记数法(如B1.001×2 1 )。如果二进制数字到不含尾随零的小数点右边的数量小于或等于52和指数减去一个是-1022和+1023之间,包括,则数的的准确再presentable。

The key to knowing whether a real is exactly-representable as an IEEE 754 double is to calculate the real number's binary representation and write it in scientific notation (e.g. b1.001×2-1 for 0.5625). If the number of binary digits to the right of the decimal point excluding trailing zeroes is less than or equal to 52 and the exponent minus one is between -1022 and +1023, inclusive, then the number is exactly representable.

让我们通过几个例子。请注意,它有助于手头上有一个arbitrary- precision计算器。我将使用 ARIBAS

Let's go through a couple of examples. Note that it helps to have an arbitrary-precision calculator on hand. I will use ARIBAS.


  1. 数1/64是十进制0.015625。要计算其二进制重新presentation,我们可以使用ARIBAS德code_float 功能:


 ==> set_floatprec(double_float).
-: 64

==> 1/64.
-: 0.0156250000000000000

==> set_printbase(2).
-: 0y10

==> decode_float(1/64).
-: (0y10000000_00000000_00000000_00000000_00000000_00000000_00000000_00000000, 
-0y1000101)

==> set_printbase(10).
-: 10

==> -0y1000101.
-: -69

因此​​1/64 = b0.000001或B1.0×2 -6 科学记数法。

1/64的的确切-RE presentable。

1/64 is exactly-representable.

数= 1/10十进制0.1。要计算其二进制重新presentation:

The number 1/10 = 0.1 in decimal. To calculate its binary representation:


==> set_printbase(2).
-: 0y10

==> decode_float(1/10).
-: (0y11001100_11001100_11001100_11001100_11001100_11001100_11001100_11001100, 
-0y1000011)

==> set_printbase(10).
-: 10

==> -0y1000011.
-: -67

所以1/10 = 0.1 = b0.000 1100 (其中粗体重新presents重复数字序列),或b1.100 1100 ×2 -4 科学记数法。

So 1/10 = 0.1 = b0.0001100 (where bold represents a repeating digit sequence), or b1.1001100×2-4 in scientific notation.

1/10的不是的确切-RE presentable。

1/10 is not exactly-representable.

这篇关于关于IEEE 754,64位双待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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