为什么浮点值是这样的? [英] Why the float point value is this?

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

问题描述

当我在阅读关于C ++ Primier Plus中浮点的章节时。

And when I was reading the chapter about float points in C++ Primier Plus.

它给出了一个例子,如下所示:

It gave an example as shown below:

#include <iostream>

int main(){
using namespace std;

float a = 2.34E+22f;
float b = a + 1.0f;

cout << "a =" << a <<endl;
cout << "b -a =" << b - a<< endl;
return 0;
}

其结果是:

a = 2.34e+22
b -a = 0


b $ b

这本书的解释是,我引用:

The explanation from the book is and I quote:


问题是2.34E + 22代表一个数字在
十进制的左边有23个数字,加上1,你试图将1加到该数字的第23位。 但是
类型float只能代表第一个6或7位数字,因此尝试更改
第23位对该值没有影响。

"The problem is that 2.34E+22 represents a number with 23 digits to the left of the decimal. By adding 1, you are attempting to add 1 to the 23rd digit in that number. But type float can represent only the first 6 or 7 digits in a number, so trying to change the 23rd digit has no effect on the value."

但我不明白。

推荐答案

单精度浮点类型

分数部分只有23位,大约小于10 7 。当向 2.34E + 22f 添加一个相当小的数时, float 的精度限制结果的表示, code> b 以 a 不变的值结束。

The fraction part has only 23 bits, roughly less than 107. When you add a rather small number to 2.34E+22f, the precision of float limits the result's representaion, so b end up with unchanged value from a.

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

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