双VS小数四舍五入在C# [英] Double vs Decimal Rounding in C#

查看:196
本文介绍了双VS小数四舍五入在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么:

double dividend = 1.0;
double divisor = 3.0;
Console.WriteLine(dividend / divisor * divisor);



输出1.0,

output 1.0,

decimal dividend = 1;
decimal divisor = 3;
Console.WriteLine(dividend / divisor * divisor);



输出0.9999999999999999999999999999

outputs 0.9999999999999999999999999999

据我所知,三分之一不能被精确计算,因此,必须有一些四舍五入。
但是为什么双轮答案为1.0,而小数不?

I understand that 1/3 can't be computed exactly, so there must be some rounding. But why does Double round the answer to 1.0, but Decimal does not?

此外,为什么双计算1.0 / 3.0是0.33333333333333331?
如果采用四舍五入的话就不是最后3获得四舍五入到0,1为什么?

Also, why does double compute 1.0/3.0 to be 0.33333333333333331? If rounding is used, then wouldn't the last 3 get rounded to 0, why 1?

推荐答案

< STRONG>为什么1/3为双是0.33333333333333331

代表1/3二进制最接近的方式是这样的:
0.0101010101。 ..
这是一样的系列1/4 +(1/4)^ 2 +(1/4)^ 4 ...

The closest way to represent 1/3 in binary is like this: 0.0101010101... That's the same as the series 1/4 + (1/4)^2 + (1/4)^4...

中当然,这是通过可以在双存储的比特的数量的限制。双是64位,但其中之一是符号位,另有11所代表的指数(认为它像科学记数法,但在二进制)。这样的其余部分,这就是所谓的尾数或有效数字是52位。假设1开始,然后使用两个比特的1/4每个后续动力。这意味着你可以存储:
1/4 + 1/4 ^ 2 + ... + ^ 1/4 27
是0.33333333333333331

Of course, this is limited by the number of bits you can store in a double. A double is 64 bits, but one of those is the sign bit and another 11 represent the exponent (think of it like scientific notation, but in binary). So the rest, which is called the mantissa or significand is 52 bits. Assume a 1 to start and then use two bits for each subsequent power of 1/4. That means you can store: 1/4 + 1/4^2 + ... + 1/4 ^ 27 which is 0.33333333333333331

为什么通过3轮这个乘以1

所以1/3的二进制表示,并通过双大小的限制为:
0.010101010101010101010101010101010101010101010101010101
我不是说这是它的存储方式。就像我说的,您存储位1日后开始,并使用单独的位的指数和符号。但我认为,考虑如何你实际上它写在基地2这是很有用的。

So 1/3 represented in binary and limited by the size of a double is: 0.010101010101010101010101010101010101010101010101010101 I'm not saying that's how it's stored. Like I said, you store the bits starting after the 1, and you use separate bits for the exponent and the sign. But I think it's useful to consider how you'd actually write it in base 2.

让我们坚持这个数学家的二进制表示,而忽略双重的大小限制。你不必做这种方式,但我觉得很方便。如果我们想借此逼近1/3然后乘以3,这是一样的移位乘以2,然后添加你开始用什么。这给了我们1/3 * 3 = 0.111111111111111111111111111111111111111111111111111111

Let's stick with this "mathematician's binary" representation and ignore the size limits of a double. You don't have to do it this way, but I find it convenient. If we want to take this approximation for 1/3 and multiply by 3, that's the same as bit shifting to multiply by 2 and then adding what you started with. This gives us 1/3 * 3 = 0.111111111111111111111111111111111111111111111111111111

但双商店?不,要记住,你只能有尾数的52位第1次后,这个数字有54人。所以我们知道这会是圆形的,在这种情况下四舍五入精确1。

But can a double store that? No, remember, you can only have 52 bits of mantissa after the first 1, and that number has 54 ones. So we know that it'll be rounded, in this case rounded up to exactly 1.

为什么十进制你0.9999999999999999999999999999

使用小数,将得到96位来表示的整数,附加比特代表指数可达10 28权力因此,即使最终这一切都存储为二进制,在这里我们'再有10次幂工作,所以是有意义的认为这个数字的10为基数为96位让我们快递到79,228,162,514,264,337,593,543,950,335,但代表的1/3,我们要一起去所有3个的,由他们的28我们可以转移到小数点右边:0.3333333333333333333333333333

With decimal, you get 96 bits to represent an integer, with additional bits representing the exponent up to 28 powers of 10. So even though ultimately it's all stored as binary, here we're working with powers of 10 so it makes sense to think of the number in base 10. 96 bits lets us express up to 79,228,162,514,264,337,593,543,950,335, but to represent 1/3 we're going to go with all 3's, up to the 28 of them that we can shift to the right of the decimal point: 0.3333333333333333333333333333.

乘以3这种近似1/3给了我们一个数字,我们可以确切地表示。这只是28 9的,全部转移到了小数点右边:0.9999999999999999999999999999。所以不像双的有没有第二轮在这一点上四舍五入。

Multiplying this approximation for 1/3 by 3 gives us a number we can represent exactly. It's just 28 9's, all shifted to the right of the decimal point: 0.9999999999999999999999999999. So unlike with double's there's not a second round of rounding at this point.

这篇关于双VS小数四舍五入在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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