Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utils] DoubleMath - implement boolean comparison operations #115

Open
vsubhuman opened this issue Dec 13, 2015 · 0 comments
Open

[Utils] DoubleMath - implement boolean comparison operations #115

vsubhuman opened this issue Dec 13, 2015 · 0 comments

Comments

@vsubhuman
Copy link
Member

DoubleMath provides #compare(double,double):int operation that does not allow nulls, cuz decision whether null is greater or lower than everything else, or should throw NPE is project-specific and is up to the library user.

But! There should be explicit comparison operations except #equals:

  1. #greaterThan(Double,Double):boolean
  2. #greaterThanOrEquals(Double,Double):boolean
  3. #lessThan(Double,Double):boolean
  4. #lessThanOrEquals(Double,Double):boolean

Rules for these methods are:

  1. Two nulls are equal
  2. Null is neither greater, nor lower than any other number or null.
  3. Two NaNs are equal
  4. NaN is neither greater nor lower than any other number or NaN.
  5. Any other values are compared by DoubleMath#compare

Note: NaN comparison rules are consistent with java rules for comparison operations: ==, <, <=, >, >=. Any comparison of NaN except equality is false by definition. Though two NaNs are equal.

Note: null comparison rules are same as for NaN. Any comparison of null except equality is false by definition.

Note:

NaN comparison rule breaks consistency between these explicit methods and #compare(double,double):int, cuz latter is forced to return an integer value and uses default comparison rules for double values based on the underlying bits.

So, for example:

DoubleMath.greaterThanOrEquals(12.0, Double.NaN);   // false
DoubleMath.lessThanOrEquals(12.0, Double.NaN);      // false
DoubleMath.compare(12.0, Double.NaN);               // 1

Double.NaN by bits comparison is considered to be the smallest number (smaller than negative infinity), but it IS neither greater, nor lower than 12.0 and definitely isn't equal to it.

So any consistency isn't possible in this particular case. Default #compare should be used when you need to sort values, or anything like this. But explicit methods should be used when you need binary answer to comparing two numbers by specific comparison operation.


Related to: #116

Note: there should not be overloaded versions of these methods for additional EPS parameter. DoubleMath.Instance should be used in case like this.

@vsubhuman vsubhuman added this to the Release: 0.0.10 milestone Dec 13, 2015
@vsubhuman vsubhuman changed the title [Utils] DoubleMath - implement boolena comparison operations [Utils] DoubleMath - implement boolean comparison operations Dec 13, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant