-1

I have two large byte-arrays (byte[]) that I want need to compare. They should by logically equal but are not always technically equal. The reason being differences in how floating point numbers are represented in these two arrays.

Is there a way of comparing these by idenfitying the floating point numbers and testing if the numbers are within a error tolerance?

I suspect this situation is caused by the fact that one of these byte-arrays has been stored and retreived from a database (postgres).

So far I compare the two byte arrays like so:

public static bool AreEqual(byte[] a, byte[] b)
{
    if (a.Length == b.Length)
    {
        for (var i = 0; i < a.Length; ++i)
        {
            if (a[i] != b[i])
            {
                return false;
            }
        }
    }

    return true;
}

(Initially I serialized to JSON, but I had the same problem with that. Thought serializing to bytes would fix this, but no).

EDIT: To add context. One of the two byte-arrays is a 'recording' of complex object graphs representing the input and output of a method that I want to test. This byte-array is stored to a postgres database. The second byte-array is the same data, only in memory. I am trying to compare the two as a sort of regression test after refactoring, but the problem is something happens to floating point numbers when they are stored and retreived from the database.

I was hoping for a way of comparing without deserializing both back to their object representations. As far as I know, this is impossible, but I had to ask to be sure.

unique_ptr
  • 233
  • 3
  • 9
  • 1
    You mean the bytes represent floating point numbers? In what format? – Sweeper Sep 08 '22 at 05:33
  • 1
    If you want to compare data in a structured way, I suggest you serialise the data into those structures. – Jeremy Lakeman Sep 08 '22 at 05:47
  • 3
    "_Is there a way of comparing these by idenfitying the floating point numbers_" Yes, by getting the exact specification of what each of the bytes in the array means from whoever made/provided (or was responsible for making or suggesting to you to make/use) the byte array(s). Then, with that specification in hand, you should be able to process the bytes in the byte array(s) correctly, assuming the specification is really _exact_. –  Sep 08 '22 at 05:48
  • 2
    I suspect you are looking for a way to compare the byte arrays without actually parsing them into structured data. I think you are going to be disappointed. Floating point numbers that are only slightly different could still have very different binary representations. – John Wu Sep 08 '22 at 05:54

1 Answers1

1

First you need to convert your byte arrays to float arrays, if that is what they represent. You can either use a binary reader, or you could convert your array to a span, cast it, and copy the result to a new array.

For comparison Linq has a SequenceEquals method that compares all the individual values, and allows for a equality comparer to specify how that comparison is made.

You need to implement this equality comparer yourself, after reading about how floating point numbers should be compared. See Floating point guide and how to compare floating point numbers

JonasH
  • 28,608
  • 2
  • 10
  • 23