本文介绍了请问C轮浮点常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<一个href=\"http://stackoverflow.com/questions/22337418/golang-floating-point-$p$pcision-float32-vs-float64\">A关于围棋浮点precision 让我不知道Ç如何处理这个问题的问题。

A question about floating-point precision in Go made me wonder how C handles the problem.

在C以下code:

float a = 0.1;

威尔 A 有最亲密的IEEE 754二进制重新presentation:

Will a have the closest IEEE 754 binary representation of:

00111101110011001100110011001101 (Decimal:  0.10000000149011612)

还是会只是它作物:

or will it just crop it to:

00111101110011001100110011001100 (Decimal:  0.09999999403953552)

还是会依赖于编译器/平台有什么不同?

Or will it differ depending on compiler/platform?

推荐答案

这是实现允许做任何(或甚至一个更多关闭):

An implementation is allowed to do either (or even be off by one more):

有关十进制浮点常量,也为十六进制浮点常量时FLT_RADIX不是2的幂,其结果是要么最近重新presentable值,或更大或更小重presentable紧邻值最近重新presentable价值,在实现定义的方式选择。

(C11,与教派; 6.4.4.2/3)

(C11, §6.4.4.2/3)

C99以来,我们已经有十六进制浮点常量,使您可以指定precisely你想(假设实现提供二进制浮点:))位,所以你可以说,例如:

Since C99, we've had hexadecimal floating point constants so that you can specify precisely the bits you want (assuming that the implementation offers binary floating point :) ), so you could have said, for example:

float a = 0x1.99999Ap-4;

有关IEEE 754 32位彩车:

For IEEE 754 32-bit floats:

#include <stdio.h>
int main() {
  float a = 0.1;
  float low  = 0x0.1999999p0;
  float best = 0x0.199999ap0;
  float high = 0x0.199999bp0;
  printf("a is %.6a or %.16f, which is either %.16f, %.16f or %.16f\n",
          a, a, low, best, high);
  return 0;
}

这篇关于请问C轮浮点常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 03:41