无效的强制转换异常(从数字转换时,必须小于无穷大) [英] invalid cast exception(when casting from a number, must be less than infinity)

查看:90
本文介绍了无效的强制转换异常(从数字转换时,必须小于无穷大)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有两列的表:step,z-value如下:

step | z-value

1 | 1

2 | 4

3 | 9

4 | 16



i我试图遍历各行该表用于获取z值列中的值,以便执行计算以插入新单元格。



这样的事情:



step | z-value | z ^ 3(这是新列但要附加到不同的表格)

1 | 1 | 1

2 | 4 | 16

3 | 9 | 81

4 | 16 | 256



由于某种原因,我没有抓住单元格中的值,我不确定如何继续。



感谢您的帮助。



i have a table with two columns: step, z-value like this:
step|z-value
1|1
2|4
3|9
4|16

i am trying to loop through the rows of the table to grab the value in the z-value column in order to perform a calculation to insert into a new cell.

something like this:

step|z-value|z^3(this is the new column but to be attached to a DIFFERENT table)
1|1|1
2|4|16
3|9|81
4|16|256

for some reason, i am not grabbing the value in the cell and I'm unsure how to proceed.

thank you for any help.

public DataTable zone2(inputParametersBO ipBO)
{
    //imports original table
    DataTable preAngle = paBCBLL.z2BCPA(ipBO);


    //creates new table for calculations to go into
    DataTable dt = new DataTable();
    DataColumn c1 = new DataColumn("RotatedX^2");
    DataColumn c2 = new DataColumn("RotatedX");
    DataColumn c3 = new DataColumn("RotatedY");
    c1.DataType = System.Type.GetType("System.Decimal");
    c2.DataType = System.Type.GetType("System.Decimal");
    c3.DataType = System.Type.GetType("System.Decimal");
    dt.Columns.Add(c1); dt.Columns.Add(c2); dt.Columns.Add(c3);

    foreach (DataRow row in preAngle.Rows)
    {
        double zVal = (double)row["Z2_BC"];//gets value from col2 of orig table
        double xStep = (double)row["step"];//gets value from col1 of orig table
        double rotatedY = Math.Sin((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));
        double rotatedX = Math.Cos((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));
        double rotatedXS = Math.Pow(rotatedX,2);

        DataRow dr = dt.NewRow();
        dr["RotatedX^2"] = rotatedXS;
        dr["RotatedX"] = rotatedX;
        dr["RotatedY"] = rotatedY;
        dt.Rows.Add(dr);
    }
    return dt;
}

推荐答案

不知怎的,你的一个号码是无限的。



设iz为ipBo.z2bcxS。



我想象子表达式(xStep - iz)正在产生无穷大,因为:
(i)xStep为0,iz也是如此。或者

(ii xStep等于iz。



无论哪种方式,rotateX,rotateY和rotatingXS的结果现在都是无限的(任何操作无穷大通常会产生无穷大或NaN)。



所以当你试图(隐含地)将其转换为十进制时它会失败,因为十进制类型不能代表无限数量NaN。



希望这会有所帮助。



------------ ------------



请参阅上面的评论(意外发布在RyanDev的回复中)以获取更多建议。考虑到这似乎很可能问题是写一个双精度到十进制 - 我不认为默认情况下DataRow会尝试任何转换。最简单的解决方案可能是使用:



Somehow one of your numbers is infinite.

Let iz be ipBo.z2bcxS.

I'd imagine the subexpression (xStep - iz) is yielding infinity because either:
(i) xStep is 0 and so is iz. Or
(ii xStep is equal to iz.

Either way, the results of rotatedX, rotatedY and rotatedXS are now infinity (any operation on infinity typically yields infinity or NaN).

So when you attempt to (implictly) convert it to a decimal it fails, because the decimal type cannot represent infinite numbers of NaN.

Hope this helps.

------------------------

See my comment above (accidentally posted in RyanDev's response) for more suggestions. With consideration It seems likely that the issue is writing a double to a decimal - I don't think the DataRow will, by default, attempt any conversions. The simplest solution to this may be to write using:

dr["RotatedX^2"] = (decimal)rotatedXS;





我还会做些什么我建议的其他更改 - 使用类型安全的数据访问方法可以帮助更早地捕获此类错误。



I'd still make some of the other changes I suggested - using type-safe data access methods can help catch such errors earlier.


您好,



我不确定你传递给这种方法的输入参数是什么。



但是,请确保以下数学函数没有发生,因为你指定它们到十进制变量。



Hi,

I am not sure what's the input param that you are passing to this method.

But, please make sure the below mathematical functions aren't happening as you are assigning them to a decimal variables.

double rotatedY = Math.Sin((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));
            double rotatedX = Math.Cos((zVal - ipBO.z2bczS) / (xStep - ipBO.z2bcxS));







当xStep = ipBO.z2bcxS时,根本原因可能是除以零。



为什么不用try catch块添加一些代码,看看是否发现除零异常。




The root cause may be due to divide by zero when xStep = ipBO.z2bcxS.

Why don't you add some code with try catch block and see if you see any divide by zero exception occurs.


这篇关于无效的强制转换异常(从数字转换时,必须小于无穷大)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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