简体   繁体   English

为什么在计算相同的值后float大于两倍? 为什么不能将float隐式转换为Decimal

[英]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的算术问题,特别是浮点数,因此我将两个问题合并在一起。

  1. Why float 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();
        }
  1. Why float can't be implicitly converted to decimal but int can? 为什么float不能隐式转换为decimalint可以呢?

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: 对于第二个问题,尽管没有从floatdecimal的隐式转换,但是有一个显式的转换,因此可以使用强制转换:

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应该表示像货币一样的离散数量,而floatdouble则表示连续数量。 They represent two very different things. 它们代表了两个截然不同的事物。

  1. 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。

  2. 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.

相关问题 为什么浮点/双精度 MinValue/MaxValue 不能放在小数内? - Why can't float/double MinValue/MaxValue fit inside a decimal? 为什么不能将 Double 隐式转换为 Decimal - Why can't Double be implicitly cast to Decimal 如果C#中的十进制值大于float,则如何引发异常 - how throw exception if decimal value is bigger than float in C# 为什么从int,double,float和decimal转换为char起作用,但是当那些相同的变量预先转换为IConvertible时,不是吗? - Why casting from int, double, float and decimal to char works, but when those same variables are beforehand casted to IConvertible, doesn't? 为什么float的精度与double的精度相同? - Why is float's precision the same as double's? 为什么我的float变量语句不断显示“无法将类型double隐式转换为float”错误? - why does my float variable statement keeps on giving me an 'Cannot implicitly convert type double to float' error? 为什么不能将类型'UnityEngine.Vector2'隐式转换为'float'? - Why can I not implicitly convert type 'UnityEngine.Vector2' to 'float'? 为什么装箱的整数值会隐式转换为字符串类型? - Why can boxed integer value get implicitly converted to string type? 为什么Float转换忽略小数点后的零? - Why Float conversion ignore zero's after decimal point? 可以转换为浮点数然后再转换回十进制的最小小数是多少? - What is the minimum decimal which can be converted to float and then back to decimal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM