[jboss-jira] [JBoss JIRA] (DROOLS-167) Inaccurate comparison because of String to Double coercion

Toshiya Kobayashi (JIRA) jira-events at lists.jboss.org
Fri Jun 14 01:26:54 EDT 2013


Toshiya Kobayashi created DROOLS-167:
----------------------------------------

             Summary: Inaccurate comparison because of String to Double coercion
                 Key: DROOLS-167
                 URL: https://issues.jboss.org/browse/DROOLS-167
             Project: Drools
          Issue Type: Bug
      Security Level: Public (Everyone can see)
    Affects Versions: 6.0.0.Beta3
            Reporter: Toshiya Kobayashi
            Assignee: Mark Proctor


If you compare String fields which consist of many digits, it may be wrongly evaluated due to String to Double coercion and Double's inaccuracy.

The following test doesn't fire the rule.

{code:java}
import org.drools.compiler.Person;
rule R1
  when
    $p : Person( name < "90201304122000000000000017" )
  then
end
{code}

{code:java}
ksession.insert( new Person( "90201304122000000000000015", 38 ) );
{code}

In Drools 6.0.0.beta3, the coercion is done by org.mvel2.math.MathProcessor

{code:java}
public strictfp class MathProcessor {

...

  private static Object _doOperations(int type1, Object val1, int operation, int type2, Object val2) {
    if (operation < 20) {
      if (((type1 > 49 || operation == EQUAL || operation == NEQUAL) && type1 == type2) ||
              (isIntegerType(type1) && isIntegerType(type2) && operation >= BW_AND && operation <= BW_NOT)) {
        return doOperationsSameType(type1, val1, operation, val2);
      }
      else if ((type1 > 99 && (type2 > 99))
          || (operation != 0 && isNumber(val1) && isNumber(val2))) {
        return doPrimWrapperArithmetic(getNumber(val1, type1),
            operation,
            getNumber(val2, type2), true, box(type2) > box(type1) ? box(type2) : box(type1));
      }
...

  private static Double getNumber(Object in, int type) {
    if (in == null)
      return 0d;
    switch (type) {
...
      case DataTypes.STRING:
        return Double.parseDouble((String) in);
...
{code}

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira


More information about the jboss-jira mailing list