如何在Java中将浮点数组转换为双精度数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2019362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:38:46  来源:igfitidea点击:

How to convert array of floats to array of doubles in Java?

javatype-conversion

提问by fifigyuri

I have an array of floats and I would like to convert it to an array of doubles in Java. I am aware of the obvious way of iterating over the array and creating a new one. I expected Java to digest a float[] smoothly where it wishes to work with double[]... but it can not work with this. What is the elegant, effective way of doing this conversion?

我有一个浮点数组,我想将它转换为 Java 中的双精度数组。我知道迭代数组并创建一个新数组的明显方法。我希望 Java 在它希望与 double[] 一起工作的地方顺利消化 float[] ......但它不能与此一起工作。进行这种转换的优雅、有效的方法是什么?

采纳答案by Jon Skeet

Basically somethinghas to do the conversion of each value. There isn't an implicit conversion between the two array types because the code used to handle them after JITting would be different - they have a different element size, and the float would need a conversion whereas the double wouldn't. Compare this to array covariance for reference types, where no conversions are required when reading the data (the bit pattern is the same for a String reference as an Object reference, for example) and the element size is the same for all reference types.

基本上什么需要做的每一个值的转换。两种数组类型之间没有隐式转换,因为在 JITting 之后用于处理它们的代码会有所不同 - 它们具有不同的元素大小,并且 float 需要转换,而 double 则不需要。将此与引用类型的数组协方差进行比较,其中在读取数据时不需要转换(例如,字符串引用的位模式与对象引用的位模式相同)并且所有引用类型的元素大小都相同。

In short, somethingwill have to perform conversions in a loop. I don't know of any built-in methods to do this. I'm sure they exist in third party libraries somewhere, but unless you happen to be using one of those libraries already, I'd just write your own method. For the sake of convenience, here's a sample implementation:

简而言之,某些东西必须在循环中执行转换。我不知道有任何内置方法可以做到这一点。我确定它们存在于某个第三方库中,但除非您碰巧已经在使用其中一个库,否则我只会编写您自己的方法。为方便起见,这里有一个示例实现:

public static double[] convertFloatsToDoubles(float[] input)
{
    if (input == null)
    {
        return null; // Or throw an exception - your choice
    }
    double[] output = new double[input.length];
    for (int i = 0; i < input.length; i++)
    {
        output[i] = input[i];
    }
    return output;
}

回答by Simon

Do you actually need to copy your float array to a double array? If you are having trouble with the compiler and types when using the floats you can use this...

你真的需要将你的浮点数组复制到一个双数组吗?如果您在使用浮点数时遇到编译器和类型问题,您可以使用它...

float x = 0;
double d = Double.valueOf(x);

What advantage do you get by taking a copy? If you need greater precision in the results of computations based on the floats then make the results double. I can see quite a lot of downsides to having a copy of the array, and performing the copy function, especially if it is large. Make sure you really need to do it.

复印有什么好处?如果您需要在基于浮点数的计算结果中获得更高的精度,则将结果设为double. 我可以看到拥有数组副本和执行复制功能的很多缺点,尤其是在它很大的情况下。确保你真的需要这样做。

HTH

HTH

回答by Aleksandr Dubinsky

In Java 8 you can, if you really want to, do:

在 Java 8 中,如果您真的愿意,您可以执行以下操作:

IntStream.range(0, floatArray.length).mapToDouble(i -> floatArray[i]).toArray();

But it's better (cleaner, faster, better semantics) to use Jon Skeet's function.

但是使用 Jon Skeet 的函数更好(更清晰、更快、更好的语义)。