0

I want to convert a byte array containing floats(4 bytes as a float) into a double array in the fastest way possible so I'm using blockcopy as internet suggested it. But , it seems like the values were not correct after the conversion into double can somebody help with!

public static void Test()
        {
            var floatArray = new float[] { 123.45f, 123f, 45f, 1.2f, 34.5f };
            // create a byte array and copy the floats into it...
            var byteArray = new byte[floatArray.Length * sizeof(float)];



            Buffer.BlockCopy(floatArray, 0, byteArray, 0, byteArray.Length);

            // create a double array and copy the bytes into it...
            var doubleArray = new double[byteArray.Length / 4];

            Buffer.BlockCopy(byteArray, 0, doubleArray, 0, byteArray.Length);
            // do we have the same sequence of floats that we started with?
            foreach(var i in doubleArray)
            {
                Console.WriteLine(i);
                // outputs -->   387028163194470.4 ,   0.02500000981399353 ,    5.474008307E-315 ,   0 ,   0
            }
        }
1
  • BlockCopy() merely copies bytes, it doesn't try to convert them. But conversion is what you need, the internal representation of a float value is very different from a double value. Commented Feb 10, 2023 at 15:52

2 Answers 2

2

If you need speed, you're not going to get faster than this...

public static void Test()
{
    float val = 1024.24f;
    unsafe
    {
        // byte buffer
        Span<byte> bytes = stackalloc byte[Unsafe.SizeOf<double>()];

        // float to bytes
        fixed (byte* ptr = bytes) *((float*)ptr) = val;

        // bytes to double
        double dbl = default;
        fixed (byte* ptr = bytes) dbl = *((float*)ptr);
        
        // result
        Console.Write(dbl);
        
        //output: 1024.239990234375d == 1024.24f
    }   
}

See instructions below to understand what's happening on here - . The bytes are loaded onto the stack as a Float32, top of stack is converted to Float64, and then it's popped off the stack into into the local double at index 2...

MSIL

Sign up to request clarification or add additional context in comments.

5 Comments

How does that work since float and double have different binary representations? Where exactly is the conversion happening? Can you elaborate a bit more on your code?
They're identical as all single values can be represented by double. Both conform to IEEE 754 double just has more possible digits. The 1024.24F value that we start out with is actually 1024.239990234375. Go here and plug in the original value in the top box...baseconvert.com/ieee-754-floating-point
@JohnAlexiou - sorry - i misread your question. see above for the explanation i added. the value goes from binary -> Float32 -> Float64
Why would this be faster than conversion with (double)x ? Wouldn't this translate to conv.r8 IL code also?
By x are you referring to the origin float value? The OP asked how to convert a "byte array containing float" to double.
1

Array.Copy() can convert from float to double.

Here is an example

static class Program
{
    static void Main(string[] args)
    {
        float[] f_arr = new float[] { 1.1111f, 22.22f, 333.3f };

        Console.WriteLine(string.Join(", ", f_arr));

        double[] d_arr = new double[f_arr.Length];

        Array.Copy(f_arr, d_arr, f_arr.Length);

        Console.WriteLine(string.Join(", ", d_arr));
    }
}

with output:

1.1111, 22.22, 333.3
1.1110999584198, 22.2199993133545, 333.299987792969

as you can see the precision is crap (since the 24 bit mantissa of float is assigned to the 53 bit mantissa of double). But it will do the trick without worrying about using byte arrays, and Buffer.Copy().

3 Comments

The precision isn't crap, double is just more precise. Try testing equality - 2.22f == 22.2199993133544921875d
Wouldn't 22.22f == 22.2199999999999997 ?
Yes, we're saying the same thing. My brain just processed what you wrote in the inverse direction...crap -> double :)

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.