简体   繁体   English

将double [] []类型转换为float

[英]converting type double[][] to float

I have a function signature like: 我有一个功能签名,如:

static public double[][] G2Sweep(int row1, int row2, double[][] mtx)
{
    mtx = (float) mtx;
}

But I need to covert the double[][] matrix into float values. 但我需要将double [] []矩阵转换为浮点值。 How is this done as the code can't explicitly convert it? 这是如何完成的,因为代码无法显式转换它?

public static float[][] Convert(double[][] mtx)
{
    var floatMtx = new float[mtx.Length][];
    for (int i = 0; i < mtx.Length; i++)
    {
        floatMtx[i] = new float[mtx[i].Length];
        for (int j = 0; j < mtx[i].Length; j++)
            floatMtx[i][j] = (float)mtx[i][j];
    }
    return floatMtx;
}

Or: 要么:

public static float[][] Convert(double[][] mtx)
{
    return mtx.Select(i => i.Select(j => (float)j).ToArray()).ToArray();
}

No, you can't convert double to float in place, especially for arrays. 不,你不能将double转换为float,特别是对于数组。 You need to create copy with correct type. 您需要创建具有正确类型的副本。

Another way you can do this by is using the Array.ConvertAll method: 另一种方法是使用Array.ConvertAll方法:

Array.ConvertAll<double, float>(doubles, d => (float)d);

Does the same looping and explicit converting, but looks better. 是否循环和显式转换相同,但看起来更好。

yes all values to float, but i'd prefer not having to do a for loop to lop through it all 是的所有浮动值,但我不想做一个for循环来完成它

Well, sorry, but you can't. 好吧,抱歉,但你不能。 Casting an array to a type like float (or int , or String , or MonkeyPoo , or whatever) makes no sense. 将数组转换为类型为float (或int ,或String ,或MonkeyPoo ,或其他MonkeyPoo )是没有意义的。 If you need to look at every value in an array then you need a loop. 如果你需要查看数组中的每个值,那么你需要一个循环。 There is no getting around that fact. 没有绕过那个事实。

Lamdas and whatnot all boil down to a loop as well. Lamdas和诸如此类的东西都归结为一个循环。 You'll just need to bite the bullet and either A) Convert in a loop (or use something like .Cast<float> ), or use the correct type to begin with. 你只需要咬住子弹和A)转换循环(或使用像.Cast<float>这样的东西),或者使用正确的类型开始。

It's true that you cant convert in place and that you have to loop through the values. 确实,你无法在适当的位置进行转换,并且必须循环遍历这些值。

However, it's fair* to say nowadays that memory is cheap . 但是,现在说内存很便宜是公平的。

At the point of creating your double[][] variable; 在创建double [] []变量时; why not simply also create a float[][] at the same time, so the conversion is done one, in place. 为什么不简单地同时创建一个float [] [],所以转换是在一个地方完成的。

that way in the rest of your lifecycle, you can just use the write array for the write task. 在生命周期的其余部分中,您可以将写入数组用于写入任务。

That said, can you clarify why you need a distinct float and double array ? 那说,你能澄清为什么你需要一个独特的浮点数和双数组吗?

*Fair but not necessarily acceptable; *公平但不一定可以接受; if it's a webapp hosted on its own box or virtual image; 如果它是一个托管在自己的盒子或虚拟图像上的webapp; then it's fine. 那很好。 If this is a standalone app that might have to work on a netbook or in Silverlight somewhere, it is not fair or fine. 如果这是一个独立的应用程序,可能必须在上网本或某个地方的Silverlight上工作,这是不公平或罚款。

You can avoid writing the loop by hand if you use Linq. 如果使用Linq,可以避免手动编写循环。

float[][] floats = mtx.Select(r=>r.Select(Convert.ToSingle).ToArray()).ToArray();

EDIT: fixed. 编辑:修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM