24

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?

1
  • 1
    There is no way to "elegantly" do this because float and double are totally different datatypes by size and bit pattern. So the obvious way is the only one. Commented Jan 7, 2010 at 10:05

3 Answers 3

31

Basically something has 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.

In short, something will 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;
}
Sign up to request clarification or add additional context in comments.

3 Comments

You could make it an extension method too.
You can name it convertToDoubles or toDoubles and provide overloads for other primitive (and boxed) types.
There seems to be no convenience method in java for this....And seems like not any library having an implementation of this....ah-mazing!
12

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

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

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

1 Comment

I don't buy that Jon Skeet's is better. In fact, this one could be much faster optimized and done in parallel. It's clear what's happening and might well be turned into the functional equivalent of (double[]) floatArray. The reason to use streams is so that it tells the compiler we do not care how its done or in what order, just map all these values to these other ones. Rather than loop through them externally.
-1

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.

HTH

5 Comments

I see one very obvious reason: calling a method that needs a double[] while you have your data in a float[].
obviously, but the OP doesn't mention that. I just wanted to give an alternative since he asked for it.
Andreas_D explained it. I needed to make two libraries to play with each other. One of then works with double[] vectors, the other with float[] (vector in the mathematical meaning). Java troubles with casting arrays, matching these libraries is not that simple. It seems there does not exist any solution without the need to loop over the array. Thanks for the reply.
Double.valueOf [possibly] instantiates an object. Use a simple cast.
@AleksandrDubinsky you don't even need an explicit cast: float f = 0; double d = f; works just fine

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.