2

I have complex test cases with geometric entities. It is easy for me to visually verify that the test case has passed by looking at the generated geometry in a viewport like so.

When I am satisfied that the test is passing I take a hash of the geometry.

var hashCode = GetHashCodeForRegionResult(region);
hashCode.Should().Be(1243932659);

the calculation involves taking the hash of floating point numbers. Ordinarily this would be a bad thing to do. However if I input the same data to the algorithm then I would expect exactly the same result down to the bit level. Is this expectation valid especially if I am running the same test in the .Net 4.5.1 runtime on different CPU's, AMD vs INTEL, 64 bit vs 32 bit?

JK82
  • 415
  • 2
  • 13
  • I think the question can be simplified to: Will the same C# or IL always produce the exact same floating point results (given the same inputs)? I don't think we need to consider hashing here. Good question. – usr Jul 30 '14 at 13:55
  • I don't know the answer to your question, but be aware that there is both a positive zero and a negative zero. Innocent changes to the implementation might result in getting one or the other. They have different representations. Apart from this, I would think the answer is "yes"; perhaps if you add zero before finding the hash of a floating-point number you will get the desired result. – tmyklebu Jul 30 '14 at 15:19

2 Answers2

0

Assuming that everything is identical, then you'd expect even floating point operations to be deterministic, and yield identical results... unless you end up finding bugs in either hardware or software.

Floating point is notoriously sensitive to changes in the order of execution, even (especially !) where this is mathematically irrelevant. If there are small differences in libraries between versions, this could produce small differences, usually too small to notice.

So, yes, one can imagine ways in which results could, in theory, vary. But it's reasonable (IMHO) to proceed on the basis that the results will, in practice, be identical -- but not be devastated if there are small differences.

-2

This expectation is not valid because of IEEE 754; "floats" are approximations and therefore cannot be expected to be bit-wise equivalent to one another after executing math that should result in sameness. There are many examples where things should mathematically be the same -- but are not when using floats; and if the inputs aren't the same it follows naturally that the hash of the results wont be.

I'd review:

About determinism: How deterministic is floating point inaccuracy?

Community
  • 1
  • 1
dasm80x86
  • 363
  • 2
  • 7
  • But is the result *deterministic*? IEEE floats are defined to be deterministic. – usr Jul 30 '14 at 14:02
  • 1
    That is not what I am asking. I am asking if the same code with the same input will always return the same result. The equivalent test is to see if your code returns __-4.57966997657877E-16__ every time it is run on every platform that .Net supports. – JK82 Jul 30 '14 at 14:04
  • 1
    What does your code demonstrate? Are you claiming that it demonstrates that the same float does not always get the same hash? Because this is (is short) the question being asked… – Pascal Cuoq Jul 30 '14 at 14:31
  • Would you have the same problem with System.Double? I think I am running into a similar problem where after math GetHashCode() is producing different values. – Alex Hope O'Connor Oct 13 '15 at 04:29