Skip to content

Generated DTO comparers treat equal floating-point values as different (NearlyEqual not negated) #426

Closed

Description

@samatstariongroup

Problem

The generated DTO comparers treat two equal floating-point values as different. The generator emits the floating-point check without its negation:

SysML2.NET.Extensions/Core/AutoGenDtoComparers/LiteralRationalComparer.cs:111:

if (FloatingPointComparer.NearlyEqual(x.Value, y.Value, FloatingPointComparer.DefaultDoubleEpsilon)) return false;

Equals returns false exactly when the values are nearly equal, and true when they differ. So ComparerProvider.Resolve(literal).Equals(literal, copy) is false for a LiteralRational and an identical copy of it.

Cause

SysML2.NET.CodeGenerator/HandleBarHelpers/PropertyHelper.cs:845-862 emits all four floating-point branches without !:

  • double, nullable and not nullable (lines 845 and 849);
  • float, nullable and not nullable (lines 859 and 862).

The float branch also has two more slips:

  • The non-nullable line is emitted after the nullable one even for a nullable float, because there is no else.
  • It compares with DefaultDoubleEpsilon instead of DefaultFloatEpsilon.

Impact

Today the only generated comparer with a real-valued property is LiteralRationalComparer (LiteralRational::value : Real). Every comparison of two LiteralRationals is inverted on value.

Fix

Negate the four checks, add the else to the float branch, and use DefaultFloatEpsilon there. Then regenerate the comparers. A unit test that compares a LiteralRational with a copy of itself, and with one whose value differs, catches it.

How it was found

The Flexo integration scenario of #407 (step 4, KerML Kernel) reads a LiteralRational with value = 2.5 back from the server. It reports "differs from what was sent", although the value read back is equal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions