ç浮点precision [英] C floating point precision

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

问题描述

可能重复:结果
  浮点比较

我有一个浮动的C / C ++的准确性问题++。当我执行下面的程序:

I have a problem about the accuracy of float in C/C++. When I execute the program below:

#include <stdio.h>

int main (void) {
    float a = 101.1;
    double b = 101.1;
    printf ("a: %f\n", a);
    printf ("b: %lf\n", b);
    return 0;
}

结果:

a: 101.099998
b: 101.100000

我相信浮动应该有32位,所以应该足以存放101.1为什么?

I believe float should have 32-bit so should be enough to store 101.1 Why?

推荐答案

您只能重新present数字恰好IEEE754(至少在单人和双人precision二进制格式),如果他们能够从构造增加两个(即 2 -n 1 ,<$一起倒权力C $ C> 1/2 , 1/4 1/65536 等上)除供precision位的数量。

You can only represent numbers exactly in IEEE754 (at least for the single and double precision binary formats) if they can be constructed from adding together inverted powers of two (i.e., 2-n like 1, 1/2, 1/4, 1/65536 and so on) subject to the number of bits available for precision.

有没有的两个倒置的权力,将让你确切地101.1结合,通过彩车提供的(precision 23位)的比例范围内的的双打($ P的52位$ pcision)。

There is no combination of inverted powers of two that will get you exactly to 101.1, within the scaling provided by floats (23 bits of precision) or doubles (52 bits of precision).

如果你想在这个倒幂两的东西是如何工作的快速教程,请参阅this回答

If you want a quick tutorial on how this inverted-power-of-two stuff works, see this answer.

从回答你的 101.1 若干应用知识(作为一个单独的precision浮动):

Applying the knowledge from that answer to your 101.1 number (as a single precision float):

s eeeeeeee mmmmmmmmmmmmmmmmmmmmmmm    1/n
0 10000101 10010100011001100110011
           |  | |   ||  ||  ||  |+- 8388608
           |  | |   ||  ||  ||  +-- 4194304
           |  | |   ||  ||  |+-----  524288
           |  | |   ||  ||  +------  262144
           |  | |   ||  |+---------   32768
           |  | |   ||  +----------   16384
           |  | |   |+-------------    2048
           |  | |   +--------------    1024
           |  | +------------------      64
           |  +--------------------      16
           +-----------------------       2

的尾数部分却永远持续 101.1

mmmmmmmmm mmmm mmmm mmmm mm
100101000 1100 1100 1100 11|00 1100 (and so on).

因此​​,它不是precision的事,没有有限的比特数量将在IEEE754格式,数字准确地重新present。

hence it's not a matter of precision, no amount of finite bits will represent that number exactly in IEEE754 format.

使用位来计算的实际的数目(最接近)的符号为正。指数是128 + 4 + 1 = 133 - 127偏压= 6,所以乘数是2 6 或64

Using the bits to calculate the actual number (closest approximation), the sign is positive. The exponent is 128+4+1 = 133 - 127 bias = 6, so the multiplier is 26 or 64.

尾数由1(隐式基)加(用于与每个都是值得1 /(2 N )为n从1开始并且向右增加所有那些比特), {1/2,1/16,1/64,1/1024,二千○四十八分之一,16384分之1,32768分之1,262144分之1,524288分之1,四百一十九万四千三百零四分之一,八百三十八万八千六百○八分之一}

The mantissa consists of 1 (the implicit base) plus (for all those bits with each being worth 1/(2n) as n starts at 1 and increases to the right), {1/2, 1/16, 1/64, 1/1024, 1/2048, 1/16384, 1/32768, 1/262144, 1/524288, 1/4194304, 1/8388608}.

当您添加所有这些,你得到 1.57968747615814208984375

When you add all these up, you get 1.57968747615814208984375.

在乘以乘数previously计算, 64 ,你得到 101.09999847412109375

When you multiply that by the multiplier previously calculated, 64, you get 101.09999847412109375.

所有的数字用计算BC 使用的100位十进制数字的规模,导致很多尾随零,所以数字的的是非常精准。双下去,因为我检查的结果有:

All numbers were calculated with bc using a scale of 100 decimal digits, resulting in a lot of trailing zeros, so the numbers should be very accurate. Doubly so, since I checked the result with:

#include <stdio.h>
int main (void) {
    float f = 101.1f;
    printf ("%.50f\n", f);
    return 0;
}

其中的给了我 101.09999847412109375000 ...

这篇关于ç浮点precision的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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