Double values comparison changed to compare method.

Using the Double.compare() method is often preferred over the == comparison
operator for comparing double values due to several reasons:

Handling NaN (Not-a-Number) values: The Double.compare() method correctly
handles NaN values, while the == operator does not. If either of the operands
is NaN, the == operator will always return false, regardless of the other
operand. In contrast, Double.compare() will correctly evaluate NaN values
according to the IEEE 754 floating-point standard.

Handling positive and negative zero: The == operator treats positive zero and
negative zero as equal, whereas they are distinct values in IEEE 754
floating-point representation. Double.compare() correctly distinguishes
between positive and negative zero.

Robustness against rounding errors: Floating-point arithmetic can introduce
rounding errors, causing two double values that should be equal to differ
slightly. Directly comparing them with the == operator might yield unexpected
results due to these small differences. Double.compare() allows you to define
a tolerance level if necessary, providing more control over how equality is
determined.

Consistent behavior: The behavior of Double.compare() is consistent and
predictable across different platforms and JVM implementations, as it follows
the IEEE 754 standard. On the other hand, the behavior of the == operator
might vary depending on the platform and compiler optimizations.

Suitability for sorting: Double.compare() returns an integer value that can
be directly used for sorting double values in ascending or descending order.
This makes it convenient for sorting arrays or collections of double values.

Overall, while the == operator might work in some cases, using
Double.compare() provides more robust and predictable behavior, especially
when dealing with floating-point numbers in Java.

Change-Id: I5756936a0d2b4fe11b9113ddd33b6ae691f5103f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/166796
Tested-by: Jenkins
Reviewed-by: Caolán McNamara <caolan.mcnamara@collabora.com>
Tested-by: Caolán McNamara <caolan.mcnamara@collabora.com>
This commit is contained in:
Todor Balabanov 2024-04-28 12:14:40 +03:00 committed by Caolán McNamara
parent 27ed452ae6
commit 763149e2f4

View file

@ -211,7 +211,7 @@ public abstract class BaseEvolutionarySolver extends BaseNLPSolver {
//0 is a bad starting point, so just pick some other. //0 is a bad starting point, so just pick some other.
//That is certainly not optimal but the user should specify //That is certainly not optimal but the user should specify
//bounds or at least a good starting point anyway. //bounds or at least a good starting point anyway.
if (value == 0.0) if (Double.compare(value, 0.0D) == 0)
value = 1000; value = 1000;
double b1; double b1;
@ -366,7 +366,7 @@ public abstract class BaseEvolutionarySolver extends BaseNLPSolver {
switch (m_extConstraints[i].Operator.getValue()) { switch (m_extConstraints[i].Operator.getValue()) {
case SolverConstraintOperator.EQUAL_value: case SolverConstraintOperator.EQUAL_value:
result = value == targetValue; result = Double.compare(value, targetValue) == 0;
break; break;
case SolverConstraintOperator.GREATER_EQUAL_value: case SolverConstraintOperator.GREATER_EQUAL_value:
result = value >= targetValue; result = value >= targetValue;
@ -375,10 +375,10 @@ public abstract class BaseEvolutionarySolver extends BaseNLPSolver {
result = value <= targetValue; result = value <= targetValue;
break; break;
case SolverConstraintOperator.INTEGER_value: case SolverConstraintOperator.INTEGER_value:
result = Math.rint(value) == value; result = Double.compare(Math.rint(value), value) == 0;
break; break;
case SolverConstraintOperator.BINARY_value: case SolverConstraintOperator.BINARY_value:
result = (value == 0.0 || value == 1.0); result = (Double.compare(value, 0.0D) == 0 || Double.compare(value, 1.0D) == 0);
break; break;
} }
} else { } else {