Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#转换为双返回无穷大_C#_Double_Converter_Infinity_Typedescriptor - Fatal编程技术网

C#转换为双返回无穷大

C#转换为双返回无穷大,c#,double,converter,infinity,typedescriptor,C#,Double,Converter,Infinity,Typedescriptor,我有一个方法: public void StoreNumberInSmallestType(ValueType number) { if (numberTypes == null) numberTypes = new List<Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double) }; foreach (Type

我有一个方法:

public void StoreNumberInSmallestType(ValueType number)
{
    if (numberTypes == null)
        numberTypes = new List<Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double) };

    foreach (Type t in numberTypes)
    {
        try
        {
            var converter = TypeDescriptor.GetConverter(t);
            value = converter.ConvertTo(number, t);

            Type = value.GetType();

            return;
        }

        catch (OverflowException) { }
    }
}
最终为
无穷大
。如果我一步一步地完成这个过程,我发现
数字
的值不是
无穷大
,而是用科学符号表示的结果。每当
number
被转换并存储在
value
中时,就会发生一些不好的事情。有人知道为什么
number
保存了正确的值,而
value
没有

编辑: 以下是完整的代码示例:

主要内容:

类别:

namespace ConsoleApplication1
{
    public class Class1
    {
        List<Type> numberTypes;
        dynamic value;
        public Type Type { get; set; }

        public void StoreNumberInSmallestType(ValueType number)
        {
            if (numberTypes == null)
                numberTypes = new List<Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double) };

            foreach (Type t in numberTypes)
            {
                try
                {
                    var converter = TypeDescriptor.GetConverter(t);
                    value = converter.ConvertTo(number, t);

                    Type = value.GetType();

                    return;
                }

                catch (OverflowException) { }
            }
        }
    }
}
命名空间控制台应用程序1
{
公共班级1
{
列出数字类型;
动态值;
公共类型类型{get;set;}
public void storenumbersinsmallesttype(ValueType编号)
{
if(numberTypes==null)
numberTypes=newlist(){typeof(sbyte)、typeof(short)、typeof(int)、typeof(long)、typeof(float)、typeof(double)};
foreach(在numberTypes中输入t)
{
尝试
{
var converter=TypeDescriptor.GetConverter(t);
值=转换器。转换器(数字,t);
Type=value.GetType();
返回;
}
捕获(溢出例外){}
}
}
}
}

当您使用最大值为
3.40282347E+38
且number值为
1.2676506002282294E+230
的单一类型对您的号码进行转换时,就会发生这种情况,因此您超过了该值类型。一旦您将其设置为双精度类型,其值将为
1.2676506002282294E+230

从以上链接:

如果浮点运算结果的大小对于目标格式太大,则运算结果为正整数或负整数,这与结果的符号有关


当您使用最大值为
3.40282347E+38
且number值为
1.2676506002282294E+230
的单一类型对号码进行转换时,就会发生这种情况,因此您超过了该值类型。一旦您将其设置为双精度类型,其值将为
1.2676506002282294E+230

从以上链接:

如果浮点运算结果的大小对于目标格式太大,则运算结果为正整数或负整数,这与结果的符号有关


双转换不能得到无穷大,但浮点转换可以得到无穷大。

在这里,您得到无穷大,没有异常,然后在进行双转换之前,您的代码返回。

对于双转换,您得不到无穷大,但是对于浮点转换。

在那里你得到无穷大,没有异常,然后你的代码在你得到双重转换之前返回。

溢出条件导致无穷大;您可以使用以下任一方法检查此问题:

  • MSDN:
  • MSDN:

浮点转换导致您超出数据类型的限制。

溢出条件导致无穷大;您可以使用以下任一方法检查此问题:

  • MSDN:
  • MSDN:

浮点转换导致您超出了数据类型的限制。

您的代码没有编译,这里缺少一些基本部分。代码是否在泛型类中?很抱歉,lemme只发布了一个完整的示例场景。在转换之前,与其使用try-catch,不如测试值是否大于每种类型的最大值。@benjer3-hmm。。。我没想到。我试试看,谢谢。哦,一本
字典类型的字典怎么样。每个
double
将是相应类型的最大值。然后,您可以对其进行foreach,如果
number
小于
KeyValuePair
,则将其转换为
键的类型
并返回。如果您的代码未编译,则此处缺少一些基本部分。代码是否在泛型类中?很抱歉,lemme只发布了一个完整的示例场景。在转换之前,与其使用try-catch,不如测试值是否大于每种类型的最大值。@benjer3-hmm。。。我没想到。我试试看,谢谢。哦,一本
字典类型的字典怎么样。每个
double
将是相应类型的最大值。然后,您可以对其进行foreach,如果
number
小于
KeyValuePair
值,则将其转换为
键的类型并返回。我很高兴这归结为操作员错误。我觉得双精度真的是一个系统有点奇怪。我很高兴这归结为运算符错误。我觉得双人间真的是一个系统有点奇怪。单人间
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c1 = new Class1();
            c1.StoreNumberInSmallestType(Math.Pow(200, 100));
        }
    }
}
namespace ConsoleApplication1
{
    public class Class1
    {
        List<Type> numberTypes;
        dynamic value;
        public Type Type { get; set; }

        public void StoreNumberInSmallestType(ValueType number)
        {
            if (numberTypes == null)
                numberTypes = new List<Type>() { typeof(sbyte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double) };

            foreach (Type t in numberTypes)
            {
                try
                {
                    var converter = TypeDescriptor.GetConverter(t);
                    value = converter.ConvertTo(number, t);

                    Type = value.GetType();

                    return;
                }

                catch (OverflowException) { }
            }
        }
    }
}