[英]Why float is bigger than double after computing same value? Why float can't be implicitly converted to Decimal
So many arithmetic questions on SO, specially floating point hence I am merging my 2 questions together. 这么多关于SO的算术问题,特别是浮点数,因此我将两个问题合并在一起。
0.3333333
> double 0.333333333333333
? 为什么要浮动0.3333333
>两倍0.333333333333333
? Here is the program that prove it. 这是证明它的程序。
static void Main(string[] args)
{
int a = 1;
int b = 3;
float f = Convert.ToSingle(a) / Convert.ToSingle(b);
double db = Convert.ToDouble(a) / Convert.ToDouble(b);
Console.WriteLine(f > db);
Console.Read();
}
float
can't be implicitly converted to decimal
but int
can? 为什么float
不能隐式转换为decimal
而int
可以呢? eg 例如
decimal d1 = 0.1f; //error
decimal d2 = 1; //no error
For your first question, float
s are actually converted to double
s when you use the >
operator on them. 对于第一个问题,当在float
使用>
运算符时,实际上float
转换为double
。 If you print (double)f
, you'll see its value: 如果打印(double)f
,则会看到其值:
0.333333343267441
While db
is: 而db
是:
0.333333333333333
For the second question, although there isn't an implicit conversion from float
to decimal
, there is an explicit one, so you can use a cast: 对于第二个问题,尽管没有从float
到decimal
的隐式转换,但是有一个显式的转换,因此可以使用强制转换:
float a = 0.1f;
decimal d = (decimal)a;
I can't find anything in the language spec as to why this is, but I speculate that this conversion isn't something that you should do, so you need to be explicit about it. 我在语言规范中找不到关于为什么这样做的任何内容,但我推测这种转换不是您应该执行的操作,因此需要对此进行明确说明。 Why shouldn't you do this? 你为什么不这样做呢? Because decimal
is supposed to represent discrete amounts like currency, while float
and double
are supposed to represent continuous amounts. 因为decimal
应该表示像货币一样的离散数量,而float
和double
则表示连续数量。 They represent two very different things. 它们代表了两个截然不同的事物。
When numerals in source text, such as .3333333
are converted to floating-point, they are rounded to the nearest representable value. 当源文本中的数字(例如.3333333
转换为浮点时,它们将四舍五入为最接近的可表示值。 It so happens that the nearest float
value to .3333333
is slightly greater than .3333333; 碰巧的是,最接近.3333333
float
值略大于.3333333; it is 0.333333313465118408203125, while the double
value nearest to 0.333333333333333 is slightly less than that; 它是0.333333313465118408203125,而最接近0.333333333333333333的double
值则稍小于该值。 it is 0.333333333333332981762708868700428865849971771240234375. 它是0.333333333333332981762708868700428865849971771240234375。 Since 0.333333313465118408203125 is greater than 0.333333333333332981762708868700428865849971771240234375, f > db
evaluates to true. 因为0.333333313465118118203125大于0.333333333333332981762708868700428865849971771240234375,所以f > db
计算为true。
I am unfamiliar with the rules of C# and its decimal type. 我不熟悉C#的规则及其十进制类型。 However, I suspect the reason decimal d1 = 0.1f;
但是,我怀疑decimal d1 = 0.1f;
的原因decimal d1 = 0.1f;
is disallowed while decimal d2 = 1;
decimal d2 = 1;
不允许decimal d2 = 1;
is allowed is that not all float
values can be converted to decimal
without error, while all int
values can be converted to decimal
without error. 允许并非所有float
值都可以无错误地转换为decimal
,而所有int
值都可以无错误地转换为decimal
。 According to Microsoft , decimal
uses a significand of 96 digits, which suffices to represent any int
exactly. 根据Microsoft的说法 , decimal
使用96位有效数字,足以精确表示任何int
。 However, it has smaller range than float
, with its largest finite value being 2 96 −1, around 7.9228•10 28 . 但是,它的范围比float
小,其最大有限值为2 96 -1,大约7.9228•10 28 。 The largest finite float
is 2 128 −2 104 , around 3.4028•10 38 . 最大的有限float
为2 128 -2 104 ,大约为3.4028•10 38 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.